如何将窗体上的所有文本框控件与相同的事件处理程序关联

时间:2022-07-03 15:53:07

VB 2008.

I have several text boxes on a form and I want each of them to use the same event handler. I know how to manually wire each one up to the handler, but I'm looking for a more generic way so if I add more text boxes they will automatically be hooked up to the event handler.

我在表单上有几个文本框,我希望每个文本框都使用相同的事件处理程序。我知道如何手动将每一个连接到处理程序,但我正在寻找一种更通用的方式,所以如果我添加更多文本框,它们将自动连接到事件处理程序。

Ideas?

EDIT: Using the C# sample from MusiGenesis (and with the help of the comment left by nick), I wrote this VB code:

编辑:使用MusiGenesis的C#样本(以及在nick留下的评论的帮助下),我写了这个VB代码:

Private Sub AssociateTextboxEventHandler()
  For Each c As Control In Me.Controls
    If TypeOf c Is TextBox Then
      AddHandler c.TextChanged, AddressOf tb_TextChanged
    End If
  Next
End Sub

Thanks a lot! SO is great.

非常感谢!太棒了。

6 个解决方案

#1


10  

Do something like this in your form's load event (C#, sorry, but it's easy to translate):

在表单的加载事件中执行类似的操作(C#,抱歉,但很容易翻译):

private void Form1_Load(object sender, EventArgs e)
{
    foreach (Control ctrl in this.Controls)
    {
        if (ctrl is TextBox)
        {
            TextBox tb = (TextBox)ctrl;
            tb.TextChanged += new EventHandler(tb_TextChanged);
        }
    }

}

void tb_TextChanged(object sender, EventArgs e)
{
    TextBox tb = (TextBox)sender;
    tb.Tag = "CHANGED"; // or whatever
}

It's not properly recursive (it won't find text boxes on panels, for example), but you get the idea.

它没有正确的递归(例如,它不会在面板上找到文本框),但是你明白了。

#2


3  

"c As Control" has no "TextChanged" event, so it will throw an error. You may optimize that to Linq and also get rid of that error with:

“c As Control”没有“TextChanged”事件,因此会抛出错误。您可以将其优化为Linq并使用以下方法消除该错误:

For Each c As TextBox In Controls.OfType(Of TextBox)()
    AddHandler c.TextChanged, AddressOf tb_TextChanged
Next

But that would avoid to go deeper than first level thou.

但这样可以避免比第一级更深入。

#3


2  

You could recursively iterate over the Controls collection in the OnLoad of the form and assign the event handler to any text boxes you find.

您可以递归迭代窗体的OnLoad中的Controls集合,并将事件处理程序分配给您找到的任何文本框。

#4


0  

It might be possible with a macro, but otherwise, I a m not aware of anything that would automatically wire an control to a generic handler.

有可能使用宏,但除此之外,我不知道任何会自动将控件连接到通用处理程序的东西。

The only easy way I know of would be to select all appliciable textboxes, and simply set the eventhandler for the click event at the same time, but that isn't automatic.

我所知道的唯一简单方法是选择所有可应用的文本框,并简单地同时为click事件设置eventhandler,但这不是自动的。

#5


0  

I think this will update all text controls.

我认为这将更新所有文本控件。

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            UpdateControls(Me)
    End Sub


    Private Sub UpdateControls(ByVal myControlIN As Control)
        Dim myControl

        For Each myControl In myControlIN.Controls
            UpdateControls(myControl)
            Dim myTextbox As TextBox
            If TypeOf myControl Is TextBox Then
                myTextbox = myControl
                AddHandler myTextbox.TextChanged, AddressOf TextBox_TextChanged
            End If
        Next

    End Sub


    Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MsgBox("text changed in " & CType(sender, TextBox).Name)
    End Sub

#6


0  

I have many Textboxes inside many GroupBoxes inside a TabPage-Control.

我在TabPage-Control中的许多GroupBox中有很多Textbox。

To add/define/delete the Handler of those Textboxes i had to iterate first through the Tabpages, then through the Groupboxes in every Tabpage and then i can access the Textboxes inside these Groupboxes.

要添加/定义/删除那些文本框的处理程序,我必须先通过Tabpages迭代,然后通过每个Tabpage中的Groupbox,然后我可以访问这些Groupbox中的Textboxes。

