WPF Lost-Focus issue in same control - c#

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
}

Related

Preventing a TextBox leave event when loosing focus

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?

How to un-keyboard focus on TextBox

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

how to launch event from a textbox even when the field is empty c# wpf

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

How to bypass the enter/leave event in c sharp

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();
}

Message box causes loss of focus

I have created a tool bar which has three controls. First one being a text box , an OK button and a Clear button. Essentially I am using this toolbar to search some text. When there are no results found , I pop up a message box informing the user that no results were found. But when the user clicks "OK" button of the message box, the text box looses focus and the focus passes to the next control which is the "OK" button. What should I do to avoid the text box to loose focus. I am using c#.
You can't. Clicking on the Ok button forces it to have control (and therefore the textbox loses control).
You can, however, do this on your click event:
MessageBox.Show("asdf");
textBox1.Focus();
EDIT
In response to your comment, I don't think that there's an easy way to return focus to the last control once another control has received focus, and the search and clear buttons will have to receive focus when clicked. You can do this:
private Control _last;
private void textBox1_Leave(object sender, EventArgs e)
{
_last = (Control) sender;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("asdf");
_last.Focus();
}
It's not very clear your question, but you can make your control take the focus like this:
textBox1.Focus();

Categories