C#学习之五大基础控件

发布时间 2023-11-02 17:37:25作者: 突破铁皮

Label

常见属性:

  • Text: 显示的文本内容。
  • Font: 设置字体属性。
  • ForeColor: 设置前景颜色。
  • BackColor: 设置背景颜色。
  • AutoSize: 控制是否自动调整大小。

常见方法:

  • Invalidate(): 使 Label 的整个显示区域无效。
  • Refresh(): 强制控件立即重新绘制。

TextBox

常见属性:

  • Text: 设置或获取文本框中的文本内容。
  • Font: 设置或获取文本框中文本的字体属性。
  • ForeColor: 设置或获取文本框中文本的前景色。
  • BackColor: 设置或获取文本框的背景颜色。
  • Multiline: 用于控制文本框是否可以输入多行文本。
  • PasswordChar: 设置密码文本框中的掩码字符。

常见方法:

  • Clear(): 清除文本框中的文本。
  • SelectAll(): 选中文本框中的所有文本。
  • Copy()、Cut()、Paste(): 分别用于复制、剪切和粘贴文本框中的文本。

Button

常见属性:

  • Text: 设置按钮显示的文本内容。
  • Font: 设置按钮文本的字体属性。
  • ForeColor: 设置按钮文本的前景颜色。
  • BackColor: 设置按钮的背景颜色。
  • Enabled: 控制按钮是否可用。

常见方法:

  • PerformClick(): 以编程方式模拟用户单击按钮。
  • Focus(): 将焦点设置到按钮上。
  • Click() 事件: 通过该事件处理方法响应按钮被点击的事件。

RadioButton

常见属性:

  • Text: 设置单选按钮显示的文本内容。
  • Checked: 设置或获取单选按钮的选中状态。
  • Enabled: 控制单选按钮是否可用。

常见方法:

  • Focus(): 将焦点设置到单选按钮上。
  • Check() 和 Uncheck(): 用于编程方式设置或取消单选按钮的选中状态。
  • CheckedChanged 事件: 通过该事件处理方法响应单选按钮选中状态改变的事件。

CheckBox 

常见属性:

  • Text: 设置复选框显示的文本内容。
  • Checked: 设置或获取复选框的选中状态。
  • CheckState: 设置或获取复选框的选中状态(Checked、Unchecked、Indeterminate)。
  • ThreeState: 控制复选框是否支持三种状态(Checked、Unchecked、Indeterminate)。

常见方法:

  • Focus(): 将焦点设置到复选框上。
  • Check() 和 Uncheck(): 用于编程方式设置或取消复选框的选中状态。
  • CheckedChanged 事件: 通过该事件处理方法响应复选框选中状态改变的事件。

下面是我使用上面五个控件写的一个简单的登录页面

采用垂直布局嵌套每个控件


using MySqlConnector;
using System.Data;

namespace CsharpLearn
{
    
