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.
Related
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.
New Thought, Maybe I am looking at this totally incorrectly. So Here is exactly what I am trying to do in case there is another option I am not aware of.
I have a WPF app, the main window shows a smaller dialog window using ShowDialog(), when a user clicks on the parent window that showed the dialog, I need to make the dialog window, flash, shake or blink.
AresAvatar posted a link that looks like it might be able to use, but is there another option I am not aware of?
My original Question.
Mouse click event when Modal window's parent is clicked in WPF app?
I have a wpf app that shows a modal window using ShowDialog().
I would like to fire an event when the user tries to click the parent window that is now disabled.
Is it possible on the parent to receive a click event when it has shown a modal window?
When I attempted this using an interaction trigger, the events never fired on parent window.
Otherwise, what suggestions / options are there.
Thanks
No WPF events are sent under these conditions. The only Windows message I can see that gets sent is WM_WINDOWPOSCHANGING. You could check for that message, and check if the window was disabled when it occurred. Here's a good article on checking WM_WINDOWPOSCHANGING.
Edit: that link seems to be dead. Here's an example on StackOverflow of checking window messages.
I know this is an old question but I'll post my solution in case any one needs it.
Set the dialog.owner prior to calling ShowDialog().
var dialog = new DialogWindow();
dialog.owner = MainWindow;
dialog.ShowDialog();
The result is that clicking on the main window, brings the dialog window to the front and makes the dialog window flash.
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);
}
I am using the ToolStripDropDown to host the user control as the pop-up window. The problem is when a context menu strip is displayed from within this pop-up window, the pop-up itself closes in the moment the context menu opens.
I have tried to subclass the ContextMenuStrip and added WS_EX_NOACTIVATE to CreateParams but nothing changed. First I thought that there is no way to do this since it is common behavior but then I tried to put a TextBox class onto the pop-up user control and invoke the Edit control context menu - and the parent pop-up window did not close.
What am I missing?
Had a similary Problem. On my UserControll was a toolstrip. When I pressed the toolsstripdropdownbutton the dropdown was shown but the popup disapeared.
The reason was that popup.Autoclose was true. After Setting to false the Popup is not closed any more.
ToolStripDropDown popup = new ToolStripDropDown();
popup.AutoClose = false; //Set to FALSE
popup.Margin = Padding.Empty;
popup.Padding = Padding.Empty;
ToolStripControlHost host = new ToolStripControlHost(userControl1);
host.Margin = Padding.Empty;
host.Padding = Padding.Empty;
popup.Items.Add(host);
popup.Show(button1, new Point(100,100));
Actual Solution should be the one in Martin's final comment:
Use ContextMenu Instead of ContextMenuStrip
That one worked for me, and the ToolStripDropDown no longer closes by itself when right clicking one of its content controls, like it should. We still need it to AutoClose, disabling AutoClose on ToolStripDropDown will do bad things, it is supposed to close on losing focus. Example: open any other app window, and the ToolStripDropDown will continue to appear on top