?
Solved

Enable/Disable Button Based On Empty TextBoxes

Posted on 2007-11-16
7
Medium Priority
?
1,365 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 16

Accepted Solution

by:
TSmooth earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

770 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