using System;
using System.Windows.Forms;
namespace TextBoxExample
{
    public class Form1 : Form
    {
        private TextBox textBox1;
        public Form1()
        {
            // 创建一个新的TextBox控件
            textBox1 = new TextBox();
            // 设置TextBox的位置和大小
            textBox1.Location = new System.Drawing.Point(50, 50);
            textBox1.Size = new System.Drawing.Size(200, 20);
            // 添加事件处理程序,当文本改变时触发
            textBox1.TextChanged += new EventHandler(this.TextBox_TextChanged);
            // 将TextBox添加到窗体的控件集合中
            this.Controls.Add(textBox1);
        }
        // 当TextBox中的文本改变时触发的事件处理程序
        private void TextBox_TextChanged(object sender, EventArgs e)
        {
            // 获取TextBox中的文本并输出到控制台
            Console.WriteLine("TextBox内容: " + textBox1.Text);
        }
        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
    }
}new TextBox() 创建一个 TextBox 控件。Location 和 Size 属性来设置 TextBox 的位置和大小。TextChanged 事件来监听 TextBox 中文本的变化,并在变化时触发相应的处理程序。this.Controls.Add(textBox1) 将 TextBox 控件添加到窗体的控件集合中。TextBox_TextChanged 方法中,获取 TextBox 中的文本并输出到控制台。Main 方法中启动应用程序并显示窗体。这个示例展示了如何在 C# WinForms 应用程序中使用 TextBox 控件,并处理其文本变化事件。
上一篇:c#控件
下一篇:c# 判断字符串是否为数字
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站