Solved

Enable/Disable Button Based On Empty TextBoxes

Posted on 2007-11-16
7
1,232 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
Comment Utility
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
Comment Utility
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
Comment Utility
Opps, is it in .NET ???

My example is in VB6
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Expert Comment

by:pavaneeshkumar
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
TSmooth,

That worked great!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now