' cycle through TabPages ...
For Each page As TabPage In Me.TabControl1.Controls.OfType(Of TabPage)()
    ' in every TabPage cycle through Groupboxes ...
    For Each gbox As GroupBox In page.Controls.OfType(Of GroupBox)()
        ' and in every TextBox inside the actual GroupBox
        For Each tbox As TextBox In gbox.Controls.OfType(Of TextBox)()
            AddHandler tbox.TextChanged, AddressOf _TextChanged
        Next
    Next
Next

Private Sub _TextChanged(sender As System.Object, e As System.EventArgs)
    somethingWasChanged = True
End Sub

#1


10  

Do something like this in your form's load event (C#, sorry, but it's easy to translate):

在表单的加载事件中执行类似的操作(C#,抱歉,但很容易翻译):

private void Form1_Load(object sender, EventArgs e)
{
    foreach (Control ctrl in this.Controls)
    {
        if (ctrl is TextBox)
        {
            TextBox tb = (TextBox)ctrl;
            tb.TextChanged += new EventHandler(tb_TextChanged);
        }
    }

}

void tb_TextChanged(object sender, EventArgs e)
{
    TextBox tb = (TextBox)sender;
    tb.Tag = "CHANGED"; // or whatever
}

It's not properly recursive (it won't find text boxes on panels, for example), but you get the idea.

它没有正确的递归(例如,它不会在面板上找到文本框),但是你明白了。

#2


3  

"c As Control" has no "TextChanged" event, so it will throw an error. You may optimize that to Linq and also get rid of that error with:

“c As Control”没有“TextChanged”事件,因此会抛出错误。您可以将其优化为Linq并使用以下方法消除该错误:

For Each c As TextBox In Controls.OfType(Of TextBox)()
    AddHandler c.TextChanged, AddressOf tb_TextChanged
Next

But that would avoid to go deeper than first level thou.

但这样可以避免比第一级更深入。

#3


2  

You could recursively iterate over the Controls collection in the OnLoad of the form and assign the event handler to any text boxes you find.

您可以递归迭代窗体的OnLoad中的Controls集合,并将事件处理程序分配给您找到的任何文本框。

#4


0  

It might be possible with a macro, but otherwise, I a m not aware of anything that would automatically wire an control to a generic handler.

有可能使用宏,但除此之外,我不知道任何会自动将控件连接到通用处理程序的东西。

The only easy way I know of would be to select all appliciable textboxes, and simply set the eventhandler for the click event at the same time, but that isn't automatic.

我所知道的唯一简单方法是选择所有可应用的文本框,并简单地同时为click事件设置eventhandler,但这不是自动的。

#5


0  

I think this will update all text controls.

我认为这将更新所有文本控件。

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            UpdateControls(Me)
    End Sub


    Private Sub UpdateControls(ByVal myControlIN As Control)
        Dim myControl

        For Each myControl In myControlIN.Controls
            UpdateControls(myControl)
            Dim myTextbox As TextBox
            If TypeOf myControl Is TextBox Then
                myTextbox = myControl
                AddHandler myTextbox.TextChanged, AddressOf TextBox_TextChanged
            End If
        Next

    End Sub


    Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MsgBox("text changed in " & CType(sender, TextBox).Name)
    End Sub

#6


0  

I have many Textboxes inside many GroupBoxes inside a TabPage-Control.

我在TabPage-Control中的许多GroupBox中有很多Textbox。

To add/define/delete the Handler of those Textboxes i had to iterate first through the Tabpages, then through the Groupboxes in every Tabpage and then i can access the Textboxes inside these Groupboxes.

要添加/定义/删除那些文本框的处理程序,我必须先通过Tabpages迭代,然后通过每个Tabpage中的Groupbox,然后我可以访问这些Groupbox中的Textboxes。

' cycle through TabPages ...
For Each page As TabPage In Me.TabControl1.Controls.OfType(Of TabPage)()
    ' in every TabPage cycle through Groupboxes ...
    For Each gbox As GroupBox In page.Controls.OfType(Of GroupBox)()
        ' and in every TextBox inside the actual GroupBox
        For Each tbox As TextBox In gbox.Controls.OfType(Of TextBox)()
            AddHandler tbox.TextChanged, AddressOf _TextChanged
        Next
    Next
Next

Private Sub _TextChanged(sender As System.Object, e As System.EventArgs)
    somethingWasChanged = True
End Sub