一些基础控件的常用操作记录(Button/ComboBox/TextBox/TimePicker)
记录一些基础控件的常用属性及事件。
Button按钮,Button按钮背景设为透明,背景选择一张圆角图片,可实现简单的圆角效果。
1 2 3 4 5 6 7 | 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:
1 2 3 4 5 6 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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:
1 2 3 4 5 6 7 8 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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 (); } }); |
分类:
.NET技术 / Winform
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~