簡體   English   中英

如何在C#winforms中為表單上的多個文本框控件調用Text_Changed事件

[英]How call the Text_Changed Event for multiple text box controls on a form in C# winforms

我的表單有大約20個TextBox控件,我想激發Text_Changed事件,而不是為每個單獨的文本框添加事件。 有沒有辦法循環文本框來觸發此事件? 我想要做的是在文本更改時清除標簽控件。 我沒有顯示消息框,而是使用標簽控件來顯示消息。 我還設置了如果文本框包含無效數據的地方,我選擇所有文本並將焦點放在該TextBox這樣當用戶重新輸入信息時,標簽控件就會清除消息。

編輯:

為了解決一些困惑,這里是我的驗證方法中的一些代碼

if (txtBentLeadsQty.Text == "")
{
    //ValidData = true;
    BentLeadsCount = 0;
}
else 
{
    if (int.TryParse(txtBentLeadsQty.Text.Trim(), out BentLeadsCount))
        ValidData = true;
    else
    {
        ValidData = false;
        lblError.Text = "Bent Lead Qty must be a numeric value";
        txtBentLeadsQty.SelectAll();
        txtBentLeadsQty.Focus();
    }
}

我已經有辦法檢查數值,並且我輸入代碼來選擇所有輸入的文本並在值不是數字時給予焦點,我只想在文本更改時清除Label控件的方法如果用戶點擊退格鍵或開始輸入錯誤發生的原因,我會突出顯示該TextBox所有文本(如果它無效)。 如果我在每個文本框TextChanged事件中放置代碼,我可以這樣做,但是為了保存編碼,我想知道是否有辦法清除標簽控件,如果任何文本框從我的驗證方法拋出錯誤而不是添加單個事件20個文本框。

注意:並非所有文本框都會輸入數據,如果TextBox為null,這些是我放入代碼的數量文本框,為變量賦值0。

您可以使用以下代碼:

private void Form1_Load(object sender, EventArgs e)
{
    foreach (Control ctrl in this.Controls)
    {
        if ((ctrl as TextBox) != null)
        {
            (ctrl as TextBox).TextChanged += Form1_TextChanged;
        }
    }
}
private void Form1_TextChanged(object sender, EventArgs e)
{
    MessageBox.Show((sender as TextBox).Name);
}

我假設您想要動態地向表單中的所有文本框添加相同的處理程序,即無需為可視編輯器(或代碼)中的每個文本框添加它們。

如果是這樣,這可能就是您所需要的:

// Iterate over all controls in the current form:
foreach (var ctl in Controls)
{
    // Check if the current control is a textbox 
    // (will be null if it is of another type)
    var txtBox = ctl as TextBox;
    if (txtBox != null)
    {
        txtBox.TextChanged += YourMethod();
    }
}

聽起來好像你想以編程方式在每個文本框上觸發方法。

首先,創建一個包含大約20個文本框的數組。

var textBoxes = new []{textBox0, textBox1, textBox2};

循環遍歷每個框並調用文本更改的方法

foreach(var textBox in textBoxes)
{
     TextChangedMethod(textBox);
}

如果您調用的方法是由Visual Studio生成的,則它將為EventArgs采用第二個參數。 您可以簡單地為其傳遞null值。

TextChangedMethod(textBox, null);

創建一個方法,如下所示:

public void TextChanged(object sender, EventArgs e)
{
    //Text-changed code here.
}

此時,您可以單擊表單上的每個文本框,並將新方法添加到要發生的事件中。 單擊表單上的文本框,然后單擊屬性菜單中的thunderbolt圖標並向下滾動到“TextChanged”事件。 將該事件的下拉列表更改為新方法。

要么

初始化表單組件后,在運行時添加事件:

textBox1.TextChanged += new EventHandler(TextChanged);
textBox2.TextChanged += new EventHandler(TextChanged);
textBox3.TextChanged += new EventHandler(TextChanged);

如果將所有文本框添加到數組中,並使用foreach循環遍歷它們以將事件添加到每個文本框,則可以更輕松地完成此操作。 您也可以從表單中獲取所有文本框並以相同的方式循環,但我不知道您是否有其他控件/文本框可以讓您避免使用此方法。

使用foreach語句。

List<TextBox> TextblockCollection = null;//You have to add them all individually to the list
foreach (var text in TextblockCollection)
{
    //Change the text to the same thing, firing the method
    text.Text = text.Text
}

因此,您要檢查何時更改了任何文本框,然后檢查更改的文本框中的新輸入是否為數字(我假設為整數),然后在標簽中顯示消息(如果它不是數字)。

public MainForm()
{
    InitializeComponent();

    foreach (Control control in this.Controls)
    {
        if (typeof(control)==typeof(TextBox))
        {
            (control as TextBox).TextChanged += CommonHandler_TextChanged;
        }
    }
}

private void CommonHandler_TextChanged(object sender, EventArgs e)
{
    int number;
    string input=(sender as TextBox).Text;
    bool isnumber=false;
    isnumber = Int32.TryParse(input, number);
    if(isnumber==false)
    {
        yourLabel.Text = "This textbox contains an incorrect number: "
                         +(sender as TextBox).Name;
    }
    else{ /*use the number*/ }
}

暫無
暫無

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

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