Reputation: 1857
How to stop textbox leave event on clicking on linklable/button while Textbox was in focus?
I have a textbox TextBox1
. On leave event of it, I have to validate it's text. And as per text of it, I have to populate suggestion of next textbox TextBox2
.
so in this process, when I'm clicking on a link-label [which is there for different reason] while TextBox1
is in focus, It is firing Leave event [which is obvious] and calling validating functionality [which is not supposed to be called - because user have not left textbox after completing input- It was triggered by linkable click].
I have tried to unsubscribe Leave event on link-label's click event but that doesn't help as leave event is being fired in first place.
what should be done in this case ?
EDIT : posting related code,
I'm Having same Event Handler for both textbox
private void txtBox_Leave(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
textBox.Visible = false;
#region TextBox 1
if (textBox.Equals(txtBox1))
{
//Text Box 1 validation
//Populating Suggestions for TextBox2
//Passing focus on Textbox 2
}
#endregion
#region TextBox 2
else if (textBox.Equals(txtBox2))
{
//Text Box 2 validation
}
#endregion
}
As I've mentioned earlier, Link label is there for different and I cannot disable or hide it during operation on Text box.
One observation is, When I click on Link label, Leave event of text box raise first and after that Click Event of Link Label, so we cannot perform any logic (like unsubscribing leave event of Textbox or something else) on Link-Label's click event - as by the time validation process would be done which we don't want.
Upvotes: 1
Views: 2295
Reputation: 1277
When you click on link label
, it becomes focused control. What you can do is - find the focused control of the form in textbox1_leave
event handler. If it is not that link label
then only the functionality for populating suggestions of textbox2
should be done.
This link should be helpful for you to find out focused control of the form - What is the preferred way to find focused control in WinForms app?
Upvotes: 1