简体   繁体   English

使用C#Windows窗体在Visual Studio中创建新页面

[英]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.

相关问题 使用Visual Studio 2017安装项目为C#Windows窗体创建Exe / MSI - Creating Exe/MSI for C# Windows Forms using Visual Studio 2017 Setup Project 使用MongoDB的Windows窗体应用程序(Visual Studio 2008 C#) - Windows forms application using MongoDB (Visual Studio 2008 C#) Visual Studio C#Windows表单管理 - visual studio C# windows forms managing 使用`dotnet new`在C#中创建Windows窗体应用程序 - Creating a Windows Forms Application in C# using `dotnet new` C++ 等效于 Visual Studio 中的 Pages.Add() Windows Forms - C++ Equivalent of Pages.Add() in Visual Studio Windows Forms 如何在运行时允许用户创建新的 Picturebox Visual Studio C# Windows Forms - How to at runtime allow a user to create a new Picturebox Visual Studio C# Windows Forms Apps 使用Visual Studio 2012创建的C#Windows窗体应用程序无法在Windows XP上运行 - C# Windows forms app created using Visual Studio 2012 not working on windows xp 在Vista上使用Visual Studio 2005 C#编写的代码无法在使用Visual Studio 2010 C#的新Windows 7 PC上运行 - Code written using visual studio 2005 C# on vista does not run on new Windows 7 PC using visual studio 2010 C# Visual Studio C# Windows 窗体……更改按钮颜色? - Visual Studio C# Windows Forms… changing button color? 一个c#项目(MS Visual Studio)中有2个表单或窗口 - 2 forms or windows in one single c# project (MS visual studio)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM