Validate a textbox in a windows form - c#

Windows form application(.Net 3.5) I have a textbox and a button on the form.
I want to disable the button once the textbox is empty.
I don't want to use this method. Because the button is still enabled.
Thanks.

In the event handler for TextChanged, simply determine if the text box contains any data. If it does, enable it. Otherwise, disable it. Add your event handler and then implement something like the following,
private void textBox1_TextChanged(object sender, EventArgs e)
{
button1.Enabled = !(textBox1.Text == String.Empty);
}

Handle the on text change event
check and see what textbox.text is like this
if(string.IsNullOrEmpty(textbox1.text))
{
Button1.enabled = false;
}

if you want to disable the textbox, then using textChanged:
if (textbox.Text == ""){
button.Enabled = false;
}
Hope it helps

Make button1.Enabled = false;
and add EventHandler to textbox1.TextChanged = new System.EventHandler(SearchBoxTextChanged);
private void textbox1_TextChanged(object sender, EventArgs e)
{
button1.Enabled = (textBox1.Text.Trim() != string.Empty);
}

Related

C# richBox1 text disable

I have problem with richBox1 text disabling.
I've tryed richTextBox1.readonly = true; and richTextBox1.Enabled = false;
My code:
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
richTextBox1.ReadOnly = !richTextBox1.ReadOnly;
}
Its disabling after one letter.
EDIT: And if disable I can still copy text but cant write there.
Honestly, disabling expected functionality is not something you should be doing. It is not good UI design.
The event TextChanged is fired every time the text changes (including writing or removing one letter). You can use Form's Load event (by double clicking the form on design time) :
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.ReadOnly = true;
richTextBox1.Enabled = false;
}

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