简体   繁体   English

从另一个具有所有组件的窗体中打开一个新窗体

[英]Open a new Form from another Form whith ALL componentns

Im trying to open a new form on a click on the main form. 我试图在主窗体上单击以打开新窗体。 The problem is the the new form oppens up but whithout any component, just embty form this is what i wrote. 问题是出现了新表格,但没有任何内容,只是难忘的表格,这就是我写的。

private void button2_Click(object sender, EventArgs e)
    {
        Form rr = new Form();
        this.Hide();
        rr.ShowDialog();

    }

That's because you're opening basic Form but not your form (that is with controls). 那是因为你打开基本形式 ,但不是你的形式(即与对照)。

Suppose your form class (which you've designed) is MyForm , so you should put it like 假设您的表单类(您设计的)是MyForm ,那么您应该像这样

  private void button2_Click(object sender, EventArgs e) {
    MyForm rr = new MyForm(); // <- MyForm not Form!
    rr.ShowDialog();
  }

Problem : it is because you are just creating and displaying the new Form without adding controls on it. 问题:这是因为您只是在创建和显示新Form而未在其上添加controls

Method 1 : You can Add controls to the Form created at runtime. 方法1:您可以将控件添加到在运行时创建的窗体。

Try This: Adding Label on Form -similarly you can add other controls. 尝试以下操作:在Form上添加Label ,您可以添加其他控件。

        Form rr = new Form();
        Label l1=new Label();
        l1.Text = "label 1";
        rr.Controls.Add(l1);
        this.Hide();
        rr.ShowDialog();

Method 2 : you can create the NewForm from IDE and add then add controls from DesignView . 方法2:可以从IDE创建NewForm ,然后从DesignView添加然后添加controls

Adding Form from Visual Studio IDE: Visual Studio IDE添加表单:

1. Right click on project 1.右键单击project
2. Select Add -> Windows Form... 2.选择Add -> Windows Form...
3. Now Enter form your formname -> SampleForm 3.现在输入您的表单名称-> SampleForm
4. Now Add the controls on the newly created SampleForm 4.现在,在新创建的SampleForm上添加controls
5. Display the newly created Form 5.显示新创建的表格

SampleForm sample=new SampleForm();
sample.ShowDialog();

i have a little helper function witch is this: 我有一个小助手功能,女巫是这样的:

  private void ShowForm<T>() where T: Form, new ()
    {
        this.Hide();
        T form = new T();
        form.FormClosed += (s, e) => this.Show();
        form.Show();
    }

this is the main form witch works like a storyboard and for example when some one click a button and im going to display a new form i do this: 这是女巫工作的主要形式,就像情节提要一样,例如,当某个人单击一个按钮并且即时通讯将显示新形式时,我这样做:

    private void SomeButton_Click(object sender, EventArgs e)
    {
        ShowForm<PublishersForm>();
    }

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

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