第十章. Windows应用程序常用控件

本文详细介绍了Windows应用程序中常见的控件使用,包括文本类控件(如标签、文本框、richtextbox)、选择类控件(如ComboBox、CheckBox、RadioButton)、数值选择控件(NumericUpDown)以及列表控件(ListBox)。通过实例展示了各控件的基本属性、事件处理和自定义数据绑定。此外,还涵盖了分组控件(Panel、GroupBox、TabControl)的用法,为开发者提供了全面的Winform控件指南。
摘要由CSDN通过智能技术生成

一、 文本类控件
标签控件

按钮控件

文本框控件

     this.textBox1.ReadOnly = true;
     this.textBox1.Text = "只读文本";

 

     this.textBox2.PasswordChar = '@';
     this.textBox2.UseSystemPasswordChar = true;  //textBox2中的文本为密码字符 * 显示

 

     this.textBox3.Multiline = true;          //多行文本输入
     this.textBox3.Height=100;

richTextBox有格式文本控件
    richTextBox1.Multiline = true;

    this.richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical;   //垂直滚动条
    richTextBox1.SelectionFont = new Font("楷体", 12, FontStyle.Bold);  //字体
    richTextBox1.SelectionColor = System.Drawing.Color.Blue;               //字体颜色

 

 

二、选择类控件
下拉组合框控件

 (1)comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;  //不能修改下拉列表
     comboBox1.Items.Add("大学");
     comboBox1.Items.Add("中学");
     comboBox1.Items.Add("小学");

     comboBox1.SelectedIndex = 0;   //设置当前选中项的索引

 (2)下拉列表选择改变时,激发事件

     private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
     {
            this.label1.Text = comboBox1.Text;
     }

winform ComboBox绑定自定义数据


    public partial class Form3 : Form
    {

        public Form3()
        {
            InitializeComponent();

            ComboBox cb = new ComboBox();
            ListItem item = new ListItem("我是名字值", "我是名字文本");
            cb.Items.Add(item);
            ListItem item2 = new ListItem("我是名字值2", "我是名字文本2");
            cb.Items.Add(item2);       
            ListItem item3 = new ListItem("我是名字值3", "我是名字文本3");
            cb.Items.Add(item3);

            cb.DisplayMember = "Name";
            cb.ValueMember = "ID";
            cb.SelectedItem = item2;//设置默认选中项
            groupBox1.Controls.Add(cb);;
        }
        /// <summary>
        /// 选择项类,用于ComboBox或者ListBox添加项
        /// </summary>
        public class ListItem
        {
            private string id = string.Empty;
            private string name = string.Empty;
            public ListItem(string sid, string sname)
            {
                id = sid;
                name = sname;
            }
            public override string ToString()
            {
                return this.name;
            }
            public string ID
            {
                get
                {
                    return this.id;
                }
                set
                {
                    this.id = value;
                }
            }
            public string Name
            {
                get
                {
                    return this.name;
                }
                set
                {
                    this.name = value;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (Control ctl in groupBox1.Controls)
            {
                ComboBox cbb = ctl as ComboBox;
                        if (cbb != null)
                        {
                            ListItem li = (ListItem)cbb.SelectedItem;//将选中项,强制转成ListItem
                            MessageBox.Show(li.ID);
                        }
            }
        }

  }      




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tiz198183

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值