开发者

Winforms Controlling Forms

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 e开发者_开发技巧xplicitly
        // 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


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.


The usual way is to use event handlers.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