自学c#基础第六课,常用控件label

发布时间 2023-05-08 16:21:15作者: PHPer1

label

label和linklabel控件,主要是设置里面可见属性、字体什么的,linklabel的话有点中后下划线样式LinkBehavior

listbox

主要介绍了里面的方法

            //清除listBox1列表
            // listBox1.Items.Clear();
            //添加listBox1元素
            // listBox1.Items.Add(1);
            //添加listBox1元素
            //listBox1.Items.Add(2);
            //添加listBox1元素
            // listBox1.Items.Add(3);
            //输出listBox1选中的
            MessageBox.Show(listBox1.SelectedIndex.ToString());
            MessageBox.Show(listBox1.SelectedItem.ToString());
            //listBox1.Items.Count;
            //listBox1.Items.Remove(22);
            //listBox1.Items.RemoveAt(3);
            listBox2.Items.AddRange(listBox1.Items);                

checkBox1

checkBox1有三种状态,可以使用属性ThreeState

private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == false && checkBox2.Checked == true)
            {
                textBox1.Text = "二号被选中";
            }
            else if (checkBox1.Checked == true && checkBox2.Checked == false)
            {
                textBox1.Text = "一号被选中";
            }
            else if (checkBox1.Checked == true && checkBox2.Checked == true)
            {
                textBox1.Text = "一号、二都被选中";
            }
            else if (checkBox1.Checked == false && checkBox2.Checked == false)
            {
                textBox1.Text = "都没被选中";
            }

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == false && checkBox2.Checked == true)
            {
                textBox1.Text = "二号被选中";
            }
            else if (checkBox1.Checked == true && checkBox2.Checked == false)
            {
                textBox1.Text = "一号被选中";
            }
            else if (checkBox1.Checked == true && checkBox2.Checked == true)
            {
                textBox1.Text = "一号、二都被选中";
            }
            else if (checkBox1.Checked == false && checkBox2.Checked == false)
            {
                textBox1.Text = "都没被选中";
            }
        }

radioButton

当同一个容器中(Form、Panel、GroupBox、PictureBox等)存在两个以上的单选按钮时,只能有一个被选中。但不在同一个容器中的几组单选按钮彼此不关联,是可以有多个被选中的。

拖拽出来的两个radioButton默认为选中1了,但是在属性checked的时候都是false啊,google了下,需要这样操作:

本来radioButton在窗体上默认是有一个要选中的,要想启动窗体时,不被选中,需要进行如下操作。
在设计窗体上选中所有radioButton,然后将checked状态修改为true,再修改为false。

private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                textBox1.Text = "radioButton1选中了";
                textBox2.Text = "";

            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked == true)
            {
                textBox2.Text = "radioButton2选中了";

                textBox1.Text = "";
            }
        }