Capturing when ANY textbox on a form has changed..[RESOLVED]
Hey guys...
I'm writing a record editing screen, and I want to capture whenever ANY textbox (or combo box, or list box for that matter) has been changed so I can enable a 'Save' button. I'd prefer to not have to write a Changed event for every control , especially since I need to make six or seven of these forms and many of them will have many controls on each form.
Any suggestions? I could inherit the textbox classes I know, but I was hoping for a simpler solution.
Thanks,
Ben
Re: Capturing when ANY textbox on a form has changed..
I don't know of any way other than using their "...Changed" events.
You could always create a sub that Handles all textboxes, but then again You still have to type all the textbox/combo box names.
Code:
Private Sub TextChanged(ByVal sender As System.Object, ByVal e as System.EventArgs) _
Handles txtbox1.TextChanged, txtBox2.TextChanged, txtBox3.TextChanged
RecordChanged = True
End Sub
Sorry if that's not what you are hoping for but it is all I can think of.
Re: Capturing when ANY textbox on a form has changed..
You can add each of the controls changed events to a single event handler ie:
Code:
Private Sub TextValueChanged(sender as Object, e as System.EventArgs) _
Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
If Sender.Text.Trim.Length > 0 Then btnSave.Enabled = True
'or whatever code you want to add to the sub
End Sub
Re: Capturing when ANY textbox on a form has changed..
Those are good suggestions... and exactly the kind of thing I was thinking I would have to do.
The problem is that as development continues items are going to be added, removed, changed, etc... and I hate having to remember to add an additional line of code for each new control I drop on.
Ah well, I was just hoping.
Thanks Guys,
Ben
Re: Capturing when ANY textbox on a form has changed..
Quote:
Originally Posted by BenFinkel
The problem is that as development continues items are going to be added, removed, changed, etc... and I hate having to remember to add an additional line of code for each new control I drop on.
You can create a procedure that browse all controls of your form of type 'TextBox' .
Regards
Jorge
Re: Capturing when ANY textbox on a form has changed..
wow you guys really have to explore the power of .NET
only Asgorath was on the right track to tackle this problem..
one of the really cool features of .NET, is the ability to add handlers for events, and an even can have more than 1 handler...
observe:
I have a data entry app, that I only want to prompt the user to save changes, if in fact something has changed.
Code:
Private Sub AddSaveHandlers(ByVal c As Control)
For Each ctrl As Control In c.Controls
If ctrl.Controls.Count > 0 Then
AddSaveHandlers(ctrl)
End If
Select Case ctrl.GetType.Name.ToUpper
Case "TEXTBOX", "COMBOBOX"
AddHandler ctrl.TextChanged, AddressOf SetPromptSaveTrue
Case "CHECKBOX"
AddHandler CType(ctrl, CheckBox).CheckStateChanged, AddressOf SetPromptSaveTrue
Case "RADIOBUTTON"
AddHandler CType(ctrl, RadioButton).CheckedChanged, AddressOf SetPromptSaveTrue
End Select
Next
End Sub
Private Sub SetPromptSaveTrue(ByVal sender As System.Object, ByVal e As System.EventArgs)
mPromptSave = True
End Sub
in the "Windows Form Designer generated code" Sub New where it says "'Add any initialization after the InitializeComponent() call" I put
AddSaveHandlers(Me)
this function is recusive in that if you have any container controls, like groupboxes, pictureboxes, panels, etc.. it will add the handler to all the controls contained in them as well
Re: Capturing when ANY textbox on a form has changed..
you can then also add specific per control handlers.. even for the same TextChanged or CheckStateChanged events.. just like you normally would, and they will work in conjunction with these dynamic handlers
Re: Capturing when ANY textbox on a form has changed..
Kleinma,
That's %$(*ing perfect. Thanks for that, it's obvious (now that I see it :))
--Ben
Re: Capturing when ANY textbox on a form has changed..
That is a great idea kleinma. I don't know why that never crossed my mind. I use that alot when it comes to dynamically creating controls, but never once thought to do it on controls I created in design time. I will have to keep that in mind next time.
Re: Capturing when ANY textbox on a form has changed..[RESOLVED]
Kleinma,
For the record it worked perfectly. You 4r3 1337 d00d! :thumb:
Re: Capturing when ANY textbox on a form has changed..[RESOLVED]
To top it off...
I made a form frmEdit and put the code in there, and now all of my Edit forms inherit that form and zero additional code writing and I've got my edit form!
Yea!
--Ben
Re: Capturing when ANY textbox on a form has changed..[RESOLVED]
glad to hear it worked for you....
chalk another one up for what .NET has that VB6 just couldn't do :bigyello:
Re: Capturing when ANY textbox on a form has changed..[RESOLVED]
I'm with BenFinkel - that's some pretty cool #^$%#W there kliema. It always seems so obvious in hindsight.
Crap - now I can't wait to get home to try it out on a project I'm working on.
:confused: Here's a thought, if I create a global object that points to an instance of a class, can I add handlers to controls that then point to a method in that class?
Tg
Re: Capturing when ANY textbox on a form has changed..[RESOLVED]
Quote:
Originally Posted by techgnome
I'm with BenFinkel - that's some pretty cool #^$%#W there kliema. It always seems so obvious in hindsight.
Crap - now I can't wait to get home to try it out on a project I'm working on.
:confused: Here's a thought, if I create a global object that points to an instance of a class, can I add handlers to controls that then point to a method in that class?
Tg
I think so.. I don't think there is any scope limitation on the parameter of addhandler, as long as the method is exposed to the routine calling addhandler