Windows Develop Bookmark and Share   
 index > Windows Forms General > Enabling a button after text input
 

Enabling a button after text input

Hey all,

I'm trying to setup an event were after inputing info into text boxes, a button will become enabled. I've tried using the leave event for each textbox and creating a counter data member to hold a value, and then adding 1 to the counter after each leave event. So for example:
private void tbID_Leave(object sender, EventArgs e)
{
counter = counter + 1;
}

then i tried a bool to make btn.enabled work. What am I mising, or am I on the wrong track? Thank you for any help
dpotter  Wednesday, March 05, 2008 4:55 AM
Hi dpotter,

You can use a Validate function like

private void Validate()
{
// where txtBox1, txtBox2, txtBox3
// refers to your textboxes
// if any of them is empty return false
if (txtBox1.Text==String.Empty !! txtBox2.Text==String.Empty !! txtBox3.Text==String.Empty !!)
{
btnYourButton.Enabled = false;
}
btnYourButton.Enabled = true;
}

You have to fire this in a common event of your textboxes' KeyUp event.

But if you ask me, make your button enable and use this function at your
button's click event like:

private boolean Validate()
{
// where txtBox1, txtBox2, txtBox3
// refers to your textboxes
// if any of them is empty return false
if (txtBox1.Text==String.Empty !! txtBox2.Text==String.Empty !! txtBox3.Text==String.Empty !!)
{
return false;
}
return true;
}

and in click event you can use like this:

// your click event
private void btnYourButton_OnClick(....)
{
if(Validate())
{
// Do your procedures
}
else
{
MessageBox.Show("Fill all the required fields");
}
}
Because like this you are firing a event only one time, otherwise
every key up fires an event and it is not good..

Kind Regards..
Sqnyy  Wednesday, March 05, 2008 6:20 AM
Hi dpotter,

You can use a Validate function like

private void Validate()
{
// where txtBox1, txtBox2, txtBox3
// refers to your textboxes
// if any of them is empty return false
if (txtBox1.Text==String.Empty !! txtBox2.Text==String.Empty !! txtBox3.Text==String.Empty !!)
{
btnYourButton.Enabled = false;
}
btnYourButton.Enabled = true;
}

You have to fire this in a common event of your textboxes' KeyUp event.

But if you ask me, make your button enable and use this function at your
button's click event like:

private boolean Validate()
{
// where txtBox1, txtBox2, txtBox3
// refers to your textboxes
// if any of them is empty return false
if (txtBox1.Text==String.Empty !! txtBox2.Text==String.Empty !! txtBox3.Text==String.Empty !!)
{
return false;
}
return true;
}

and in click event you can use like this:

// your click event
private void btnYourButton_OnClick(....)
{
if(Validate())
{
// Do your procedures
}
else
{
MessageBox.Show("Fill all the required fields");
}
}
Because like this you are firing a event only one time, otherwise
every key up fires an event and it is not good..

Kind Regards..
Sqnyy  Wednesday, March 05, 2008 6:20 AM

You can use google to search for other answers

Custom Search

More Threads

• Listview Control - "Details" View question
• PropertyGrid and Property Editors
• publish event / subscribe
• getasynckeystate() example
• Restricting properties in Intellisense in an inherited form
• Converting a windows form (and other) object(s)?
• e.Handled for Mouse Click?
• file open error with excel
• Form1.h gets modified
• Interacting with other windows/apps?