|
-
Dec 9th, 2004, 09:09 AM
#1
Thread Starter
Hyperactive Member
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
Last edited by BenFinkel; Dec 9th, 2004 at 03:32 PM.
-
Dec 9th, 2004, 09:15 AM
#2
Hyperactive Member
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
Be who you are and say what you feel, because those who mind don't matter and those who matter don't mind.
Dr. Seuss 
-
Dec 9th, 2004, 09:15 AM
#3
Hyperactive Member
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.
-
Dec 9th, 2004, 09:23 AM
#4
Thread Starter
Hyperactive Member
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
-
Dec 9th, 2004, 09:50 AM
#5
Re: Capturing when ANY textbox on a form has changed..
 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
"The dark side clouds everything. Impossible to see the future is."
-
Dec 9th, 2004, 10:02 AM
#6
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
-
Dec 9th, 2004, 10:03 AM
#7
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
-
Dec 9th, 2004, 10:04 AM
#8
Thread Starter
Hyperactive Member
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
-
Dec 9th, 2004, 10:14 AM
#9
Hyperactive Member
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.
-
Dec 9th, 2004, 03:33 PM
#10
Thread Starter
Hyperactive Member
Re: Capturing when ANY textbox on a form has changed..[RESOLVED]
Kleinma,
For the record it worked perfectly. You 4r3 1337 d00d!
-
Dec 10th, 2004, 11:54 AM
#11
Thread Starter
Hyperactive Member
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
-
Dec 10th, 2004, 11:55 AM
#12
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
-
Dec 10th, 2004, 12:31 PM
#13
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.
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
-
Dec 10th, 2004, 12:54 PM
#14
Re: Capturing when ANY textbox on a form has changed..[RESOLVED]
 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.
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|