I have created single instance application. It accepts command line arguments and process them. If application is already running and some dialog ( open file or message box ) is opened. Now if i try to pass command line argument i need to check if dialog is shown or not. So I added this code.
if (!Application.Current.MainWindow.IsActive)
{
Application.Current.MainWindow.Activate();
}
if (Keyboard.FocusedElement != null)
{
// If focused element is not null it means no other dialog is shown.
// safe to go.
}
Ideal was like , if focused element is not null then it means focus is inside window and no other dialog is shown.
In normal scenarios when window is not minimized this code works fine. but if window is minimized then condition fails as keyboard focus is not in window.
Do u find any solution which will be generic ? I can achieve this by adding flags before each dialog box. but I have more than 10 dialog boxes. In future i may add more dialog boxes.
Thanks
Very old question but I've recently faced a similar issue.
Here is how I solved it:
public static bool IsAnyModalOpened()
{
return Application.Current.Windows.OfType<Window>().Any(IsModal);
}
public static bool IsModal(this Window window)
{
var fieldInfo = typeof(Window).GetField("_showingAsDialog", BindingFlags.Instance | BindingFlags.NonPublic);
return fieldInfo != null && (bool)fieldInfo.GetValue(window);
}
Related
My program includes many windows.
Whenever I want to change the first window (I named "bigwindow") and the new second window
I have been using the following code in order to switch betwing the windows:
Ireadonlycollection <string> openwindows1=driver.windowhandels;
Foreach (string current in openwindows1)
{
If (current!= bigwindow)
{
Driver.switchto().window(current):
Break
}
}
I trying to using this code when a popup window was opened from the second window and it is not working..
The focus is still on the second window and not on the third..
Please help me
In method that is working in the background, i have two important lines :
createPopup();
MessageBox.Show(sth);
more lines
more lines
createPopup() just creates a popup, adds a grid as a child and shows popup.
My question is, why first shows up messageBox, then shows up Popup, which appears after all lines in this method done ? How could I make this popup to show before all lines in this method will be done ?
All the UI changes are normally queued up and will be shown at once on the screen.
And this does not include MessageBox. So it shows up immediately and prevents the execution, until user clicks on Ok. Hence eventhough your popUP is first executed, it will be shown in the UI only after the MessageBox.
For your problem, Try placing your MessageBox.Show(something) in a separate thread.
createPopup();
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("some message");
});
more lines
more lines
Give it a try. I am not sure whether it solves your problem or not as I dnt know the code in createPopUp() method.
Creating the pop-up, does not actually draw it on the screen until the Layout event. If you want to ensure that the pop-up has been drawn before you display the pop-up, attach an event handler to the pop-up's LayoutUpdated event and display the message box from within that event handler. Be sure to detach the event handler as well or you will see multiple message boxes.
public InitPage()
{
Popup popup = new Popup();
popup.LayoutUpdated += popup_LayoutUpdated;
LayoutRoot.Controls.Add(popup);
}
void popup_LayoutUpdated(object sender, object e)
{
popup_LayoutUpdated -= popup_LayoutUpdated;
MessageBox.Show("hello");
}
I'm writing small notification component, however I have a problem with showing multiple notification at once.
My alert/notification window inherits from window class. The contstructor of Alert looks like that
public Alert()
{
InitializeComponent();
Focusable = true;
ShowActivated = false;
ShowInTaskbar = false;
//Topmost = true;
AllowsTransparency = true;
Opacity = 1;
// Set up the fade in and fade out animations
_Hint = "hint";
Loaded += new RoutedEventHandler(DesktopAlertBase_Loaded);
}
In main window of application I have a list of alerts, thanks to this I know where to place alerts on the screen. If I want to show an alert I create an instance of Alert class and then I use Show() method. Alerts are shown and everything is OK except the fact that I can use/move/interact with only last alert window (last created window).The rest of alert windows can't be clicked. However if I close last window I can use one before last and so on... Is it possible to make multiple windows clicable/active ?
Can you please, provide code, where you use show method? It looks like you are using ShowDialog() method instead of Show().
Another possibility is some locks in your DesktopAlertBase_Loaded() method.
The popup that gets opened in my app always stays on top. It goes away when i minimize the app. But otherwise if the app opens a new window or if i switch to another program without minimizing the app, the popup stays on top. How do i hide the popup when the app loses focus?
Thanks
It sounds like you need to set the owner/parent of the popup to the correct window by setting the Owner property to the window that created it.
You can pass owner (parent window) to the constructor of the popup window and then specify
public class PopupWindow: Window
{
public PopupWindow(Window owner)
{
this.Owner = owner;
}
}
Then open your popup window
Now to open your popup window you will use something like this:
var popup = new PopupWindow(ownerWindow);
popup.Show();
or if caller is a parent (owner)
var popup = new PopupWindow(this);
popup.Show();
In my Windows Form's Form_Load event, I want to show a FolderBrowserDialog to let the user select a directory, and if the directory they've selected is not valid (meaning it lacks certain files that the application needs), I want to show it again. However, when I create a new FolderBrowserDialog, it does not appear when I call ShowDialog.
while (ValidDirectorySelected() == false && tryAgain == true)
{
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
dialog.ShowNewFolderButton = false;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
{
tryAgain = false;
}
}
}
When I step into it, the dialog.ShowDialog() line is reached on the second time, and then nothing happens. The dialog does not appear, and the debugger doesn't move on. It just stops. It works perfectly the first time, but not the second. I've even tried just copying that entire using block and pasting it right after the first one, and the same thing happens. The dialog shows only once.
What do I need to do to show a FolderBrowserDialog more than once?
Solution:
Passing 'this' to ShowDialog fixed my issue. I also moved the using to outside of the while loop, to avoid needlessly re-creating the dialog.
Minimize Visual Studio, you'll find the dialog back.
This is a focus issue, triggered because you display the dialog in the Load event. When the dialog closes, there is no window left in your app that can receive the focus. Your Load event hasn't finished running so the app's main window isn't yet visible. Windows has to find a window to give the focus to and will select one from another program. Like Visual Studio.
When you display the dialog again, it cannot steal the focus back because Visual Studio has acquired it. So the dialog appears behind Visual Studio's main window, out of view.
You'll have to fix this by allowing your main window to become visible. And call dialog.ShowDialog(this) to be completely sure. You could use the Shown event, for example.
Try this:
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
while (ValidDirectorySelected() == false && tryAgain == true)
{
dialog.ShowNewFolderButton = false;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
{
tryAgain = false;
}
}
}
...move your using outside the while loop to keep from destroying the folder browser every time. You don't have to do that. You can reuse FolderBrowserDialog.