Close Popup when focus lost - c#

I have a popup containing a ListView. The ListView contains customer names. The popup is openen when a search bar is clicked. The user can enter text in the search bar (TextBox) and the Listview is filterd based on the input.
I want to close the popup whenever it loses focus. However, the default "auto close" behaviour StaysOpen="False" is no good, because it closes the popup everytime someone clicks on the search bar.
How can I close the Popup always when it loses focus, except when the focus goes to the search bar?

Maybe you can put some hooks on the search text box. When it receives focus, it can open the popup and set StaysOpen = true. When the textbox loses focus, it can set StaysOpen = false on the popup.
XAML Code:
<Popup x:Name="pop" StaysOpen="False">

Add an event handler to the Leave event (called when focus on the control is lost). In this event handler, you can then check to see if the new item that has focus is the search text box.
if(FormName.ActiveForm.ActiveControl == txtSearchBox)
Then set StaysOpen appropriately based on whether or not the search textbox has focus.

How about:
Forward the focus-lost(Leave, occurs when the control is no longer the active control of the form) event of popup to the parent form
Parent form would, do nothing, if the current focus is on the search bar; else, it would close the the popup.

Related

Wpf prevent Popup window from removing the focus of an Textbox

In my WPF-Application I use a small window to display some notifications, but when the notificationwindow shows up, the focus in the mainwindow gets lost.
For example when I'm writing in a TextBox and I'm receiving a notification (the notification window shows up), the textbox loses the focus and I have to click in the textbox again to continue my Work.
So how can I keep the focus of the mainwindow, when the notificationwindow shows up? I already tried to set Focusable="false" in the notificationwindow
It's not really a case of how can I stop some control from becoming focused?, rather how can I focus my control after it has lost focus? Now, as # AdrianFaciu mentioned, it's rather difficult to go into details when you have omitted all relevant information from your question. One option would be to set focus to your control whenever it loses focus:
private void OnControlLostFocus(object sender, RoutedEventArgs e)
{
YourControlType control = sender as YourControlType;
if (control != null) control.Focus();
}
There is a ShowActivated property on Window object so you can keep the main window activated after you show a child window.
From MSDN:
When a window with its ShowActivated property set to false is opened, the window is not activated and its Activated event is not raised until a user manually activates the window by selecting it.

How can I give focus to any Control after a Tab has been selected

I have a problem .. I have an error list form (works as validation summary screen) that displays validation of controls that require to save data but have no values.
This form opened when validation occurs on controls in another form that has tab control contains all controls that have validation.
The problem is when I double click on Error List form, I need cursor focus on tab control that have this control and focus on the control itself
The result : focus happened on tab control only .. but I need to focus on the control also
Use Control.Focus() in your tab selected event handler.
Call Focus() to focus on the next control.
Step 1 : You need to handle the Enter event of the TabPage Control to perform the operations when TabPage gains the focus.
Step 2: You can call Select() function on Required control to gain the Focus.
Try This: if you want to gain the Focus of TextBox control in TabPage2 use this code
tabPage2.Enter += new System.EventHandler(this.tabPage2_Enter);
private void tabPage2_Enter(object sender, EventArgs e)
{
textBox1.Select();
}
I think the trick is to set socus on the tab page first, then set focus on the actual control you want to focus on.
What I was seeing is if the tab page was already selected setting focus to the control works fine. However, if the tab was programmatically activated then setting focus on the control alone does not work.
So this works for me reliably:
// first select and focus the tab
TabsResult.SelectedTab = tabRequest;
TabsResult.SelectedTab.Focus();
// then focus the control
txtRequestUrl.Focus();

Click textbox to show popup, click outside to close popup

I am designing a WPF usercontrol, and there is a textbox and a popup under the textbox. I want to click the textbox, then the popup shows. If I click outside of the textbox, the popup closes.
Now the problem is how to unfocus the textbox if I click outside the usercontrol area? Is any better way to design this control? Thanks.
On click inside you should show your popup, and to close it ater click on non-user are try to use solutions from link Is there an event handler that is fired when mouse is clicked outside textbox in c# Windows Form application?

How can I set up my winform button so that I can either click the button or press enter

I have some buttons on a winform that I would like to have the option of either clicking the button or press enter but I cannot figure out how to do it. Is it even possible?
Set the AcceptButton property on your form to the button you want.
If you want to make a button the default button on a form you should set the property AcceptButton.
In this way, if another control in your form has focus, pressing enter will close the form.
The same process is valid if you want a cancel button (a button connected to the Escape key). This time you set the CancelButton property on the form.

How to focus control in the tabItem in WPF

I have tabControl in the form. In one of the tabItems I have textbox(myTextBox). Let's call it tabItem1. When I write something into the this text box placed in the tabItem1 I want to focus textbox(searchTextBox) in the tabItem2. I placed this code in the KeyDown of the
tabItem2.Focus();
searchTextBox.Text = searchTextBoxTeropatik.Text;
searchTextBox.Focus();
I wrote this small function for this purpose.
But there is a big problem.
I press the Key
tabItem2 gets the focus.
But searchTextBox does not get the focus.(My problem)
Call UpdateLayout() after focusing the second TabItem so the system gets the time to redraw the tab.
tabItem2.Focus();
UpdateLayout();
searchTextBox.Focus();

Categories

Resources