Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c#点击按钮弹出新窗口

作者:ˉ吻干迩的'泪   发布日期:2026-01-18   浏览:79

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());
        }
    }
}

解释说明:

  • MainForm 类是主窗口,包含一个按钮 button1。当用户点击该按钮时,会触发 button1_Click 事件。
  • button1_Click 事件处理程序中,创建了一个新的 NewWindow 实例,并调用 Show() 方法将其显示出来。
  • NewWindow 类是一个简单的窗体,它继承自 Form 类,并设置了窗口标题和大小。
  • Program 类中的 Main 方法是应用程序的入口点,负责启动主窗口。

这段代码展示了如何在 C# WinForms 应用程序中实现点击按钮弹出新窗口的功能。

上一篇:c# linq select

下一篇:c#网页开发

大家都在看

c# 二进制

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

c#网络编程

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站