detect change to controls

pettrer

Well-known member
Joined
Sep 5, 2008
Messages
92
Programming Experience
10+
Hi,

I have a windows form with a lot of controls (VB.Net).

I need to chnage the text of the cancel button from "Close" to "Cancel change" when any of these controls are checked/clicked/changed.

I could do LOTS of subs to generate cmdCancel.Text = "Cancel change", but I'm thinknig there might be a way to hook up to the page events or so, so that as soon as ANY control is changed, the name of the button is changed, like this:

VB.NET:
Private Sub frmMyform_changed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.ControlChanged
    cmdCancel.Text = "Cancel change"
End Sub

However, there is no such event. Does anyone know of a way to handle this situation?

All the best,

Pettrer
 
This may be a little to generic and some futher processing may be neccesary however if you override the wndproc and intercept the WM_COMMAND message and place the code to change the text there. I have no doubt that you will have to either use the HIWORD of the wParam to determine which control is recieving the message, or get the handle from the lParam and determine from there if you should change the text. Very much an 'off the top of my head' answer. But this is probably how i'd go about it.
 
Thanks a lot for your reply.

However, this seems too "risky" for me (as I wouldn't feel at home with ocde at that magnitude...).

Is there no way of doing as I proposed above, or perhaps loop trough all controls in a forloop on click or so? (There could be a global boolean set to true the first time the user has changed something, to minimise CPU overhead.)

Thanks again,

Pettrer
 
You only need one event handler. You just every control in the Handles clause, e.g. "Handles TextBox1.TextChanged, TextBox2.TextChanged".

Thanks!

I now have say thirty controls in the handles clause like below (posted here for other users) and it works like a charm.

/Pettrer

VB.NET:
Private Sub Changed(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
    txtEfternamn.TextChanged, txtEpost.TextChanged, etc
 
Note that you don't have to add all those events to the Handles clause manually either. If you go to the designer and select all the controls then open the Properties window and click the Events button, you can then double-click the desired event and the IDE will create the method and include all the selected controls in the Handles clause. You can add more controls by selecting the existing method from the drop-down list for the desired event in the Properties window.
 
Note that you don't have to add all those events to the Handles clause manually either. If you go to the designer and select all the controls then open the Properties window and click the Events button, you can then double-click the desired event and the IDE will create the method and include all the selected controls in the Handles clause. You can add more controls by selecting the existing method from the drop-down list for the desired event in the Properties window.

Very neat!

P
 
Back
Top