I have a main app that is able to call .net methods. My .net method starts a new thread that shows a modal form.
Now my .net method needs to disable all windows from the main app while the modal form is shown.
How can I do this?
I was thinking about the win32 method "GetWindow",, but then I would need the handle of the main-app form.
In my example you see the main app that calls the method with the modal dialog.
When I click on the main app while the threadingform-dlg is open, the threadingform-dlg should blink.
Use the ShowDialog() function of the form instead of Show(). It will automatically make the form modal.
I don't know if it disables all other windows or only the calling one, try.
You should not show a modal form from a separate thread (you really shouldn't even show a non-modal form from a thread). Instead, display the modal form from your application's main form. If this requires moving some of your code around, so be it.
foreach (Form openedForm in Application.OpenForms) {
if (openedForm.GetType() == FormToClose) {
openedForm.Hide();
}
}
You can get the all open windows of the application by accessing OpenForms property.
var forms = Application.OpenForms;
Related
Let's say I have some threads running there.
They must perform some actions only if no modal forms shown over main form.
I don't want to add hack checks like setting some flag before opening some modal form. Is there any generic way?
We use this hack:
if (this.Visible && !this.CanFocus)
{
//another modal window is opened
}
But you should know, that modal window "blocks" main form execution. And you can check this only in some cases, like repainting.
I'm aware that this title doesn't say much but it's really hart to explain what I want in few words.
I have two forms (main & help). Once I press button on main form help form pop ups. What I would like to implement is function to block user from doing anything on main form till he close help form.
I would not like to play with visible controls but I would like to have an effect you might have seen on some program that when user tries to click on main form help form "blinks" along with error sound playing. Once user close help form program works as usual
Hope you understand what I meant
This is called a modal dialog, and luckily, the answer is simple; show the child Form with the ShowDialog method instead of using Show. This is a blocking call that will not return until the child form/dialog is closed, so it means that you can check the return value and any properties if needed right after that line of code (probably not useful for a help window, but in most circumstances it is useful to check the user's action).
using( var dlg = new MyHelpDialog() )
{
if( dlg.ShowDialog() == DialogResult.OK )
{
// user chose "OK", do something (?)
// you can also access properties of the form after the fact
string whatever = dlg.SomeStringProperty;
}
}
You're looking for modal forms:
How to: Display Modal and Modeless Windows Forms
The thing you're talking about is called a "Modal Window".
See
How to: Display Modal and Modeless Windows Forms
I have a requirement where the user wants to be able to click a button to show a dialog with some information. They want the ability to move the dialog off of the form and put focus back on the calling form and make changes to the calling form with the dialog still open.
It is basically a map on the main form and the dialog is a map legend.
Is this possible? How would I accomplish this task? It seems like I would need to do something with a panel like how Visual Studio does this with their dockable panels.
Call the Show method instead of ShowDialog.
This method is a non-blocking call (unlike ShowDialog, it will return immediately, not after the new form closes) and will not show the form modally.
You'll probably want to pass the parent form as the parameter so that it will show as a child form.
You can show the dialog in non-modal way.
Like this:
formLegend.Show();
Insead of calling legendForm.ShowDialog(), just use legendForm.Show(). It will display the legend form without restricting the map's usage.
I am working on an application for work and I need a customized messagebox to appear. I have created a simple form called Alert.cs that I have styled the way I want and added one button with a click method of this.Close(). now I want it to behave exactly like a standard messagebox.show(). I have the form showing but when I use the standard messagebox.show("text of alert") it waits to continue operation until the user click 'OK', this is what I want the form to do.
Use Form.ShowDialog();. This allows the form to act the same way as a MessageBox in the sense that it retains focus until closed.
You will need to implement a static method for your Alert class if you want the exact MessageBox-like behaviour.
public static DialogResult Show(string text)
{
Alert form = new Alert(text);
return form.ShowDialog();
}
Now you can use the form by calling:
Alert.Show("my message");
You can use a modal windows form. Something like
Form frm = new Form();
frm.ShowDialog(this);
See Form.ShowDialog Method
Shows the form as a modal dialog box
with the currently active window set
as its owner.
Displaying Modal and Modeless Windows Forms
You don't write how you currently display your Alert Form, but calling
alert.ShowDialog();
instead of alert.Show() should do the trick.
The ShowDialog that takes an owner is an even better alternative:
alert.ShowDialog(owner);
I am running an external process from an outlook plugin - and it doesn't show the dialog. I am using ShowDialog without parameters.
Any ideas how to show the dialog ?
The process doesn't have any form - i just want to show a dialog with some selections. If I call "MessageBox.Show" before, the dialog is shown - otherwise not.
I guess it is something like the answer of Peterchen in this thread: Form.ShowDialog() or Form.ShowDialog(this)?
BTW I don't have any owner, because this process doesn't have any "main window".
Any ideas ?
I think you would need a parent window for modal windows to work. After all, the only difference from Show() is that the child window blocks the parent. You can always create a hidden dummy window and use it as a main window but why not just use .Show() since it works ?