Results 1 to 14 of 14

Thread: Capturing when ANY textbox on a form has changed..[RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    Buffalo, NY
    Posts
    297

    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.

  2. #2
    Hyperactive Member Lil Ms Squirrel's Avatar
    Join Date
    Nov 2004
    Location
    planet squirrel
    Posts
    494

    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

  3. #3
    Hyperactive Member
    Join Date
    Jun 2002
    Location
    midewest u.s.
    Posts
    275

    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.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    Buffalo, NY
    Posts
    297

    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

  5. #5
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    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
    "The dark side clouds everything. Impossible to see the future is."

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    Buffalo, NY
    Posts
    297

    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

  9. #9
    Hyperactive Member
    Join Date
    Jun 2002
    Location
    midewest u.s.
    Posts
    275

    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.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    Buffalo, NY
    Posts
    297

    Re: Capturing when ANY textbox on a form has changed..[RESOLVED]

    Kleinma,

    For the record it worked perfectly. You 4r3 1337 d00d!

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    Buffalo, NY
    Posts
    297

    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

  12. #12
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

    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
  •  



Click Here to Expand Forum to Full Width