Link to home
Start Free TrialLog in
Avatar of H-SC
H-SCFlag for United States of America

asked on

Enable/Disable Button Based On Empty TextBoxes

I have a form that has a button on it with 2 textboxes.  Here is what I need to happen.

I need for the button to be disabled if both textboxes are empty.  If the user clears out one and one still has data then still disabled.  

Only enable when both boxes have data
ASKER CERTIFIED SOLUTION
Avatar of TSmooth
TSmooth

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jorge Paulino
Try this and disable the command1 (command1.enable = false):
Private Sub Text1_Change()
  If Text1.Text <> "" And Text2.Text <> "" Then
    Command1.Enable = True
  Else
    Command1.Enable = False
  End If
End Sub
 
Private Sub Text2_Change()
  If Text1.Text <> "" And Text2.Text <> "" Then
    Command1.Enable = True
  Else
    Command1.Enable = False
  End If
End Sub

Open in new window

Opps, is it in .NET ???

My example is in VB6
Avatar of pavaneeshkumar
pavaneeshkumar

Create a function like this as given below

and put in following events

textBox1_TextChanged

textBox2_TextChanged

Form1_Load


private void checkdata()
 
{
 
if (textBox1.Text.Trim().Equals("") || textBox2.Text.Trim().Equals(""))
  button1.Enabled = false;
            
else                
   button1.Enabled = true;
 
}

Open in new window

Here is one way of doing it

  Private Sub TextChange(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged
        If Me.TextBox1.Text.Length = 0 Or Me.TextBox2.Text.Length = 0 Then
            Me.Button1.Enabled = False
        Else
            Me.Button1.Enabled = True
        End If
    End Sub
for vb6 only code will change to as below

don't forget toput in following events

textBox1_TextChanged

textBox2_TextChanged

Form1_Load
private Sub checkdata
	if(Len(Trim(textbox1.Text))==0||Len(Trim(textbox2.Text))==0)
		button1.Enabled = false;
            
	else
                
		button1.Enabled = true;
 
	Endif
end sub

Open in new window

Avatar of H-SC

ASKER

TSmooth,

That worked great!