Results 1 to 18 of 18

Thread: How to enabling a command only after text boxes are filled

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    13

    How to enabling a command only after text boxes are filled

    hello

    i want to know How to enabling a command only after text boxes are filled

    in VB6

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: How to enabling a command only after text boxes are filled

    Try this
    Code:
    Private Sub Text1_Change()
        If Text1.Text = vbNullString Then
            Command1.Enabled = False
        Else
            Command1.Enabled = True
        End If
    End Sub
    OR
    Code:
    Private Sub Text1_Change()
        Command1.Enabled = Not (Text1.Text = vbNullString)
    End Sub



  3. #3
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: How to enabling a command only after text boxes are filled

    this would work also
    Code:
    Private Sub Text1_Change()
        Command1.Enabled = Len(Text1.Text)
    End Sub

  4. #4
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: How to enabling a command only after text boxes are filled

    If there are several textboxes the you could iterate thru the form's controls (Controls Collection)
    Declare a boolean... set it to true
    Inside the loop
    Ask if it is textbox (TypeOf Is Textbox) and if it's filled... set the boolean to false if empty
    ...
    Outside of the loop
    Ask for the boolean variable
    If it is true then enable command button
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  5. #5
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: How to enabling a command only after text boxes are filled

    Ask if it is textbox (TypeOf Is Textbox)
    This is VB6 forum



  6. #6
    Frenzied Member
    Join Date
    May 2006
    Location
    some place in the cloud
    Posts
    1,886

    Re: How to enabling a command only after text boxes are filled

    TypeOf is a valid keyword in VB6

    The next piece of code changes the forecolor of all Text Box controls on Form1 to red:
    Code:
    Dim c As Control 
    
    For Each c In Form1.Controls
        If TypeOf c Is TextBox Then
            c.ForeColor = vbRed
        End If 
    Next
    You'll also find this approach useful for many other tasks, such as unchecking all of the CheckBox controls.
    JG


    ... If your problem is fixed don't forget to mark your threads as resolved using the Thread Tools menu ...

  7. #7
    Lively Member
    Join Date
    Aug 2009
    Posts
    98

    Re: How to enabling a command only after text boxes are filled

    In form load make command button disabled. In each textbox's change event check for emptiness (Len(Text...)=0) of all text boxes, if any one is empty keep Command Button disabled.

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to enabling a command only after text boxes are filled

    Along the same lines as Smartchap, you can grab the length of a series of textboxes and multiply those lengths together. If any textbox is empty, it will equate to 0 and any number multipled by 0 becomes 0 and 0 will equate to False. Example:
    Code:
    Option Explicit
    
    Private Sub EnableButton() 
    Command1.Enabled = Len(Text1.Text) _
                     * Len(Text2.Text) _
                     * Len(Text3.Text) _
                     * Len(Text4.Text) _
                     * Len(Text5.Text)
    
    End Sub
    
    Private Sub Command2_Click()
    EnableButton
    End Sub

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    13

    Re: How to enabling a command only after text boxes are filled

    hello all

    thanks for all but i want that the command button disabled at default then enabled after After writing anything on textbox

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to enabling a command only after text boxes are filled

    Quote Originally Posted by Aiman H View Post
    thanks for all but i want that the command button disabled at default then enabled after After writing anything on textbox
    There are a variety of responses to your question and all of them will do what you want. Pick one.

  11. #11

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    13

    Re: How to enabling a command only after text boxes are filled

    Quote Originally Posted by Hack View Post
    There are a variety of responses to your question and all of them will do what you want. Pick one.
    hello
    yes i've tried it but the problem is that the command button shift to disabled after writing anything on textbox and clean it

    i'm loocking for any way to disable the command button at default

    for exemple if you open my program you will see the command button disabled at default

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to enabling a command only after text boxes are filled

    Set Enabled = False in design mode, on the button's property page.

  13. #13

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    13

    Re: How to enabling a command only after text boxes are filled

    Quote Originally Posted by Hack View Post
    Set Enabled = False in design mode, on the button's property page.
    thanks so much
    now it work but there is a small mistake
    i've 3 textbox after writing anything on one textbox, the command button shift to Enabled without considering the two other textbox

    this is the codethat i've used from 4x2y:
    Code:
    Private Sub Text1_Change()
        If Text1.Text = vbNullString Then
            calculateBtn.Enabled = False
        Else
            calculateBtn.Enabled = True
        End If
    End Sub
    
    Private Sub Text2_Change()
        If Text2.Text = vbNullString Then
            calculateBtn.Enabled = False
        Else
            calculateBtn.Enabled = True
        End If
    End Sub
    
    Private Sub Text3_Change()
        If Text3.Text = vbNullString Then
            calculateBtn.Enabled = False
        Else
            calculateBtn.Enabled = True
        End If
    End Sub

  14. #14
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,263

    Re: How to enabling a command only after text boxes are filled

    Or he can do it like this:

    Code:
    Private Function EnableButton()  As Boolean
    EnableButton = Len(Text1.Text) _
                     * Len(Text2.Text) _
                     * Len(Text3.Text) _
                     * Len(Text4.Text) _
                     * Len(Text5.Text)
    
    End Function
    
    'In Every Change-Event of the particular TextBoxes:
    Private Sub Text1_Change()
        Command1.Enabled = EnableButton
    End Sub
    
    'The same in Form_Load-Event
    
    Private Sub Form_Load()
        Command1.Enabled = EnableButton
    End Sub
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  15. #15
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,263

    Re: How to enabling a command only after text boxes are filled

    Quote Originally Posted by Aiman H View Post
    thanks so much
    now it work but there is a small mistake
    i've 3 textbox after writing anything on one textbox, the command button shift to Enabled without considering the two other textbox

    this is the codethat i've used from 4x2y:
    Code:
    Private Sub Text1_Change()
        If Text1.Text = vbNullString Then
            calculateBtn.Enabled = False
        Else
            calculateBtn.Enabled = True
        End If
    End Sub
    
    Private Sub Text2_Change()
        If Text2.Text = vbNullString Then
            calculateBtn.Enabled = False
        Else
            calculateBtn.Enabled = True
        End If
    End Sub
    
    Private Sub Text3_Change()
        If Text3.Text = vbNullString Then
            calculateBtn.Enabled = False
        Else
            calculateBtn.Enabled = True
        End If
    End Sub
    To stay in your example:

    vb Code:
    1. Private Function EnableButton()  As Boolean
    2.       EnableButton = Len(Text1.Text) * Len(Text2.Text) * Len(Text3.Text)
    3. End Function
    4.  
    5. Private Sub Text1_Change()
    6.    Command1.Enabled = EnableButton
    7. End Sub
    8.  
    9. Private Sub Text2_Change()
    10.     Command1.Enabled = EnableButton
    11. End Sub
    12.  
    13. Private Sub Text3_Change()
    14.    Command1.Enabled = EnableButton
    15. End Sub
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  16. #16

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    13

    Re: How to enabling a command only after text boxes are filled

    Quote Originally Posted by Zvoni View Post
    To stay in your example:

    vb Code:
    1. Private Function EnableButton()  As Boolean
    2.       EnableButton = Len(Text1.Text) * Len(Text2.Text) * Len(Text3.Text)
    3. End Function
    4.  
    5. Private Sub Text1_Change()
    6.    Command1.Enabled = EnableButton
    7. End Sub
    8.  
    9. Private Sub Text2_Change()
    10.     Command1.Enabled = EnableButton
    11. End Sub
    12.  
    13. Private Sub Text3_Change()
    14.    Command1.Enabled = EnableButton
    15. End Sub
    thanks but the problem is after set these code and run my program
    when it write anything on testbox i get an error with 3 button " end - debug - help"

  17. #17
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: How to enabling a command only after text boxes are filled

    What is the error message?



  18. #18

    Thread Starter
    New Member
    Join Date
    Oct 2012
    Posts
    13

    Re: How to enabling a command only after text boxes are filled

    Quote Originally Posted by 4x2y View Post
    What is the error message?
    Now The Code Work Fine
    Thanks For All

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