[英]Creating new pages in Visual Studio using C# Windows Forms
I am trying to write a Windows Forms application using C#, however I have never done this before. 我正在尝试使用C#编写Windows窗体应用程序,但是我之前从未做过。 I have successfully created a login page which takes the user to a home page where 4 buttons are displayed. 我已经成功创建了一个登录页面,该页面将用户带到显示4个按钮的主页。 I am trying to find code to put inside each button that will take the user to a different page. 我正在尝试查找放置在每个按钮内的代码,这些代码会将用户带到另一个页面。
In windows forms application, you have to call Show method on the Form which you want to show. 在Windows窗体应用程序中,必须在要显示的窗体上调用Show方法。
Let's say on Button1 click you want to show Form2, then below code will bring Form 2 up on the screen. 假设在Button1上单击要显示Form2,然后下面的代码将在屏幕上显示Form 2。
private void button1_Click_1(object sender, EventArgs e)
{
Form2 obj = new Form2();
if (obj == null)
{
obj.Parent = this;
}
obj.Show();
this.Hide();
}
You need to create, initialize and show the other forms. 您需要创建,初始化和显示其他形式。
frmSecond frm = new frmSecond();//You should call any other constructor, may be with some parameters
frm.Text ="I wanted to change the title";//Optional: You can change any property value if you need
frm.Show();
If you want to show the form, collect some information from user and return some values then you can use ShowDialog method instead of Show method 如果要显示表单,请从用户那里收集一些信息并返回一些值,则可以使用ShowDialog方法代替Show方法
if(frm.ShowDialog(this) == DialogResult.OK){
var myVar = frm.ReturnObject;
...
}
You can use below approach for one parent window and new child window. 您可以将以下方法用于一个父窗口和一个新的子窗口。 When you press the button new form opens: 当您按下按钮时,新表格打开:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
{
frm2.ShowDialog();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.