在 C# 中单击按钮时显示新表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1553235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:46:29  来源:igfitidea点击:

To show a new Form on click of a button in C#

c#winforms

提问by subbu

I am new to C# can anybody tell me on How to show a new Form on click of a button.

我是 C# 新手,谁能告诉我如何在单击按钮时显示新表单。

采纳答案by Johnno Nolan

Try this:

尝试这个:

private void Button1_Click(Object sender, EventArgs e ) 
{
   var myForm = new Form1();
   myForm.Show();
}

回答by Bryan

private void ButtonClick(object sender, System.EventArgs e)
{
    MyForm form = new MyForm();
    form.Show(); // or form.ShowDialog(this);
}

回答by Nelson Reis

Double click the button in the form designer and write the code:

双击表单设计器中的按钮,编写代码:

    var form2 = new Form2();
    form2.Show();

Search some samples on the Internet.

在互联网上搜索一些示例。

回答by anon58192932

This is the code that I needed. A defined user control's .show() function doesn't actually show anything. It must first be wrapped into a form like so:

这是我需要的代码。定义的用户控件的 .show() 函数实际上不显示任何内容。它必须首先被包装成这样的形式:

CustomControl customControl = new CustomControl();
Form newForm = new Form();
newForm.Controls.Add(customControl);
newForm.ShowDialog();

回答by Jalaluddin

Game_Menu Form1 = new Game_Menu();
Form1.ShowDialog();

Game_Menu is the form name

Game_Menu 是表单名称

Form1 is the object name

Form1 是对象名称

回答by Convicted Vapour

This worked for me using it in a toolstrip menu:

这对我在工具条菜单中使用它有用:

 private void calculatorToolStripMenuItem_Click(object sender, EventArgs e)
 {
     calculator form = new calculator();
     form.Show(); // or form.ShowDialog(this);
 }

回答by Vi sharma

1.Click Add on your project file new item and add windows form, the default name will be Form2.

1.在您的项目文件新项上单击添加并添加窗体,默认名称为Form2。

2.Create button in form1 (your original first form) and click it. Under that button add the above code i.e:

2.在form1(您原来的第一个表单)中创建按钮并单击它。在该按钮下添加上面的代码,即:

var form2 = new Form2();
form2.Show();

3.It will work.

3.它会起作用。