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

c# 控件

作者:魔影杀戮   发布日期:2026-07-05   浏览:74

using System;
using System.Windows.Forms;

namespace CSharpControlsExample
{
    public class MainForm : Form
    {
        private Button button1;
        private Label label1;
        private TextBox textBox1;

        public MainForm()
        {
            // 初始化控件
            button1 = new Button();
            label1 = new Label();
            textBox1 = new TextBox();

            // 设置按钮属性
            button1.Text = "点击我";
            button1.Location = new System.Drawing.Point(100, 50);
            button1.Click += new EventHandler(Button1_Click);

            // 设置标签属性
            label1.Text = "请输入内容:";
            label1.Location = new System.Drawing.Point(100, 100);

            // 设置文本框属性
            textBox1.Location = new System.Drawing.Point(200, 100);
            textBox1.Size = new System.Drawing.Size(150, 20);

            // 将控件添加到窗体
            this.Controls.Add(button1);
            this.Controls.Add(label1);
            this.Controls.Add(textBox1);

            // 设置窗体属性
            this.Text = "C# 控件示例";
            this.Size = new System.Drawing.Size(400, 300);
        }

        // 按钮点击事件处理程序
        private void Button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("你输入的内容是: " + textBox1.Text);
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}

解释说明

这段代码展示了一个简单的 Windows Forms 应用程序,包含三个主要的控件:ButtonLabelTextBox

  1. 初始化控件

    • button1 是一个按钮控件,用户可以点击它触发事件。
    • label1 是一个标签控件,用于显示静态文本。
    • textBox1 是一个文本框控件,允许用户输入文本。
  2. 设置控件属性

    • 设置了每个控件的位置 (Location) 和大小 (Size)。
    • 设置了按钮的文本 (Text) 和点击事件 (Click)。
    • 设置了标签的文本 (Text)。
    • 设置了文本框的位置和大小。
  3. 将控件添加到窗体

    • 使用 this.Controls.Add() 方法将控件添加到窗体中。
  4. 设置窗体属性

    • 设置了窗体的标题 (Text) 和大小 (Size)。
  5. 事件处理

    • 定义了按钮点击事件处理程序 Button1_Click,当用户点击按钮时,会弹出一个消息框显示文本框中的内容。
  6. 应用程序入口

    • Main 方法是应用程序的入口点,创建并运行 MainForm 窗体。

上一篇:websocket c#

下一篇:c# char转string

大家都在看

c# 二进制

c# 创建目录

c# socket服务端连接多个客户端

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

c# invoke方法

.net和c#

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

Laravel 中文站