Results 1 to 14 of 14

Thread: enable button

  1. #1

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075

    enable button

    I have a save button on a form with 20 textboxes. I have it set to enable = false till I detect some changes in the textboxes and then change it to enable = true...whats the most efficient way to do besides checking the change event for each textbox?

    thanks
    It's tough being an unhandled exception...

    ___________
    VB.NET 2008
    VB.NET 2010
    ORACLE 11g
    CRYSTAL 11

  2. #2
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227
    the way i would do it is to add a sub that handles the validated event of all the text boxes, and there enable and disable your save button

    ( and i would add a sub that handles all the validating event of the text boxes , and cancel bad data input there )

  3. #3
    Addicted Member The Phoenix's Avatar
    Join Date
    Aug 2003
    Location
    With my wife
    Posts
    142
    If you mean that a change in any of the the textboxes can activate the button, you could use something like this:

    VB Code:
    1. Dim ctrl As Control
    2.         For Each ctrl In Me.Controls
    3.             If TypeOf ctrl Is TextBox Then
    4.                 If ctrl.Text <> Nothing Then
    5.                     SaveButton.Enable = True
    6.                 End If
    7.             End If
    8.         Next
    Take my love
    Take my land
    Take me where I cannot stand
    I don't care, I'm still free
    You can't take the sky from me...

  4. #4

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075
    does that code in the text change event? place it in there and did not flag any activity for the textbox?
    It's tough being an unhandled exception...

    ___________
    VB.NET 2008
    VB.NET 2010
    ORACLE 11g
    CRYSTAL 11

  5. #5
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you need to add a handler for all textboxes ( maybe on form load ) eg:
    VB Code:
    1. [Color=Blue]Private[/COLOR] [Color=Blue]WithEvents[/COLOR] txtBox [Color=Blue]As[/COLOR] TextBox
    2.  
    3.     [Color=Blue]Private[/COLOR] [Color=Blue]Sub[/COLOR] Form1_Load([Color=Blue]ByVal[/COLOR] sender [Color=Blue]As[/COLOR] System.Object, [Color=Blue]ByVal[/COLOR] e [Color=Blue]As[/COLOR] System.EventArgs) [Color=Blue]Handles[/COLOR] [Color=blue]MyBase[/color].Load
    4.         [Color=Blue]Dim[/COLOR] tb [Color=Blue]As[/COLOR] Control
    5.         [Color=Blue]For[/COLOR] [Color=Blue]Each[/COLOR] tb [Color=Blue]In[/COLOR] Controls
    6.         [Color=Blue]If[/COLOR] [Color=Blue]TypeOf[/COLOR] tb [Color=Blue]Is[/COLOR] TextBox [Color=Blue]Then
    7. [/COLOR]                [Color=Blue]AddHandler[/COLOR] tb.TextChanged, [Color=Blue]AddressOf[/COLOR] txtBox_TextChanged
    8.             [Color=Blue]End[/COLOR] [Color=Blue]If[/COLOR]
    9.         [Color=Blue]Next
    10. [/COLOR]    [Color=Blue]End[/COLOR] [Color=Blue]Sub
    11.  
    12. [/COLOR]    [Color=Blue]Private[/COLOR] [Color=Blue]Sub[/COLOR] txtBox_TextChanged([Color=Blue]ByVal[/COLOR] sender [Color=Blue]As[/COLOR] [Color=Blue]Object[/COLOR], [Color=Blue]ByVal[/COLOR] e [Color=Blue]As[/COLOR] System.EventArgs) [Color=Blue]Handles[/COLOR] txtBox.TextChanged
    13.         [Color=Green]'///[/COLOR] [Color=Green]all[/COLOR] [Color=Green]textboxes[/COLOR] [Color=Green]on[/COLOR] [Color=Green]this[/COLOR] [Color=Green]form[/COLOR] [Color=Green]now[/COLOR] [Color=Green]have[/COLOR] [Color=Green]the[/COLOR] [Color=Green]TextChanged[/COLOR] [Color=Green]event[/COLOR] [Color=Green]handled[/COLOR] [Color=Green]here.
    14. [/COLOR]    [Color=Blue]End[/COLOR] [Color=Blue]Sub[/COLOR]
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  6. #6

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075
    do I need to name each textbox here?

    VB Code:
    1. AddressOf txtBox_TextChanged

    where do I set the btnSave.enabled = true?

    in each txtbox change event? doesn't that defeat the purpose of creating a handler?
    It's tough being an unhandled exception...

    ___________
    VB.NET 2008
    VB.NET 2010
    ORACLE 11g
    CRYSTAL 11

  7. #7
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227
    Code:
    For Each tb In Controls
            If TypeOf tb Is TextBox Then
                    AddHandler tb.TextChanged, AddressOf (any event handler )
                End If 
            Next

    no you dont need the name, when using the code above (as dynamic... wrote )

  8. #8

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075
    in which event would the code go into?
    It's tough being an unhandled exception...

    ___________
    VB.NET 2008
    VB.NET 2010
    ORACLE 11g
    CRYSTAL 11

  9. #9
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227
    sorry didnt see the all your questions

    where do I set the btnSave.enabled = true ?
    in the event handler
    in case your using dynamic_sysop sample it would look like this
    Code:
    Private Sub txtBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBox.TextChanged
        btnSave.Enabled=True
        End Sub



    in each txtbox change event? doesn't that defeat the purpose of creating a handler?
    didnt really understand what your asking, but here you just have 1 handler for all the text boxes

  10. #10

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075
    I meant I have 20 textboxes on my screen , so I would have to name each handler (textbox) here?


    VB Code:
    1. AddressOf txtBox_TextChanged
    It's tough being an unhandled exception...

    ___________
    VB.NET 2008
    VB.NET 2010
    ORACLE 11g
    CRYSTAL 11

  11. #11
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227
    this is all you need, you dont have to add or remove anything ( you dont need the textbox names )

    Code:
       Private WithEvents txtBox As TextBox
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim tb As Control
            For Each tb In Controls
            If TypeOf tb Is TextBox Then
                    AddHandler tb.TextChanged, AddressOf txtBox_TextChanged
                End If 
            Next
        End Sub
    
        Private Sub txtBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBox.TextChanged
            BtnSave.Enabled=True
        End Sub

  12. #12

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075
    still not woeking...I have my textboxes in a groupbox...could this be why it's not being read as a control?
    It's tough being an unhandled exception...

    ___________
    VB.NET 2008
    VB.NET 2010
    ORACLE 11g
    CRYSTAL 11

  13. #13
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227
    try this,
    Code:
     For Each txt In GroupBox1.Controls
    istead of
    Code:
    For Each tb In Controls

  14. #14

    Thread Starter
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075
    thanks persian, that did it...
    It's tough being an unhandled exception...

    ___________
    VB.NET 2008
    VB.NET 2010
    ORACLE 11g
    CRYSTAL 11

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