Solved

Enable/Disable Button Based On Empty TextBoxes

Posted on 2007-11-16
7
1,280 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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