![](/img/trans.png)
[英]InvalidCastException: Unable to cast COM object of type x to interface type y
[英]Unable to cast object of type x to type y
public void darkModeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!darkmode)
{
foreach(TextBox b in this.Controls)
{
}
It says "Unable to cast object of 'System.Windows.Forms.MenuStrip' to type 'System.Windows.Forms.TextBox'.'
並非Controls
集合中的所有元素都是文本框,這就是失敗的原因。
嘗試:
foreach(object b in this.Controls)
{
if (b is TextBox textBox)
{
// use textBox...
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.