How to enable a button that is disabled based on textBox? c# - c#

I got a textBox that load value from my database and a button that update changes based on the value of the textBox. What I need is to enabled the button if the textBox value changed. For example, the value that the textBox loads is 3 if I also input again 3 in the textBox the button will still be disable. The button will only enabled if I changed the value for example to 4 or any number but not 3.

Cache the original value somewhere then compare in the TextChanged Event
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == OriginalValue)
{
button1.Enabled = false;
}
else
{
button1.Enabled = true;
}
}
Alternatively, you could just do this (see CodesInChaos' comment below):
private void textBox1_TextChanged(object sender, EventArgs e)
{
button1.Enabled = textBox1.Text != OriginalValue;
}

Related

Create a login form with enable/disable button

I learned how to have a textbox and when the value is empty the button is disabled and when I enter a value in the textbox the button is enabled.
Now I want to have a login form that contains one textbox (for username) and another textbox (for password), so here I learned how to code.
But how should I write the code so that when the condition (both text boxes are empty) the button is disabled and when the condition is (both text boxes have values) the button is enabled.
Try this:
private void txtUserName_TextChanged(object sender, EventArgs e)
{
CheckFields();
}
private void txtPassword_TextChanged(object sender, EventArgs e)
{
CheckFields();
}
private void CheckFields()
{
btnLogin.Enabled = txtPassword.Text.Length == 0 || txtUserName.Text.Length == 0 ? false : true;
}
I assume that your username textbox is named, txtUserName, and your password textbox is named, txtPassword. Also the login button is named, btnLogin. I recommend setting the btnLogin enabled property to false when the form first loads. You can set that in the form's Load event:
private void Form1_Load(object sender, EventArgs e)
{
btnLogin.Enabled = false;
}
I think it would be better if you implement this logic on the client side (javascript), it is unnecessary to go to the server again and again for every text change.
you should add onclick function on username and password textboxes and implement the logic to check whether both have values then enable the button else disable it.
You should add the TextChanged event for both boxes and have it be something like this:
private void CheckTextboxes(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtUsername.Text) || string.IsNullOrWhiteSpace(txtPassword.Text))
{
button.Enabled = false;
return;
}
button.Enabled = true;
}
private void txtUsername_TextChanged(object sender, EventArgs e)
{
CheckTextboxes();
}
private void txtPassword_TextChanged(object sender, EventArgs e)
{
CheckTextboxes();
}
That way you ensure that if the user enters values on the textboxes in either order the button only enables if both have actual text written on them.

C# WinForms toggle button "enabled" property based on listView.selectedItems.count

I'd like to be able to have a button ONLY enabled when a certain listview has a selected item... such as listView1.SelectedItems.Count > 0
I can enable a button once a listViewItem is selected... but I can't figure out how to UNenable once the user clicks away from the listView.
Is there any "ListViewItem DeActivate" function? I've looked around but can't find anything.
You can do this in the SelectedIndexChanged event ...
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
button1.Enabled = listView1.SelectedItems.Count > 0;
}
use this code i am writing this code by supposing that in list at 0 index you have nothing to select or it has --select-- value
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listView1.SelectedItems.Count>0)
{
//this code will disable the button if it has any selection
button1.Enabled =false;
}
if(listView1.SelectedItems.Count==0)
{
//this code will enable the button if it has any selection
button1.Enabled =true;
}
}
Look into the Lostfocus event and then try something like this.
private void Lost_Focus_Ev(object sender, RoutedEventArgs e)
{
My_button.IsEnabled = false;
}
Everytime a user selects another control the button will be disabled. You can renable the button when the listview is re selected.

How to validate textbox in C# WF?

I have two text boxes in windows form.
Also one disabled button.
How I can do validation text box:
if field is empty then disable button
if value inside field is less then 5 then disable button
other case - enable button
I tried this on event TextChange, but when I tried to enter value 43 I get notification, because event TextChange works after each typing symbols.
Code:
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(textBox2.Text))
{
button6.Enabled = true;
}
}
If you don't want to validate each time a key is pressed but would rather validate when the user leaves the field, instead of hooking into the TextChanged event, hook into the Leave event.
private void textBox2_Leave(object sender, EventArgs e)
{
button6.Enabled = !(string.IsNullOrEmpty(textBox2.Text)) && textBox2.Text.Length >= 5;
if (!button6.Enabled)
{
textBox2.Focus();
}
}
Update your event handle like this :
private void textBox2_TextChanged(object sender, EventArgs e)
{
button6.Enabled =
!String.IsNullOrEmpty(textBox2.Text) && textBox2.Text.Length > 5
}
As for disabling the button on start up, you just set button6 to be disabled by default.
Or, invoke your validation in your constructor :
textBox2_TextChanged(null, null);
Neither TextChanged nor Leave events are appropriate for this. The proper event is called (surprise:-) Validating. You need to set e.Cancel = true if validation is wrong. More info: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating(v=vs.110).aspx

