C# TextBox:文本框控件和Button按钮控件

2022-10-07 09:38:45

文本框 (TextBox) 是在窗体中输入信息时最常用的控件,通过设置文本框属性可以实现多行文本框、密码框等。

 文本框控件最常使用的事件是文本改变事件 (TextChange),即在文本框控件中的内容改变时触发该事件。

【实例 1】

             创建一个窗体,在文本框中输入一个值,通过文本改变事件将该文本框中的值写到一个标签中。

TextBoxText.cs

public partial class TextBoxTest : Form
{
    public TextBoxTest()
    {
        InitializeComponent();
    }
    //文本框文本改变事件
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        //将文本框中的文本值显示在标签中
        label2.Text = textBox1.Text;
    }
}

按钮包括普通的按钮 (Button)、单选按钮 (RadioButton)

【实例 2】

        1)单击“登录”超链接标签,从登录界面进入主界面
        2)单击“登录”按钮,从登录界面进入主界面

Login.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace LoginForm
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void Login_Load(object sender, EventArgs e)
        {

        }
        /*
         *点击“登录”按钮
         */
        private void button1_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text;
            string pwd = textBox2.Text;
            if ("碧瑶".Equals(name) && "111111".Equals(pwd))
            {
                //指定消息框中显示的文本  消息框的标题
                MessageBox.Show("用户登录权限为管理员", "提示");
                this.Hide();//隐藏窗口
                Form1 form1 = new Form1();
                form1.Show();//显示窗体
            }else {
                MessageBox.Show("登录失败","提示");
            }
        }

        /*
         *单击“登录”超链接标签
         */
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

            //获取用户名
            string username = textBox1.Text;
            //获取密码
            string password = textBox2.Text;
            //判断用户名密码是否正确
            if ("xiaoming".Equals(username) && "123456".Equals(password))
            {
                MessageBox.Show("登录成功!","提示");
                this.Hide();//隐藏窗口
                Form1 form1 = new Form1();
                form1.Show();//显示窗体
            }
            else
            {
                MessageBox.Show("登录失败!","提示");
            }
        }
    }
}

  Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace LoginForm
{
    static class Program
    {
        /// <summary>
        /// 1)单击“登录”超链接标签,从登录界面进入主界面
        /// 2)单击“登录”按钮,从登录界面进入主界面
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Login());
        }
    }
}

【实例 3】

           实现一个简单的用户注册功能,并将提交的注册信息显示在新窗体的文本框中。

RegForm.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RegForm
{
    public partial class RegForm : Form
    {
        public RegForm()
        {
            InitializeComponent();
        }
        
        //“确定”按钮的单击事件,用于判断注册信息并跳转到新窗口显示注册信息
        private void button1_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text;
            string pwd = textBox2.Text;
            string repwd = textBox3.Text;
            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show("用户名不能为空!", "提示");
                return;
            }
            else if (string.IsNullOrEmpty(pwd))
            {
                MessageBox.Show("密码不能为空!", "提示");
                return;
            }
            else if (!pwd.Equals(repwd))
            {
                MessageBox.Show("两次输入的密码不一致!", "提示");
                return;
            }
            //将用户名和密码传递到主窗体中
            Form1 form = new Form1(name, pwd);
            form.Show();
        }

        //“取消”按钮的事件,用于关闭窗体
        private void button2_Click(object sender, EventArgs e)
        {
            //关闭窗体
            this.Close();        
    }
}

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RegForm
{
    public partial class Form1 : Form
    {
        public Form1(string name,string pwd)
        {
            InitializeComponent();
            label1.Text = "用户名:" + name;
            label2.Text = "密  码:" + pwd;
        }       
    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace RegForm
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new RegForm());
        }
    }
}

  • 作者:zs1342084776
  • 原文链接:https://blog.csdn.net/zs1342084776/article/details/91415728
    更新时间:2022-10-07 09:38:45