new form problem c# - c#

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

Related

How to check in .net if program's main form doesn't have any modal forms opened?

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.

Get current MessageBox

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.

.net C# windows Form Application : open popup window

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?

How do you make a non-modal topmost dialog that is only topmost in regards to the parent form in WinForms?

Thinking about this for an About dialog but I'm sure it's applicable in other places (say a find box)
Sorry if this is a dupe, but I couldn't find this or how to articulate the last part about it only being on top of the parent. How do you make a form that is always on top of the parent form, but is non-modal, but doesn't cover up other apps?
Try this to open your dialog:
FindDialog fd = new FindDialog();
fd.Show(this);
The key is to assign dialog's owner.
Not sure exactly what you mean; Form.ShowDialog is only modal with respect to the parent, not the application, unless the application is single threaded.
For example, I made an app to test this which was organized like the following:
mainform:
2 buttons, each of which begins a thread that creates a frmDialog1 and calls ShowDialog
frmDialog1:
single button which creates a frmDialog2 and calls ShowDialog on it.
frmDialog2:
does nothing (ie. blank)
when they were all running I could access/drag mainform. I could also do the same with frmDialog1 (both versions) only if I hadn't clicked the button that shows dialog 2.

C#: What is the proper way to show a form for "frmMainForm" settings and disposing?

Backdrop: There are two forms, the main application form and a form to edit various settings for the operations of main application.
What would be the proper way to show the frmSettings form and how to dispose it once clicked OK or Cancel in the actual frmSettings form?
Perhaps a Dialog would be better suited to your Settings "form." There are subtle differences between a dialog and a form that would make the dialog easier to handle. A return code indicating the button that was clicked makes dialogs useful.
Supposing that you used a dialog - a using statement could be used (off the top of my head):
using (DialogSettings dlgSettings = new DialogSettings)
{
if (dlgSettings.ShowDialog() == DialogResult.OK)
{
}
}
If you insist on using a form then you would have to
Instance the form
show the form
record whether ok or cancel was clicked to a form level variable (from within the forms ok/cancel button click code)
hide the form
save the recorded value from the form
dispose of the form
make use of the saved ok/cancel value
fyi, using "frm" is not a recommended part of the C# coding guidelines. Microsoft prefers you don't use hungarian notation in .NET at all.
using (frmSettings s = new frmSettings() )
{
if( s.ShowDialog() == DialogResult.OK )
{
//do work
}
}
In the main application declare an instance and show it.
using(frmSettings settingsInstance = new frmSettings())
{
settingsInstance.Show(); //or ShowDialog()
}
Then just close the form when done...

Categories

Resources