Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Jun-11 23:29pm    
Correct, my 5.
--SA
Cool Smith 16-Jun-11 3:32am    
i tried that, but i get some problems, see my reply to Naerling post
Dave Kreskowiak 16-Jun-11 7:49am    
That's nice, but you forgot to mention what the problem was...
The same handler (or handlers if you set a different handler for each type of control) can handle events from multiple sources: the sender 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:
VB
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 Sub
You can then provide a similar handler for all the Checkboxes, the NumericUpDowns, etc.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Jun-11 23:29pm    
Correct, my 5.
--SA
You could have one Method listen to the Events of all those controls.
For example:
VB
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.
VB
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Jun-11 23:29pm    
Correct, my 5.
--SA
Sander Rossel 16-Jun-11 0:58am    
Thanks :)
Cool Smith 16-Jun-11 3:28am    
Hello, all i want is just to detect a change in their value changes, see the approach am using


For Each ctrl As Control In pnlGeneral.Controls
If TypeOf ctrl Is CheckBox Then
AddHandler (ctrl.Click), AddressOf Control_Changed
ElseIf TypeOf ctrl Is TextBox Then
AddHandler (ctrl.TextChanged), AddressOf Control_Changed
ElseIf TypeOf ctrl Is NumericUpDown Then
'AddHandler (ctrl.ValueChanged), AddressOf Control_Changed
End If

Next
Sander Rossel 16-Jun-11 13:15pm    
I am laughing right now... You ask us this question and you give the quickest solution yourself? ;)
Looping through the Controls in a Panel and adding the Handlers is certainly a good way to go. Although for the CheckBox I would use the CheckChanged Event. If you Click right next to the CheckBox (such as in the text behind it) the Click Event fires, without the value being changed. Do this like:
AddHandler DirectCast(ctrl, CheckBox).CheckChanged, AddressOf Control_Changed

Also keep in mind that if you would add a ContainerControl (such as a Panel, TabControl or GroupBox) in the Panel that every Control in this new ContainerControl will NOT have their Events added.
Other then that nice solution though :thumbsup:
Cool Smith 16-Jun-11 14:52pm    
am having problem with the NumericUpDown


ElseIf TypeOf ctrl Is NumericUpDown Then
DirectCast(ctrl, NumericUpDown)
AddHandler (ctrl.ValueChanged), AddressOf Control_Changed
End If

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900