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

c# textbox

作者:清梦独行   发布日期:2025-05-16   浏览:30

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

解释说明:

  1. 创建TextBox控件:通过 new TextBox() 创建一个 TextBox 控件。
  2. 设置位置和大小:使用 LocationSize 属性来设置 TextBox 的位置和大小。
  3. 添加事件处理程序:使用 TextChanged 事件来监听 TextBox 中文本的变化,并在变化时触发相应的处理程序。
  4. 将控件添加到窗体:使用 this.Controls.Add(textBox1)TextBox 控件添加到窗体的控件集合中。
  5. 事件处理程序:在 TextBox_TextChanged 方法中,获取 TextBox 中的文本并输出到控制台。
  6. 主程序入口:在 Main 方法中启动应用程序并显示窗体。

这个示例展示了如何在 C# WinForms 应用程序中使用 TextBox 控件,并处理其文本变化事件。

上一篇:c#控件

下一篇:c# eventargs

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

.net和c#

c#游戏开发

c#网络编程

c# rectangle

c# if else

c# rtsp

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

Laravel 中文站