簡體   English   中英

在C#中以編程方式刪除文本框的最簡單方法是?

[英]Most Simple way to programatically delete textboxes in C#?

我有一個用於程序的文本框字典。 更改選項時,我希望刪除框,以便可以在下一頁創建新框。

    public static bool command0 () {
        foreach (TextBox tBox in Globals.inputBoxes)
            tBox = null;
        return true;
    }

返回此錯誤

Cannot convert type 'System.Collections.Generic.KeyValuePair<string,System.Windows.Forms.TextBox>' to 'System.Windows.Forms.TextBox' (CS0030) - C:\Users\e309706\Documents\SharpDevelop Projects\simulator 3\simulator 3\Globals.cs:73,4

是否有一種簡單正確的方法來做我想做的事情?

您的Globals.inputBoxes是一個字典,因此它們將遍歷鍵值對。 將代碼調整為此:

// This kvp variable is what your exception is saying was wrong.
foreach (KeyValuePair<string, TextBox> kvp in Globals.inputBoxes)
{
    Textbox txt = kvp.Value;
    // Do what you need to with the txt object now
}

編輯

還有另一種方法可以做到這一點,正如幾個人建議的那樣:

foreach (TextBox txtin Globals.inputBoxes.Values)
{
    // Do what you need to with the txt object now
}

關於這種附加方法的問題是,很多時候,您可能還希望在循環中使用字典中的鍵。 僅迭代值將使您無法再次訪問字典而無法訪問該值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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