I have a large application with several forms, any of them could get a MessageBox (MessageBox.Show()) that is modal and locks the form.
On activation of another form I now need to find this MessageBox and bring the form that has this MessageBox to front. Is there any way to check this?
I know about the Application.OpenForms property, maybe there is something like this for MessageBox?
Edit1 : For example, say that we open Winform1, then a event in Winform1 will go to the mainController that opens Winform2. Lateron Winform1 is getting a MessageBox.Show, But its fully possible to bring Winform2 to front(above Winform1). So now I need to react to the Winform.Activated to check if there is any MessageBox.Show and if so, bring this form that holds the MessageBox to front.
You could find them by using Application.OpenForms like this:
foreach (Form f in Application.OpenForms)
{
if (f.Visible && ! f.CanFocus)
{
// whatever...
}
}
Or: use a different approach altogether:
Make all your forms handle Application.EnterThreadModal and Application.LeaveThreadModal, so that when the app goes modal while that form is current, you add that form onto a list so you can keep track of it, and remove it from the list when it leaves modal...
Then all you need to do is query that list to see if any forms have a modal dialog box open.
Try using one of the Show methods that takes an owner:
MessageBox.Show(this, "My Message");
I tested this on .NET 4 / Windows 7, and when the message box is opened it brings its owner to the front.
Related
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.
(C# / WindowsMobile 6)
Let's take an application with 3 STATIC forms: Form1, Form2, Form3, where Form1 opens Form2 by calling Form2.Show(), and Form2 does the same with Form3. Form2 and Form3 have a "Exit" button, that just hides the form (not "close", just hide).
So, we execute these steps:
open the application;
go to Form2, by clicking "Form2" button on Form1;
go to Form3, by clicking "Form3" button on Form2;
open File Explorer, and "re-open" application by clicking on it's file. Form3 appears;
hide Form3 by clicking on "Exit" button on Form3 ( this.Hide() ). That's the problem: file explorer appears instead Form2.
I don't want to call "callingform".Show() every time I hide a form. This "works", but file explorer screen appears after "this.Hide()" and before "callinform.Show()" and I need to "control" who's calling who.
How to solve this? Is there any way to bring all application's form to foreground in the same order they appeared?
Thanks in advance.
There really isn't a way. You could implement a way to store forms in a similar way to the first answer, but when you switch you need to do:
"callingform".BringToFront();
"callingform".Show();
That will put all of your forms in front of Explorer.
You might need to do some investigation into this, but off the top of my head you could try looking at the Application.Forms[] collection.
Maybe someone can confirm or deny this but I think usually, Application.OpenForms[0] will be the main/initial form with subsequent Form appearances being in Application.OpenForms[1], Application.OpenForms[2], etc...
So you could simply try navigating backwards through this Forms collections.
Something like (or a variation of),
public void BringLastOpenedFormToFront()
{
if(Application.OpenForms.Count > 0)
{
Form form = Application.OpenForms[Application.OpenForms.Count - 1];
BringToFront(form); // your bring to front method.
}
}
This would allow you to ensure the last Form that appeared was brought to the front and immediately visible to the user. Let me know if you need any clarification.
Link to MSDN:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.openforms.aspx
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 using Windows Form application. I want to open a small textbox on a window to enter user's name or Email in Starting for the program.
How can I achieve this?
Write one, 'tis almost trivial (creating the form and adding label, textbox and buttons) and using the VB one is perputating something that was only put in to appease the baying mob.
Key method is ShowDialog() which is a method on a Form.
On the form make sure you set the flags for the Ok and Cancel buttons correctly and provide a property (ideally) to allow you to read (and write if necessary) the text box
You can then do something along the lines of the following:
using(MyInputForm mif = new MyInputForm)
{
if (mif.ShowDialog() == DialogResult.OK)
{
dataFromDialog = mif.InputData;
}
else
{
// logic to deal with cancel
}
}
You can do something similar in WPF, don't have an example to hand though.
Maybe the answer to this question will help:
What is the C# version of VB.net’s InputDialog?