一些基础控件的常用操作记录(Button/ComboBox/TextBox/TimePicker)

发布时间 2023-03-29 14:20:23作者: [春风十里]

记录一些基础控件的常用属性及事件。

Button按钮,Button按钮背景设为透明,背景选择一张圆角图片,可实现简单的圆角效果。

this.button2.Text = "Click Me";
this.button2.FlatStyle = FlatStyle.Flat;
this.button2.FlatAppearance.BorderSize = 0;//去除边框
this.button2.TextImageRelation = TextImageRelation.ImageBeforeText;//导入Image,实现图片在前,文字在后
this.button2.MouseEnter += ((object sender, EventArgs e) => { this.button2.BackColor = Color.AliceBlue; });
this.button2.MouseLeave += ((object sender, EventArgs e) => { this.button2.BackColor = Color.Transparent; });//实现鼠标移入移出效果
this.button2.Click += ((object sender, EventArgs e) => { this.button2.Text = "Good!"; });

复选框CheckBox: 

this.checkBox1.Text = "复选框";
this.checkBox1.AutoCheck = false;//设为False后,点击不再有变化,需从程序中赋值
this.checkBox1.Checked = false;
this.checkBox1.ThreeState = false;//设为True表示三种状态,False为两种
this.checkBox1.Click += ((object sender, EventArgs e) => { this.checkBox1.Text = "点击"; this.checkBox1.Checked = !this.checkBox1.Checked; });
this.checkBox1.CheckedChanged += ((object sender, EventArgs e) => { this.checkBox1.Text = this.checkBox1.Checked ? "选中" : "未选中"; });

列表框ListBox:

this.listBox1.Items.Clear();
for (int i = 0; i < 10; i++)
    this.listBox1.Items.Add("第" + i + "项");
this.listBox1.SelectionMode = SelectionMode.MultiSimple;//设置选择模式
this.listBox1.MultiColumn = true;
this.listBox1.SelectedIndexChanged += ((object sender, EventArgs e) =>
{
    this.textBox1.Text = "Count:" + this.listBox1.SelectedItems.Count;
    for (int i = 0; i < this.listBox1.SelectedIndices.Count; i++)
        this.textBox1.Text += this.listBox1.SelectedIndices[i] + ";";
    for (int i = 0; i < this.listBox1.SelectedItems.Count; i++)
        this.textBox1.Text += this.listBox1.SelectedItems[i].ToString() + ";";
});
this.listBox1.SetSelected(2, true);
this.listBox1.Items.Remove("第1项");
this.listBox1.Items.RemoveAt(1);
this.listBox1.SelectedItems.Remove("第1项");
this.textBox1.Text = this.listBox1.SelectedIndex.ToString();
this.textBox1.Text = this.listBox1.SelectedItem.ToString();

时间选择器DateTimePicker:

this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
this.dateTimePicker1.CustomFormat = "yyyy-MM-dd HH:mm:ss";
this.dateTimePicker1.ShowUpDown = false;
this.dateTimePicker1.DropDownAlign = LeftRightAlignment.Right;
this.dateTimePicker1.CalendarTitleBackColor = Color.Tomato;
this.dateTimePicker1.CalendarTitleForeColor = Color.Teal;
this.dateTimePicker1.CalendarForeColor = Color.SteelBlue;
this.dateTimePicker1.ValueChanged += ((object sender, EventArgs e) => { this.textBox1.Text = this.dateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss:fff"); });

 下拉列表ComboBox:

this.comboBox1.Items.Clear();
this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;//此模式下Text不可编辑修改
for (int i = 0; i < 5; i++)
    this.comboBox1.Items.Add(i);
this.comboBox1.SelectedIndex = 0;
this.comboBox1.DropDownHeight = 200;
this.comboBox1.DropDownWidth = 100;
this.comboBox1.SelectedValueChanged += ((object sender, EventArgs e) => {
    this.textBox1.Text = "Index:" + this.comboBox1.SelectedIndex
        + ";Text:" + this.comboBox1.SelectedText
        + ";Value:" + this.comboBox1.SelectedValue
        + ";Value:" + this.comboBox1.SelectedItem.ToString();
});

文本框TextBox:

this.textBox1.Multiline = true;
this.textBox1.ScrollBars = ScrollBars.None;
this.textBox1.Width = 120;
this.textBox1.Height = 20;
this.textBox1.BackColor = Color.SteelBlue;
this.textBox1.ForeColor = Color.DarkSlateGray;
this.textBox1.BorderStyle = BorderStyle.Fixed3D;
this.textBox1.Text = "pls input password";
this.textBox1.MouseEnter += ((object sender, EventArgs e) =>//光标进入
{
    if (this.textBox1.Text == "pls input password")
    {
        this.textBox1.Text = "";
        this.textBox1.ForeColor = Color.Black;
        this.textBox1.PasswordChar = '*';  //实现特殊字符隐藏密码,光标移入移出时提示的文本变化
    }
});
this.textBox1.MouseLeave += ((object sender, EventArgs e) =>//光标离开
{
    if (this.textBox1.Text == "")
    {
        this.textBox1.Text = "pls input password";
        this.textBox1.ForeColor = Color.DarkSlateGray;
        this.textBox1.PasswordChar = new char();
    }
});