using System;
using System.Windows.Forms;
namespace ButtonPopupWindowExample
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(50, 50);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "点击我";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.button1);
this.Name = "MainForm";
this.ResumeLayout(false);
}
private void button1_Click(object sender, EventArgs e)
{
// 创建并显示新窗口
NewWindow newWindow = new NewWindow();
newWindow.Show();
}
private System.Windows.Forms.Button button1;
}
public class NewWindow : Form
{
public NewWindow()
{
this.Text = "新窗口";
this.Size = new System.Drawing.Size(300, 200);
}
}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
button1。当用户点击该按钮时,会触发 button1_Click 事件。button1_Click 事件处理程序中,创建了一个新的 NewWindow 实例,并调用 Show() 方法将其显示出来。Form 类,并设置了窗口标题和大小。Main 方法是应用程序的入口点,负责启动主窗口。这段代码展示了如何在 C# WinForms 应用程序中实现点击按钮弹出新窗口的功能。
上一篇:c# linq select
下一篇:c#网页开发
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站