Better way to Display forms one on top of another - c#

I have a Main form.I want to launch another form from it and launch another from the launched form.I want to ensure that the Main form is not editable when sub forms are displayed so i use showdialog()
Mainform>(Showdialog)>form1>(showDialog+dispose)>form2(dispose)>Mainform
From Mainform i call form2.ShowDialog() then from form2 i use the following code to launch another form
this.visible=false;
form3.showdialog();
this.dispose();
But there are some problems in this.Is there a better way to achieve what im looking for?
edit:more description
I have a Main form,User clicks a Button on Mainform>Form1 is lauched>User clicks a Button in Form1>Form 2 is lauched(diposing/hiding form1) after form2 is closed Mainform should be brought to front and made editable,until then all other forms should be on top of Mainform and Mainform should be un-editable

The problem is that you have to specify the MainForm as the parent for (both) form2 and form3. When you use the overload of ShowDialog that has no parameters, WinForms uses the active form as the parent, so form3's parent becomes form2 automatically. You are then trying to close/dispose form2 causing form3 to become orphaned.
There are several options for getting the reference to MainForm, but the simplest is to use:
form2/3.ShowDialog(Application.OpenForms["MainForm"]);
Assuming that you have set the Name property on MainForm to "MainForm".

In your code, this.dispose() is executed only after form3 is closed. What i think you want is to close form2 after form3 was closed, so you can call this.Close() instead of this.Dispose().
this.visible=false;
form3.showdialog();
this.Close();
Or maybe, after form3 is shown up you dont need form2 any more. That meas:
this.visible=false;
//show instead of showdialog so it wont wait until form3 is closed
form3.show();
this.Close();

It looks like you are trying to implement something like a wizard. The best solution would be to launch all the child forms sequentially, in the main form.
If you need to pass data along the sequence, you should pass it from each dialog to the main form, which then passes it to the next dialog.

MainForm:
Form1 f = new Form1();
if (f.ShowDialog(this) == DialogResult.OK) {
Form2 f2 = new Form2();
f2.ShowDialog(this);
}
Form1 (button click which will open the form 2):
button1_click(object sender, EvengArgs e) {
this.DialogResult = DialogResult.OK;
Close();
}

Related

How to hide or close second dialogbox before opening the third dialogbox without hiding/ closing main WinForm

Is it possible a C# form to be modal without being dialogbox? Is there any alternatives to dialogbox. Because I think I cannot achieve what I want with dialogboxes. I have to (?) use dialogbox to make sure modality.
First Edit:
I have main Winform. From main Winform I want to produce a modal form like this
Form2 frm2 = new Form2();
frm2.ShowDialog();
From the frm2 I want to open new modal form frm3, but I want to close frm2 before opening frm3.
I want to close open
Form3 frm3 = new Form3();
frm3.ShowDialog();
I want to close frm2 which is password dialog; as the user enters the correct password, I want to close frm2 and show advanced settings form which is frm3.
Second Edit:
One approach is hiding the second frame before opening the third. But when I try
this.Hide()
in the second form, the first one also hid. Any solutions to hide only the second without the first? Or any other different solutions?
Third Edit:
Editing the title.
I still don't understand exactly the problem,but let's see if this is what you want to accomplish. You've got a main window, and want to show a login dialog and if the login is correct, show a settings dialog. This is how i would do it:
MainForm:
FormLogin login= new FormLogin();
if (login.ShowDialog() == true)
{
FormSettings frmSett= new FormSettings();
frmSett.ShowDialog();
}
FormLogin:
...
if (LoginOK)
{
this.DialogResult = true;
}
else
{
this.DialogResult = false;
}
this.Close();
...
Something like that. Hope that helps

Refresh an open form from another open form

