Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何控制从其他表单加载表单_C#_Winforms_Windows Applications - Fatal编程技术网

C# 如何控制从其他表单加载表单

C# 如何控制从其他表单加载表单,c#,winforms,windows-applications,C#,Winforms,Windows Applications,我想从另一个窗体控制窗体加载事件。 我的问题是我在form1运行时创建了一些winform控件,但是创建将由form2控制 我将在form2中从用户处读取一些数据,当用户输入特定文本时,我将在form1中创建winform控件 我使用from1在运行时创建winform控件来编写一些代码 private TextBox txtBox = new TextBox(); private Button btnAdd = new Button(); private Lis

我想从另一个窗体控制窗体加载事件。 我的问题是我在
form1
运行时创建了一些
winform控件
,但是创建将由
form2
控制

我将在
form2
中从用户处读取一些数据,当用户输入特定文本时,我将在
form1
中创建winform控件

我使用
from1
在运行时创建
winform
控件来编写一些代码

private TextBox txtBox = new TextBox();
        private Button btnAdd = new Button();
        private ListBox lstBox = new ListBox();
        private CheckBox chkBox = new CheckBox();
        private Label lblCount = new Label();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.BackColor = Color.White;
            this.ForeColor = Color.Black;
            this.Size = new System.Drawing.Size(550, 550);
            this.Text = "Test Create form in runtime ";
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.StartPosition = FormStartPosition.CenterScreen;

            this.btnAdd.BackColor = Color.Gray;
            this.btnAdd.Text = "Add";
            this.btnAdd.Location = new System.Drawing.Point(90, 25);
            this.btnAdd.Size = new System.Drawing.Size(50, 25);
            this.txtBox.Text = "Text";
            this.txtBox.Location = new System.Drawing.Point(10, 25);
            this.txtBox.Size = new System.Drawing.Size(70, 20);

            this.lstBox.Items.Add("One");

            this.lstBox.Sorted = true;
            this.lstBox.Location = new System.Drawing.Point(10, 55);
            this.lstBox.Size = new System.Drawing.Size(130, 95);
            this.chkBox.Text = "Disable";
            this.chkBox.Location = new System.Drawing.Point(15, 190);
            this.chkBox.Size = new System.Drawing.Size(110, 30);
            this.lblCount.Text = lstBox.Items.Count.ToString() + " items";
            this.lblCount.Location = new System.Drawing.Point(55, 160);
            this.lblCount.Size = new System.Drawing.Size(65, 15);

            this.Controls.Add(btnAdd);
            this.Controls.Add(txtBox);
            this.Controls.Add(lstBox);
            this.Controls.Add(chkBox);
            this.Controls.Add(lblCount);

        }

如何从form2制作相同的东西

我不知道你需要哪种“控制”。然而,在多表单环境中,表单之间的通信是微不足道的。沟通有很多种方式,一种可以是

在父窗体中创建类型为
Form
的公共属性

public Form propForm1 {get;set;}
在菜单项上单击打开form1时,将其对象存储到该公共属性

var form1 = New yourchildformname();
form1.MdiParent = this;
propForm1 = form1; // Add this line.
form1.Show();
现在,每次单击其他按钮打开form2时,都会有propForm1对象,您可以使用它在该表单上设置一些数据

编辑: 在form2上,您可以按以下方式访问form1的控件:

private void button1_Click(object sender, EventArgs e)
{
    this.parent.propForm1.txtUserName = "Yokohama";
}

记住上面的代码在表单2中。还可以在Form2中使用Form1的实例将txtUserName的“access modifier”属性从
private
设置为
public

,顺便说一句,你在哪里搞混了?我尝试了一下,但无法控制加载事件当你创建实例时加载将在Form1加载时被调用OK,调用了fine load,但如何控制它?如果要在
InitializeComponent()
定义中控制它,请查找
Form1\u load
和它注册load Event的注释行某种控制文本框、按钮和组合框