simple vb problem kicking my butt...

by 7 replies
8
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.





#programming #butt #kicking #problem #simple
  • 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
  • thank youI did that before but may have put in the wrong place.
    • [1] reply
    • 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
  • 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
    • [1] reply
    • 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.
      • [1] reply
  • 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.. =)

Next Topics on Trending Feed

  • 8

    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.