Preserve control's visibility on condition

This is probably an easy one for some of you.
I have a TextBox and a ListBox. ListBox provides options for the TextBox and copies selected item's text to TextBox on DoubleClick event. ListBox becomes visible only when TextBox fires Enter event. I do not want to discuss my reasons for selecting this control combination.
I want ListBox to disappear when any other control within the Form gets focus. So I capture Leave event of TextBox and call ListBox.Visible = fale The problem is that TextBox will also loose focus when I click on ListBox to select provided option thus preventing me from selecting that option.
What event combination should I use to preserve ListBox to select option but hide it whenever other controls get focus?
In the Leave method, you can check to see if the ListBox is the focused control or not before changing its Visibility:
private void myTextBox_Leave(object sender, EventArgs e)
{
if (!myListBox.Focused)
{
myListBox.Visible = false;
}
}
This example will provide you with the desired outcome:
public Form1()
{
InitializeComponent();
textBox1.LostFocus += new EventHandler(textBox1_LostFocus);
textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
}
void textBox1_GotFocus(object sender, EventArgs e)
{
listBox1.Visible = true;
}
void textBox1_LostFocus(object sender, EventArgs e)
{
if(!listBox1.Focused)
listBox1.Visible = false;
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void Form1_Shown(object sender, EventArgs e)
{
//if your textbox as focus when the form shows
//this is the place to switch focus to another control
listBox1.Visible = false;
}

Tabpage control leave

I have a tab control and 3 tabpages in it. ( C#)
if i am in tab 2, and edit a text box value
and then click tab 3, i need to validate what was enetered in the text box.
if correct i should allow to to switch to tab 3 else should remain in tab 2 it self
how do i achieve this?
iam curently handling the "leave" event of the tabpage2,
i validate the text box value there and if found invalid
i set as tabcontrol.Selectedtab = tabpage2; this does
the validation but switches to new tab! how could i restrict the navigation.
I am a novice to C#, so may be i am handling a wrong event!
Here is the relevant code:
private void tabpage2_Leave(object sender, EventArgs e)
{
if (Validatetabpage2() == -1)
{
this.tabcontrol.SelectedTab =this.tabpage2;
}
}
While the other approaches may work, the Validating event is designed specifically for this.
Here's how it works. When the SelectedIndex of the tab control changes, set the focus to the newly selected page and as well as CausesValidation = true. This ensures the Validating event will called if the user tries to leave the tab in any way.
Then do your normal validation in a page specific Validating event and cancel if required.
You need to make sure to set the initial selected tab page in the Form Shown event (Form_Load will not work) and also wire up the tab page specific validating events.
Here's an example:
private void Form_Shown(object sender, System.EventArgs e)
{
// Focus on the first tab page
tabControl1.TabPages[0].Focus();
tabControl1.TabPages[0].CausesValidation = true;
tabControl1.TabPages[0].Validating += new CancelEventHandler(Page1_Validating);
tabControl1.TabPages[1].Validating += new CancelEventHandler(Page2_Validating);
}
void Page1_Validating(object sender, CancelEventArgs e)
{
if (textBox1.Text == "")
{
e.Cancel = true;
}
}
void Page2_Validating(object sender, CancelEventArgs e)
{
if (checkBox1.Checked == false)
{
e.Cancel = true;
}
}
private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
{
// Whenever the current tab page changes
tabControl1.TabPages[tabControl1.SelectedIndex].Focus();
tabControl1.TabPages[tabControl1.SelectedIndex].CausesValidation = true;
}
You can use the TabControl Selecting event to cancel switching pages. Setting e.Cancel to true in the event stops the tabcontrol from selecting a different tab.
private bool _cancelLeaving = false;
private void tabpage2_Leave(object sender, EventArgs e)
{
_cancelLeaving = Validatetabpage2() == -1;
}
private void tabcontrol_Selecting(object sender, TabControlCancelEventArgs e)
{
e.Cancel = _cancelLeaving;
_cancelLeaving = false;
}

Categories