簡體   English   中英

無法將 x 類型的 object 轉換為 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM