简体   繁体   English

Winforms控制表单

[英]Winforms Controlling Forms

How can i control all my forms from main () 我如何从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.ShowDialog()可以提供很多帮助,但是执行堆栈可能会溢出。 Form.Show and Form.Hide methods runs when an application class has been set. 设置应用程序类后,将运行Form.Show和Form.Hide方法。 In Application.Run (Form) way there's always a main form. 在Application.Run(窗体)方式中,总有一个主窗体。 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. 它们都应该并排存在,但是由于您向母版制作了Form1 ,因此遇到了一些问题。

To solve this you need another FormMaster above all four of them. 为了解决这个问题,您需要在所有四个之上的另一个FormMaster This one will be started from Application.Run() . 这将从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. 现在,此表单可以是Visible = false ,但是在其构造函数中,您可以创建所有四种表单,并决定如何将它们粘合在一起,首先显示哪种表单,在什么情况下关闭整个应用程序。

通常的方法是使用事件处理程序

All I can understand is that you have several WinForm s and you want some main Form to control them? 我所能理解的是,您有多个WinForm并且想要一些主Form来控制它们吗? 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 而且您的MainForm将按常规启动

Application.Run(new Form3());

Let me know if I dragged your question to 180 degrees! 让我知道是否将您的问题拖到了180度!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM