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;
}
Related
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.
I've a user control with 2 labels and two textboxes. When the label is clicked the textbox's visible prop is set to true. Here's the code I've used:
private void label_Heading_Click(object sender, EventArgs e)
{
label_Heading.Visible = false;
textBox_Heading.Text = label_Heading.Text;
textBox_Heading.Visible = true;
textBox_Heading.Focus();
}
After the textbox lose focus, it's visible prop is set to false and the label is updated with the text. Code:
private void textBox_Heading_Leave(object sender, EventArgs e)
{
textBox_Heading.Visible = false;
if(textBox_Heading.Text != "")
label_Heading.Text = textBox_Heading.Text;
label_Heading.Visible = true;
}
The code to create user controls on click:
private void label1_Click(object sender, EventArgs e)
{
TaskCard _taskCard = new TaskCard(++TOTAL_ITEM_COUNT, PanelName);
panel_DeletedItem.Controls.Add(_taskCard);
panel_DeletedItem.Refresh();
}
These code work fine when a single user control of this type is added to a panel. But if I add more than one, the code works only for the first user control, but it wont work for the new ones, although the event is fired for every user control. What am I missing here? Please suggest.
If I add a mbox to this code, the mbox is displayed for any control, but the rest of the code won't work, except for the first one.
private void label_Heading_Click(object sender, EventArgs e)
{
MessageBox.Show("Test"); // this will display, but the rest of the code is not executed or changes are not visible, i.e., the teboxes are not displayed even if I click the labels
label_Heading.Visible = false;
textBox_Heading.Text = label_Heading.Text;
textBox_Heading.Visible = true;
textBox_Heading.Focus();
}
i need to hide a ListBox when I focus out a textbox. if i click on a different control or use Tab key then the textbox's "Leave" event occurs. But if I click inside the form, on any free space, then focusout doesn't happen. i saw something called mouse capture but i cant implement it.
i tried this:
private void txtProduct_Enter(object sender, EventArgs e)
{
listProduct.Show();
UIElement el = (UIElement)sender;
el.CaptureMouse();
}
private void MouseClickedElseWhere(object sender, MouseEventArgs e)
{
if (e.Clicks >= 1)
{
txtProduct_Leave(sender, new EventArgs());
}
}
private void txtProduct_Leave(object sender, EventArgs e)
{
listProduct.Hide();
}
but obviously it shows error. how do i achieve this? any help?
I had to make click event for my groupboxes even if groupbox doesnt have a click event by default.
//my_page.designer.cs
this.groupBox2.Click += new System.EventHandler(this.groupBox2_clicked);
//my_page.cs
private void groupBox2_clicked(object sender, EventArgs e)
{
listProduct.Hide();
}
I have made 2 user controls. First one contains a textbox and a button. In the second one there is a panel and a repeater control is used. Now when I am writing in the textbox 2nd control will open as a popup and and after focusing to textbox I am unable to write. I have searched a lot about this but nothing works.
CustomPopup customPopup;
Popup popup;
popup = new Popup(customPopup = new CustomPopup());
private void txtSearch_TextChanged(object sender, EventArgs e)
{
popup.Width = Width;
popup.Show(this);
popup.AutoClose = false;
}
In this way I am opening the popup from the textbox text changed event.
You can use this textbox event.
private void textBox1_Enter(object sender, EventArgs e)
{
}
this event call whenever textbox get focus
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;
}