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.
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?
Related
I'm using PromptTextBox that will show prompt text inside itself when Text is empty and IsKeyboardFocusWithin is false.
The problem is when I start new window, it automatically keyboard focus on the first TextBox so the prompt text isn't shown(but this behavior is acceptable).
If I want to un-keyboard focus I must click on another controls(e.g. click a Button, click on another TextBox), I can't click on blank space to un-keyboard focus.
I also test on normal TextBox, it's behavior is the same. so the question is:
How can I un-keyboard focus once I'd keyboard focused on TextBox by click on blank space?
In the MouseDown event handler for the 'blank space', focus on some other control, if you have any. Or else, you need an invisible/out-of-view-bounds TextBox to focus on:
<blankSpaceControl>.MouseDown += ClearFocus;
void ClearFocus(object sender, MouseEventArgs me)
{
<yourOtherFocusableControl>.Focus();
}
There is no other way. Focusing the Window itself will pass the focus on to the first focusable child control.
Just use Keyboard.ClearFocus()
https://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.clearfocus.aspx
I have a small problem with focusing in .net/wpf. I have written a custom user control which contains a TextBox and a SearchButton. The user-control has a lost focus event, which validate the content of the textbox if the focus is leaving. But now I have the problem, if I click on my search button, the lost focus event of the user control gets fired, even if I click on the button in the custom user control. (The button additionally has the option TabStop="False":
The problem is, that I don't want to fire the event if I click on the button.
Set
Focusable="False"
of your search button and the TextBox will not lose the focus because the button doesn't get the focus.
You can Do this Check in Event like this
protected void LostFocusEvent(object sender, RoutedEventArgs e)
{
if(textBox.Text.Length>0)
{
// if there is any character in TextBox
return;
}
// your validation Code goes here
}
I have a windows form application , In which i created two tab pages. In one of the tab page i have a button to send email notification.
In the tabpage leave event i have some code to perform some other actions.
When i click on this button to send email. First it fires tabpage leave event , as ithe button contains button1.enabled=false; in the first line as below,
private void btnTestEmail_Click(object sender, EventArgs e)
{
btnTestEmail.Enabled = false;
bool sent = Support.SendEmail("Test Email", "This is a test email, Please ignore.", null);
--
}
But when i remove btnTestEmail.Enabled = false; code it is not firing tabpage leave event.
What could be the reason that it fires the leave event of tab page. As it is vbery strange behaviour. As i dont want to fire any event of tab page .
Regards
Changing btnTestEmail.Enabled to false will change the ActiveControl, which fires the Leave event.
According to MSDN:
When you change the focus by using the keyboard (TAB, SHIFT+TAB, and
so on), by calling the Select or SelectNextControl methods, or by
setting the ContainerControl.ActiveControl property to the current
form, focus events occur in the following order:
Enter
GotFocus
Leave
Validating
Validated
LostFocus
What you can do:
What I would do to eliminate this behavior is unsubscribing the Leave event and re-subscribing it after setting the Enabled property to false.
Like this:
this.tabPage1.Leave -= new System.EventHandler(this.tabPage1_Leave);
btnTestEmail.Enabled = false;
this.tabPage1.Leave += new System.EventHandler(this.tabPage1_Leave);
The Problem is, that the Leave event fires if the control isnt the active control. If you click the Button the TabPage changes from active to inactive because the Button is active control now.
I get data from the data base then i show it in a datagrid (wpf) , the user can make quick search (filter) from a textbox after clicking on Enter.
I use this event to handle the button -Enter- click
(OnSearch - This event is spawn whenever the search button is clicked or the Enter key is pressed.)
the problem is when the user don't write anything, the event will not be launched even when he click on Enter-Button !
how can i proceed to make it work
public MainWindow()
{
InitializeComponent();
//m_txtTest is a SearchTextBox
m_txtTest.OnSearch += new RoutedEventHandler(m_txtTest_OnSearch);
}
void m_txtTest_OnSearch(object sender, RoutedEventArgs e)
{
//to get the entered string
SearchEventArgs searchArgs = e as SearchEventArgs;
....
....
....
....
}
So when your user types something in and presses enter it is the textbox handling the Event.
When they don't type anything though the textbox doesn't have focus and cant handle the event.
What I would do is create an event for searching on the window or grid.
Somthing like this
this.OnPreviewKeyDown += new OnPreviewKeyDownEvent;
http://msdn.microsoft.com/en-us/library/ms748948.aspx
You can think of it like this
Window has focus
->Grid has focus
-->textbox has focus
What ever the last thing that has focus (or the most inner element, think of it like a cake) is will be the thing that sees the event first. If you have that event registered for that UI element it will handle it.
The textbox isn't focused so it wont see the event
I'm having a Picture box in a user control window(Windows custom control library). and some functionality in the Form's Enter event and leave event.
Now my sample application is having two instances of the control. So when i run my sample application the fist control got selected and the enter event is triggered, and when i select the second control the first's leave and second's enter events are getting triggered.
Now, problem is that when i select(click) the second control's picturebox, the events are not triggering, i.e the control form is not getting the event.
So if i click whereever in the control(in the picturebox or in the control) the enter event should be triggered.
How to do this?
A picture box can't get focus. So clicking on it won't take the focus away from the previous control thus not triggering the events.
You need to add a click handler on the picture box in which you manually give focus to the associated focusable control.
private void PictureBox_Click(object sender, EventArgs e)
{
focusableControl.Focus();
}