I want to refresh an already opened form (form1) from another opened form's (form2) button_click(). In form1 I display the data saved by form2 and when form1 is already opened I want it to refresh if new data is saved on form2 in order to display it.
The problem is that I've tried iterating through `Application.Openforms`, but it turns out that it is read-only and I cannot access the form once found and I don't know how to access *form1* from *form2*, since I can't simply find it.
How can I access *form1* from *form2*?
Edit:
Form1 is actually opened from Form2.
The problem with Application.Openforms is that , as I've stated, a read-only list of forms already opened and I cant actually access the forms through it. They simply don't have the methods for it, I sugest you try using Application.OpenForms and look it up if you don't know how it works.
Also it's pointless to show what I've already tried because it includes Application.OpenForms, but for the sake of information:
FormCollection of = Application.OpenForms;
foreach (var f in of)
{
if (f.GetType().ToString() == "Kontrl_Doc.Visualizar")
{
f.Refresh();
}
}
When I click the button (button_click()) in Form2 it checks if Form1 is open or not. If Form1 isn't open it opens one and if it is than I'd like it to refresh it. Simultaneously, it closes Form2 and opens Form2 again, in order to reset is fields.
What I wan to do is, if the form1 is already opened , it form2 should tell it to refresh the already opened window with the form 1.
"Form1 is actually opened from Form2" - If this is the case, then just call Refresh using the form variable that you have in Form2. If necessary, make that a private field in the Form2 class or store it in an array for later use.
For example:
(Somewhere in Form2)
Form1 form1 = new Form1();
form1.Show();
(Inside the button click in Form2)
form1.Refresh();
you can use events. In form2 you place this code
public event Action ReloadForm1;
//on the place where you will reload form1
ReloadForm1();
and in form1 if you have opening form2:
form2.ReloadForm1 += Reload;
//outside method
void Reload()
{
this.Reload();
}
Create a void method in form1 and add the components u want to refresh maybe you want to reload a dropdown from db
public void Refresh()
{
...
}
then open a dialog of form2
catch the dialog result

New form disappear after creation

I'm facing a problem with form show. I have a main form where I have my GUI and I choose an option that creates an instance of a form. For example in my main form I have:
Form2 f2 = new Form2();
f2.Show();
The problem is that the form shows for about 1-2 secs and then goes behind the main form.
I tried some instructions in the main form below f2.Show() command like
f2.BringtoFront();
this.SendtoBack();
Also I added commands to the new form (Form2) load method:
this.BringtoFront();
this.Activate();
this.Focus();
Nothing of the above commands seems to be a solution for this. Only when I use f2.ShowDialog(); instruction in my main form but I don't want to do that because I need immediate access to both of these forms at the same time.
Any help? Thanks
If you don't want your second form to never go behind your main form then pass the owner in the overload of Show method that accepts the owner parameter
Form2 f2 = new Form2();
f2.Show(this); // Assuming this code runs inside the main form
If you remove or change it to comment this.SendtoBack(); :
f2.BringtoFront();
//this.SendtoBack();
it'll be OK!

Word loses focus (minimized) after closing non-modal form

I have a problem with the Word Add-in I'm developing.
The problem:
If I open non-modal form called Form1, and inside this form I press a button that opens another modal form called Form2. Now I close both forms, but Word windows is losing focus and minimized.
Note that this is not happening when I just open Form1 and close it.
When I open a non-modal form (let's call it Form1) by using Form1.Show(IWin32Window);
where the IWin32Window object is created by this method:
public static MyWin32Window getWordWindow()
{
IntPtr wordWindow = NativeMethods.FindWindowW("OpusApp", Globals.ThisAddIn.Application.ActiveWindow.Caption + " - Microsoft Word");
MyWin32Window myWin = new MyWin32Window(wordWindow);
return myWin;
}
The problem occurs only when I open another form from Form1, let's say Form2 is opened, but Form2 is modal form and opened with:
Form2.ShowDialog();
It works fine, but when I close Form2 and then Form1, the Word window is minimized. How can I prevent this behavior?
I also tried setting Form1 as the owner of Form2, like this:
Form2.ShowDialog(this);
but same result.
I found that I was able to resolve this problem by using
Form.Show();
instead of ShowDialog().

Give focus to form2

I have a program with two forms, but Form1 is opening up over Form2, which isn't good since you need to use Form2 first. How can you force focus to Form2 when the application runs?
Use ShowDialog for Form2 instead of Show.
You can also use Application.OpenForms property to get the already opened form Form2 and then call its Focus method like:
if(Application.OpenForms["Form2"] != null)
Application.OpenForms["Form2"].Focus();

Categories

Resources