simple vb problem kicking my butt...

7 replies
I have been fighting this problem for about 3 hours now and nothing I do seems to work. I am working on a small browser program that I am putting a small error handler type issue together.

What I want to do is have a button become enabled when there is text in a text box on the screen and disabled when there is no text in the box. The result will be when a url is in the text box the button is clickable and the web browser takes the url and if there is no url in the text box nothing will happen sice they can't click the button.

This is the code I have been trying to play with.

If UrlTextBox.Text = Text Then Button4.Enabled = True else button4.enabled = false

but when I run the program the button is still enabled. I have even tried simular lines of code and putting the code in different declerations but nothing seems to work. This isn't difficult or at least I don't think it is. Can anyone give me any advice.

Thanks
#butt #kicking #problem #simple
  • Profile picture of the author n7 Studios
    Firstly, you'll need the code to be within the textbox's onChange function (or VB similar function - I can't remember from memory).

    Then, you'd need to use something along the lines of:
    If UrlTextBox.Text = "" Then Button4.Enabled = False Else Button4.Enabled = True
    {{ DiscussionBoard.errors[1350802].message }}
  • Profile picture of the author NicheExposed
    thank youI did that before but may have put in the wrong place.
    Signature

    Join our FREE Masterminds as well as enroll in our online courses! -- Mastermind and Courses

    {{ DiscussionBoard.errors[1351033].message }}
    • Profile picture of the author YMO
      hi.Niche, you could use the code earlier suggested or better still make use of the keypress event of your textbox.You can place this code in the keypress or change events:

      If Text1.Text <> "" Then
      Command1.Enabled = True
      Else
      Command1.Enabled = False
      Exit Sub
      End If

      YMO
      {{ DiscussionBoard.errors[1353445].message }}
  • Profile picture of the author sanjid112
    Code:
         'This checks if the length of a string without leading/trailing spaces is greater than 0
        If Len(Trim$(Text1.Text)) > 0 Then
            'You might want to check for valid entries here, but this line below enables it
            Command1.Enabled = True
        Else
            Command1.Enabled = False
        End If
    End Sub
    maybe that help.

    -Malik
    {{ DiscussionBoard.errors[1353499].message }}
    • Profile picture of the author VisualWebEffects
      try this:

      Code:
      Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
           If (TextBox1.Length <= 0) then
                Button1.Enabled = False
           else
                Button1.Endabled = True
           End If
      End Sub
      Basically the Code that sanjid112 posted but instead of using the len function use the length function built into the textbox and wrap it in the TextChanged function. Doing this the TextChanged function should automatically be called when the user starts to type text. All you would need to do is change the compair number to whatever you need it to be. And ofc change the TextBox1 to whatever your textbox is named.
      Signature
      VisualWebEffects- Web Application Development, PC Software Development and Identity Design services
      {{ DiscussionBoard.errors[1354063].message }}
      • Profile picture of the author TrafficMystic
        Originally Posted by VisualWebEffects View Post

        try this:

        Code:
        Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
             If (TextBox1.Length <= 0) then
                  Button1.Enabled = False
             else
                  Button1.Endabled = True
             End If
        End Sub
        Basically the Code that sanjid112 posted but instead of using the len function use the length function built into the textbox and wrap it in the TextChanged function. Doing this the TextChanged function should automatically be called when the user starts to type text. All you would need to do is change the compair number to whatever you need it to be. And ofc change the TextBox1 to whatever your textbox is named.
        Also add some code on the show or load event to check the values on startup of the form.
        {{ DiscussionBoard.errors[1355462].message }}
  • Profile picture of the author warmlikecoffee
    Insert this into the TextChanged part of Textbox:

    If Len(Textbox1.Text) = 0 Then
    Command1.Enabled = False
    Else
    Command1.Enabled = True
    End If

    If your Textbox is empty default on startup then just set Enabled = False on the command button.

    Edit:
    Next time ask for help sooner, 3 hours is a long time.. =)
    {{ DiscussionBoard.errors[1355645].message }}

Trending Topics