1

As I'm putting the finishing touches on my program I'm having some troubles. Theres several user inputs and a submit button, once the inputs has been filled I wish to enable the submit button, else the button should be disabled. This is what I have:

 Private Sub ButtonControl(sender As System.Object, e As System.EventArgs) Handles Input1.Validated
    If Input1.Text = "" Then
        ButtonSubmit.Enabled = False
    ElseIf Input1.Text <> "" Then
        ButtonSubmit.Enabled = True
    End If
End Sub

The thing is it disables nomatter what and then it doesnt enable when my input is filed

0

5 Answers 5

2

Your code will work if you have another control that can receive the focus. Control Validation occurs on the loss of focus. If you need to have just one focusable item active you will need to use either KeyPress, KeyDown or Textchanged events to enable your button, also make sure that the CausesValidation property of your TextBox is true.

I would also make the method more generic so you could call it from multiple textbox's by using the sender object to access the textbox that raised the event. Also if you have a True/False condition you only need to do the comparison in the first if statement and then you just use an else not an elseif.

for example:

Private Sub ButtonControl(sender As System.Object, e As System.EventArgs) Handles Input1.Validated
    If DirectCast(sender, TextBox).Text = "" Then
        ButtonSubmit.Enabled = False
    Else
        ButtonSubmit.Enabled = True
    End If
End Sub

You can also use the String.IsNullOrWhiteSpace Method to check if just spaces have been entered if you are using the 4.0 framework or above. Like this TextChanged EventHandler.

Private Sub ButtonControl(sender As Object, e As EventArgs) Handles Input1.TextChanged
    If String.IsNullOrWhiteSpace(DirectCast(sender, TextBox).Text) Then
        ButtonSubmit.Enabled = False
    Else
        ButtonSubmit.Enabled = True
    End If
End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a bunch, it's like 1 texbox 3 radio groups, 1 and maybe 5-10 dropdownlists ;) I should just apply your example with AND operator correct?
This should work as is, without needing to use an AND statement. for your additional items you will need to use the CheckedShanged Event for the RadioButton and SelectedIndexChanged or SelectedValueChanged events for the ListBox
Looks like CheckedChanged doesnt work for me on radios, and i dont neeed selectedindex/valuechanged on the listboxes
1

You are going to need to use the TextBox "TextChanged" event, and be sure to set each Textbox AutoPostback="True". You can use an UpdatePanel to make the postbacks that occur on each Textbox you wish to validate less obnoxious to your end-user.

So, your textbox (if you have many, make sure they all have the OnTextChanged="ValidateForm":

<asp:TextBox ID="Input1" runat="server" OnTextChanged="Validate_TextChanged" />

Inside your textchanged ("ValidateForm") event (which each Textbox is attached to), one quick to implement route to do would just be

 ' Validation inside this event
Protected Sub Validate_TextChanged(sender As Object, e As EventArgs)

     if Input1.text <> "" AndAlso Input2.text <> "" AndAlso ' ...etc.

End Sub

If you go the route of the UpdatePanel, you may find this useful.

3 Comments

I will try and see if I can apply this tomorrow, was a bit more advanced than what I hope ;P. Thanks for the input mate
Certainly, I would try to do it without the UpdatePanel first, because it's more tricky to do so. Once you have it working, then you can try and use an UpdatePanel if you desire. Reference this link if you're having any issues. :-)
yes this is vb.net :p or do you need asp to solve this, would be strange :o
1

This is the kind of thing I would do:

Private Sub TextBoxes_TextChanged( _
        ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles _
            TextBox1.TextChanged, _
            TextBox2.TextChanged, _
            TextBox3.TextChanged

    Dim textBoxes = { TextBox1, TextBox2, TextBox3 }
    Button1.Enabled = textBoxes.All(Function (tb) tb.Text <> "")
End Sub

You can then add as many text boxes in to the textBoxes array as you need to check. Just make sure that the text boxes to the Handles list.

3 Comments

would this be possible to include with dropdownlists and radios aswell :)? Like this example alot very clean and simple
I love this after testing all the soloutions going to try and add radio's and stuff :) will report back.
@StigRexRørkær - The easiest way to add different types is to change the textBoxes array into a Func(Of Boolean) array. Then you can call it like this: Button1.Enabled = funcs.All(Function (f) f.Invoke()).
0
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged

        If TextBox1.Text.Length > 0 Then
            Me.Button1.Enabled = True
        Else
            Me.Button1.Enabled = False
        End If


    End Sub

Comments

-1

Do that code in the Input1_TextChanged event

This is what I did and worked:

Dim CheckInput1 As Boolean

Private Sub Input1_TextChanged(sender As Object, e As EventArgs) Handles Input1.TextChanged, Input1.Enter
    CheckInput1 = True
    If Input1.Text = "" Then
        CheckInput1 = False
    End If

    If CheckInput1 = False Then
        ButtonSubmit.Enabled = False
    ElseIf CheckInput1 = True Then
        ButtonSubmit.Enabled = True
    End If
End Sub

There must be a more efficient code that this but I think this solves your problem.

3 Comments

I don't understand the down vote. This code works perfectly fine.
me neither this actually looks like the simplest and easiest way to apply stuff. My only problem with it was the button is enabled in the start but fixed that quickly :)
I think the downvote would be because your answer introduces an unnecessary field and an unnecessary if statement. It doesn't improve on the OP's code and decreases the maintainability and readability of the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.