检测多个控件的值更改 [英] detect value changes for multiple controls
问题描述
控件示例包括
复选框
文字框
NumericUpDown
这里的问题是我在表单上总共有多达50个控件,我该怎么做?
好吧,我想您可以连接ValueChanged,所有这些控件的TextChanged和CheckChanged事件都属于同一事件处理程序.我认为它们都具有相同的签名,因此应该很容易工作.
相同的处理程序(或为每种控件类型设置不同处理程序的处理程序)可以处理来自多个源的事件:sender
参数告诉您以前是哪个控件.
例如,对于TextBox控件,您可以使用相同的处理程序为窗体上的所有TextBox处理TextChanged事件:
私有 子 TextBox_TextChanged( ByVal 发件人 As 系统.对象, ByVal e As System.EventArgs)句柄 TextBox1.TextChanged Dim t As TextBox = TryCast (发送方, 文本框) 如果 t 是不是 没什么 然后 ... 结束 如果 结束 子然后可以为所有Checkbox,NumericUpDowns等提供类似的处理程序.
您可以让一个方法侦听所有这些控件的事件.
例如:
私有 子 ValueChanged( ByVal 发件人 As 对象, ByVal e As System.EventArgs)句柄 CheckBox1.CheckChanged,TextBox1.TextChanged,NumericUpDown1.ValueChanged ' 要检查实际触发了哪个Control的事件,可以将发送方投射到Control. ' 当然,您也可以完全不使用控件. DoSomething( DirectCast (发送方,控件)) 结束 子 私有 子 DoSomething( ByVal ctl 作为控件) ' 在此处使用控件进行操作. 结束 子
另外,您可以为每个控件使用单独的方法.
私有 子 CheckBox1_CheckChanged( ByVal 发件人 As 对象, ByVal e As System.EventArgs)句柄 CheckBox1.CheckChanged DoSomething( DirectCast (发送方,控件)) 结束 子 私有 子 TextBox1_TextChanged( ByVal 发件人目标 对象, ByVal e As System.EventArgs) DirectCast (发送方,控件)) 结束 子 私有 子 NumericUpDown1_ValueChanged( ByVal 发件人目标 对象, ByVal e As System.EventArgs) DirectCast (发送方,控件)) 结束 子 私有 子 DoSomething( ByVal ctl 作为控件) ' 在此处使用控件进行操作. 结束 子
后一种方法使您能够在每个控件的值更改时对每个控件执行特定的操作.
无论您选择哪种方法(或两者都选择),都应该对所有50个控件执行此操作.
i need to detect change in multiple control, and fire a sub procedure when any of the mention control value changes in a form.
example of the controls include
checkbox
textbox
NumericUpDown
the problem here is that i have upto 50 controls in all on the form, how do i do this?
Well, I guess you could wire up the ValueChanged, TextChanged and CheckChanged events of all of those controls to the same event handler. I think they all have the same signature so it should work pretty easily.
The same handler (or handlers if you set a different handler for each type of control) can handle events from multiple sources: thesender
parameter tells you which control it was.
For exampe, for a TextBox control, you might handle the TextChanged event for all TextBoxes on a form with the same handler:
Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged Dim t As TextBox = TryCast(sender, TextBox) If t IsNot Nothing Then ... End If End SubYou can then provide a similar handler for all the Checkboxes, the NumericUpDowns, etc.
You could have one Method listen to the Events of all those controls.
For example:
Private Sub ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckChanged, TextBox1.TextChanged, NumericUpDown1.ValueChanged ' To check which Control actually fired the Event you could cast the sender to Control. ' Of course you could also do completely nothing with the Control. DoSomething(DirectCast(sender, Control)) End Sub Private Sub DoSomething(ByVal ctl As Control) ' Do something with the Control here. End Sub
Alternatively you could have a seperate Method for each Control.
Private Sub CheckBox1_CheckChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckChanged DoSomething(DirectCast(sender, Control)) End Sub Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged DoSomething(DirectCast(sender, Control)) End Sub Private Sub NumericUpDown1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged DoSomething(DirectCast(sender, Control)) End Sub Private Sub DoSomething(ByVal ctl As Control) ' Do something with the Control here. End Sub
The latter approach gives you the ability to also do something specific for each of the Controls when their value changes.
No matter which approach you pick (or you take a bit of both) you should do this for all of the 50 Controls.
这篇关于检测多个控件的值更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!