Solved

Enable/Disable Button Based On Empty TextBoxes

Posted on 2007-11-16
7
1,264 Views
Last Modified: 2008-02-07
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
0
Comment
Question by:H-SC
7 Comments
 
LVL 16

Accepted Solution

by:
TSmooth earned 500 total points
ID: 20300156
You need to add code to the textchanged event of both text boxes. This code simply needs be like the code below. Then make the button disabled by default if the text boxes always start out blank or force that code to run once at the forms load if the text boxes can be one way or the other.

Keep in mind that the TextChanged event does not fire if you programmatically change the text so if you do that, you must manually call the code below.




If String.IsNullOrEmpty(Textbox1.Text) Or String.IsNullOrEmpty(Textbox2.Text) Then
  Button1.Enabled = False
Else
  Button1.Enabled = True
End If

Open in new window

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 20300167
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

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 20300173
Opps, is it in .NET ???

My example is in VB6
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:pavaneeshkumar
ID: 20300209
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

0
 
LVL 27

Expert Comment

by:planocz
ID: 20300258
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
0
 
LVL 5

Expert Comment

by:pavaneeshkumar
ID: 20300261
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

0
 
LVL 1

Author Comment

by:H-SC
ID: 20301169
TSmooth,

That worked great!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question