Solved

Enable/Disable Button Based On Empty TextBoxes

Posted on 2007-11-16
7
1,302 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 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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