    internal static class Program
    {
        //标明该程序为单进程
        [STAThread]
        static void Main()
        {
            ApplicationConfiguration.Initialize();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}


using System.Text.RegularExpressions;

namespace CsharpLearn
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void loginButtion_Click(object sender, EventArgs e)
        {
            String accountText = account.Text;
            String passwordText = password.Text;
            if(!Regex.IsMatch(accountText, "^[a-zA-Z0-9]+$"))
            {
                accountMsg.Visible = true;
                return;
            }
            else if(!Regex.IsMatch(passwordText, "^[a-zA-Z0-9]+$"))
            {
                passwordMsg.Visible = true;
                return;
            }
            else
            {
                MessageBox.Show("登录成功");
            }
        }
        private void Input_Focus(object sender, EventArgs e)
        {
            accountMsg.Visible = false;
            passwordMsg.Visible = false;
        }

    }
}
namespace CsharpLearn
{
    partial class Form1
    {
        private System.ComponentModel.IContainer components = null;
        private Label accountLabel;
        private TextBox account;
        private Label accountMsg;
        private Label passwordLabel;
        private TextBox password;
        private Label passwordMsg;
        private RadioButton teacher;
        private RadioButton student;
        private RadioButton manger;
        private CheckBox rememberMe;
        private CheckBox acceptItem;
        private Button loginButton;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            //窗体基本设置
            this.Text = "系统登录页面";
            this.StartPosition = FormStartPosition.CenterScreen;
            this.ClientSize = new System.Drawing.Size(700, 600);
            this.Font=new Font("Arial", 18, FontStyle.Bold);
            this.ForeColor = Color.Blue;
            this.BackColor = Color.Azure;
            FlowLayoutPanel mainPanel = new FlowLayoutPanel();
            mainPanel.BackColor = Color.Wheat;
            mainPanel.BorderStyle = BorderStyle.FixedSingle;
            mainPanel.Location = new Point(100, 40);
            mainPanel.Padding = new Padding(50, 50, 50, 50);
            mainPanel.AutoSize = true;
            mainPanel.FlowDirection = FlowDirection.TopDown;
            FlowLayoutPanel accountPanel = new FlowLayoutPanel();
            accountPanel.FlowDirection = FlowDirection.LeftToRight;
            accountPanel.AutoSize = true;
            FlowLayoutPanel passwordPabel = new FlowLayoutPanel();
            passwordPabel.FlowDirection = FlowDirection.LeftToRight;
            passwordPabel.AutoSize = true;
            FlowLayoutPanel rolePanel = new FlowLayoutPanel();
            rolePanel.FlowDirection = FlowDirection.LeftToRight;
            rolePanel.AutoSize = true;
            FlowLayoutPanel othersPanel = new FlowLayoutPanel();
            othersPanel.FlowDirection = FlowDirection.LeftToRight;
            othersPanel.AutoSize = true;
            //标题
            Label headLabel = new Label();
            headLabel.Name = "headLabel";
            headLabel.Text = "测试系统";
            headLabel.Font= new Font("Arial", 24, FontStyle.Bold);
            headLabel.Margin = new Padding(100, 0, 0, 20);
            headLabel.AutoSize = true;
            //账号标签
            accountLabel = new Label();
            accountLabel.Name = "accountLabel";
            accountLabel.Text = "账号";
            accountLabel.AutoSize = true;
            accountPanel.Controls.Add(accountLabel);
            //账号输入,最大字符为8,不允许多行输入,只允许输入字母和数字
            account = new TextBox();
            account.Name = "account";
            account.Multiline = false;
            account.MaxLength = 8;
            account.Width = 300;
            accountPanel.Controls.Add(account);
            //账号提醒
            accountMsg = new Label();
            accountMsg.Name = "accountMsg";
            accountMsg.Text = "账号格式错误";
            accountMsg.ForeColor = Color.Red;
            accountMsg.AutoSize = true;
            accountMsg.Visible = false;
            accountMsg.Margin = new Padding(90, 0, 0, 0);
            //密码标签
            passwordLabel = new Label();
            passwordLabel.Name = "passwordLabel";
            passwordLabel.Text = "密码";
            passwordLabel.AutoSize = true;
            passwordPabel.Controls.Add(passwordLabel);
            //密码输入,最大长度为12,不允许多行输入,只允许输入字母和数字
            password = new TextBox();
            password.Name = "password";
            password.Multiline = false;
            password.PasswordChar = '-';
            password.MaxLength = 12;
            password.Width = 300;
            password.Location = new Point(300, 150);
            passwordPabel.Controls.Add(password);
            //密码提醒
            passwordMsg = new Label();
            passwordMsg.Name = "passwordMsg";
            passwordMsg.Text = "密码格式错误";
            passwordMsg.ForeColor = Color.Red;
            passwordMsg.AutoSize = true;
            passwordMsg.Visible = false;
            passwordMsg.Margin = new Padding(90, 0, 0, 0);
            //角色选择按钮
            teacher = new RadioButton();
            student = new RadioButton();
            manger = new RadioButton();
            teacher.Name = "role";
            student.Name = "role";
            manger.Name = "role";
            teacher.AutoSize = true;
            student.AutoSize = true;
            manger.AutoSize = true;
            teacher.Text = "老师";
            student.Text = "学生";
            manger.Text = "管理员";
            rolePanel.Controls.Add(teacher);
            rolePanel.Controls.Add(student);
            rolePanel.Controls.Add(manger);
            //其余选项
            rememberMe = new CheckBox();
            acceptItem = new CheckBox();
            rememberMe.Name = "others";
            acceptItem.Name = "others";
            rememberMe.Text = "记住我";
            acceptItem.Text = "接受使用条款";
            rememberMe.AutoSize = true;
            acceptItem.AutoSize = true;
            othersPanel.Controls.Add(rememberMe);
            othersPanel.Controls.Add(acceptItem);
            //登录按钮
            loginButton = new Button();
            loginButton.Text = "登录";
            loginButton.Name = "loginButton";
            loginButton.Margin = new Padding(150,10,0,0);
            loginButton.AutoSize = true;



            account.GotFocus += Input_Focus;
            password.GotFocus += Input_Focus;
            loginButton.Click += loginButtion_Click;
            

            mainPanel.Controls.Add(headLabel);
            mainPanel.Controls.Add(accountMsg);
            mainPanel.Controls.Add(accountPanel);
            mainPanel.Controls.Add(passwordMsg);
            mainPanel.Controls.Add(passwordPabel);
            mainPanel.Controls.Add(rolePanel);
            mainPanel.Controls.Add(othersPanel);
            mainPanel.Controls.Add(othersPanel);
            mainPanel.Controls.Add(loginButton);


            this.Controls.Add(mainPanel);
        }

    }
}

这里绑定了两个事件,一个是单机登录判断是否输入是否符合标准,还有一个就是输入获取角点则隐藏错误提示