0

How can i control all my forms from main ()

        Form1 frm1 = new Form1();
        Form1 frm2 = new Form1();
        Form1 frm3 = new Form1();

        Application.Run(frm1);  // This way form-control goes to the frm1.
                                // In frm1 i have to write
                                frm1.Clicked += ()=>frm2.Show;

        // I want the form-controlling style more explicitly
        // I dont want to use Application.Run()

        frm1.Show();
        frm1.Clicked += frm2.Show();

form.ShowDialog () helps much but the execution stack can overflow. Form.Show and Form.Hide methods runs when an application class has been set. In Application.Run (Form) way there's always a main form. and i dont want this one. Any other approach you use in this problem

jack-london
  • 1,599
  • 3
  • 21
  • 42
  • 2
    I didn't understand your question. Please specify precisely what you want to achieve. – Ikaso Mar 24 '10 at 09:20
  • 2
    Your going to have to explain more what you want to do. What do you mean by "control all your forms" (control them how? what do you want to do with them?) and "the execution stack can overflow". In your pseudo code there's no difference between Show and ShowDialog, except that the execution will stop until the form is closed when you use ShowDialog. – Andy Shellam Mar 24 '10 at 09:21
  • But when i use showdialog the stacktrace's length starts be longer. I can see all the form navigation over this trace.it can be resulted with stackoverflow exception. – jack-london Mar 24 '10 at 11:26
  • 1
    If what you want is a situation where there is no "master form," that is possible, but you need to clarify the question, as others have commented. – BillW Mar 24 '10 at 12:05
  • Inherit from `ApplicationContext` - see [here](http://stackoverflow.com/a/13406508/111794). – Zev Spitz Nov 16 '12 at 09:48

3 Answers3

1

Your problem is, that you have four forms. All of them should exist side by side, but because you made Form1 to the master you got some problems.

To solve this you need another FormMaster above all four of them. This one will be started from Application.Run(). Now this form can be Visible = false, but in its constructor you can create all your four forms and decide how they will be glued together, which one will be shown first and under which circumstances your whole application will be closed.

Oliver
  • 43,366
  • 8
  • 94
  • 151
  • Form Controlling logic should be written in the masterform. There's no other way – jack-london Mar 24 '10 at 16:18
  • @jacklondon There is the right way: inheriting from `ApplicationContext`. See [here](http://stackoverflow.com/a/13406508/111794). – Zev Spitz Nov 16 '12 at 09:46
  • @ZevSpitz: What is the big difference of using a hidden main form that orchestrates the child forms against using an application context that does the same? I don't think that any of them is *right* or *wrong*. Just two different approaches achieving the same without any other disadvantages making one better than the other. – Oliver Aug 25 '14 at 07:36
  • Isn't it important to use the right tool for the job? A form _represents a window or dialog box that makes up an application's user interface._ ([MSDN](http://msdn.microsoft.com/en-us/library/system.windows.forms.form(v=vs.110).aspx)) and should be used to that end. Sometimes it might be necessary to resort to ugly hacks of hidden forms that are never shown, but not here. – Zev Spitz Aug 25 '14 at 11:13
0

The usual way is to use event handlers.

Phil Gan
  • 2,813
  • 2
  • 29
  • 38
0

All I can understand is that you have several WinForms and you want some main Form to control them? Well, if I understanding/assumption is correct, then about controlling like following?

public partial class Form3 : Form
{
    private void Form3_Load(object sender, EventArgs e)
    {
        Demo();
    }

    MyMainForm main = new MyMainForm(); //Your actual form
    private void Demo()
    {
        main.Click += new EventHandler(main_Click);
        main.ShowDialog();
    }

    void main_Click(object sender, EventArgs e)
    {
        MyNotificationForm notify = new MyNotificationForm();//Your notification form
        notify.Name = "notify";
        notify.Click += new EventHandler(notify_Click);
        notify.ShowDialog(main);
    }

    void notify_Click(object sender, EventArgs e)
    {
        MyWarningForm warning = new MyWarningForm();//Your warning form
        warning.Click += new EventHandler(warning_Click);
        warning.ShowDialog(main.ActiveMdiChild);
    }

    void warning_Click(object sender, EventArgs e)
    {
        ((Form)sender).Close(); //Click on form would close this.
    }
}

Following is how I'd implement the classes.

public class CBaseForm : Form
{ public CBaseForm() { this.Text = "Main App"; } }

public class MyWarningForm : CBaseForm
{ public MyWarningForm() { Label lbl = new Label(); lbl.Text = "Warning Form"; this.Controls.Add(lbl); } }

public class MyNotificationForm : CBaseForm
{ public MyNotificationForm() { Label lbl = new Label(); lbl.Text = "Notification Form"; this.Controls.Add(lbl); } }

public class MyMainForm : CBaseForm
{ public MyMainForm() { Label lbl = new Label(); lbl.Text = "Controller Form"; this.Controls.Add(lbl); } }

And you MainForm would start conventionally

Application.Run(new Form3());

Let me know if I dragged your question to 180 degrees!

KMån
  • 9,896
  • 2
  • 31
  • 41
  • -1. Inherit from [`ApplicationContext`](http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext.aspx) - see [here](http://stackoverflow.com/a/13406508/111794). – Zev Spitz Aug 20 '14 at 18:24