WinForm RadioButton、CheckBox、ComboBox控件的使用
参考
- 豆包
- https://www.cnblogs.com/ljhandsomeblog/p/11126277.html
- https://www.cnblogs.com/xiaomihu-0001/p/11750273.html
- https://cloud.tencent.com/developer/ask/sof/102044678
- https://www.cnblogs.com/ericyi/p/3924547.html
- https://blog.csdn.net/jingcairensheng/article/details/78879240
环境
软件/系统 | 版本 | 说明 |
---|---|---|
Windows | windows 10 专业版 22H2 64 位操作系统, 基于 x64 的处理器 | |
Microsoft Visual Studio | Community 2022 (64 位) - Current 版本 17.13.6 | |
.NET Framework | 4.8 |
注意
RadioButton
需要放入同一容器才可以实现分组唯一选择。ComboBox
如果通过DataSource
赋值List
数据,并且List
是自定义类泛型,那么这个类、类属性需要设置为public
,并且属性要设置{ get; set; }
,否则会报错无法绑定到新的显示成员,参数名:newDisplayMember
- 获取一组控件的值可以通过
容器.Controls
进行遍历处理。
预览
代码
- Form.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 单选_多选_下拉组件测试Demo { public partial class Form1 : Form { public Form1() { InitializeComponent(); //// 列表赋值 //this.comboBox1.DataSource = new List<string>() { // "男人", // "女人", // "不告诉你", //}; // 列表结构化数据赋值 var data = new List<ComboItem>() { new ComboItem(){ Id=1, Text ="男人" }, new ComboItem(){ Id=2, Text ="女人" }, new ComboItem(){ Id=3, Text ="不告诉你" }, }; this.comboBox1.DataSource = data; this.comboBox1.DisplayMember = "Text"; this.comboBox1.ValueMember = "Id"; } /// <summary> /// 单选事件(这里会出现多选项时会调用多次,因为也会让别的组件取消选中,所以出发changed) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void radioButton_CheckedChanged(object sender, EventArgs e) { foreach (RadioButton item in radioButtons1.Controls) { Debug.WriteLine($"item:{item.Text} value:{item.Checked}"); } } /// <summary> /// 多选事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void checkBox_CheckedChanged(object sender, EventArgs e) { foreach (CheckBox item in checkBoxs.Controls) { Debug.WriteLine($"item:{item.Text} value:{item.Checked}"); } } /// <summary> /// 下拉框调整 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { Debug.WriteLine(this.comboBox1.ValueMember); Debug.WriteLine(this.comboBox1.SelectedValue); Debug.WriteLine(this.comboBox1.SelectedIndex); Debug.WriteLine(this.comboBox1.SelectedItem); } } public class ComboItem { public int Id { get; set; } public string Text { get; set; } } }
- Form1.Designer.cs
namespace 单选_多选_下拉组件测试Demo { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要修改 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.checkBox1 = new System.Windows.Forms.CheckBox(); this.checkBox2 = new System.Windows.Forms.CheckBox(); this.checkBox3 = new System.Windows.Forms.CheckBox(); this.comboBox1 = new System.Windows.Forms.ComboBox(); this.radioButton1 = new System.Windows.Forms.RadioButton(); this.radioButton2 = new System.Windows.Forms.RadioButton(); this.radioButtons1 = new System.Windows.Forms.FlowLayoutPanel(); this.radioButtons2 = new System.Windows.Forms.FlowLayoutPanel(); this.radioButton3 = new System.Windows.Forms.RadioButton(); this.radioButton4 = new System.Windows.Forms.RadioButton(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.checkBoxs = new System.Windows.Forms.FlowLayoutPanel(); this.label3 = new System.Windows.Forms.Label(); this.radioButtons1.SuspendLayout(); this.radioButtons2.SuspendLayout(); this.checkBoxs.SuspendLayout(); this.SuspendLayout(); // // checkBox1 // this.checkBox1.AutoSize = true; this.checkBox1.Location = new System.Drawing.Point(3, 3); this.checkBox1.Name = "checkBox1"; this.checkBox1.Size = new System.Drawing.Size(78, 16); this.checkBox1.TabIndex = 0; this.checkBox1.Text = "checkBox1"; this.checkBox1.UseVisualStyleBackColor = true; this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox_CheckedChanged); // // checkBox2 // this.checkBox2.AutoSize = true; this.checkBox2.Location = new System.Drawing.Point(87, 3); this.checkBox2.Name = "checkBox2"; this.checkBox2.Size = new System.Drawing.Size(78, 16); this.checkBox2.TabIndex = 1; this.checkBox2.Text = "checkBox2"; this.checkBox2.UseVisualStyleBackColor = true; this.checkBox2.CheckedChanged += new System.EventHandler(this.checkBox_CheckedChanged); // // checkBox3 // this.checkBox3.AutoSize = true; this.checkBox3.Location = new System.Drawing.Point(171, 3); this.checkBox3.Name = "checkBox3"; this.checkBox3.Size = new System.Drawing.Size(78, 16); this.checkBox3.TabIndex = 2; this.checkBox3.Text = "checkBox3"; this.checkBox3.UseVisualStyleBackColor = true; this.checkBox3.CheckedChanged += new System.EventHandler(this.checkBox_CheckedChanged); // // comboBox1 // this.comboBox1.FormattingEnabled = true; this.comboBox1.Items.AddRange(new object[] { "男人", "女人"}); this.comboBox1.Location = new System.Drawing.Point(44, 250); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(246, 20); this.comboBox1.TabIndex = 3; this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged); // // radioButton1 // this.radioButton1.AutoSize = true; this.radioButton1.Location = new System.Drawing.Point(3, 3); this.radioButton1.Name = "radioButton1"; this.radioButton1.Size = new System.Drawing.Size(95, 16); this.radioButton1.TabIndex = 4; this.radioButton1.TabStop = true; this.radioButton1.Text = "radioButton1"; this.radioButton1.UseVisualStyleBackColor = true; this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged); // // radioButton2 // this.radioButton2.AutoSize = true; this.radioButton2.Location = new System.Drawing.Point(104, 3); this.radioButton2.Name = "radioButton2"; this.radioButton2.Size = new System.Drawing.Size(95, 16); this.radioButton2.TabIndex = 5; this.radioButton2.TabStop = true; this.radioButton2.Text = "radioButton2"; this.radioButton2.UseVisualStyleBackColor = true; this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged); // // radioButtons1 // this.radioButtons1.Controls.Add(this.radioButton1); this.radioButtons1.Controls.Add(this.radioButton2); this.radioButtons1.Location = new System.Drawing.Point(47, 128); this.radioButtons1.Name = "radioButtons1"; this.radioButtons1.Size = new System.Drawing.Size(263, 25); this.radioButtons1.TabIndex = 7; // // radioButtons2 // this.radioButtons2.Controls.Add(this.radioButton3); this.radioButtons2.Controls.Add(this.radioButton4); this.radioButtons2.Location = new System.Drawing.Point(47, 169); this.radioButtons2.Name = "radioButtons2"; this.radioButtons2.Size = new System.Drawing.Size(263, 25); this.radioButtons2.TabIndex = 8; // // radioButton3 // this.radioButton3.AutoSize = true; this.radioButton3.Location = new System.Drawing.Point(3, 3); this.radioButton3.Name = "radioButton3"; this.radioButton3.Size = new System.Drawing.Size(95, 16); this.radioButton3.TabIndex = 4; this.radioButton3.TabStop = true; this.radioButton3.Text = "radioButton3"; this.radioButton3.UseVisualStyleBackColor = true; // // radioButton4 // this.radioButton4.AutoSize = true; this.radioButton4.Location = new System.Drawing.Point(104, 3); this.radioButton4.Name = "radioButton4"; this.radioButton4.Size = new System.Drawing.Size(95, 16); this.radioButton4.TabIndex = 5; this.radioButton4.TabStop = true; this.radioButton4.Text = "radioButton4"; this.radioButton4.UseVisualStyleBackColor = true; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(45, 101); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(125, 12); this.label1.TabIndex = 9; this.label1.Text = "单选需要放入同一布局"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(42, 20); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(197, 12); this.label2.TabIndex = 10; this.label2.Text = "多选可以放入统一布局容器循环获取"; // // checkBoxs // this.checkBoxs.Controls.Add(this.checkBox1); this.checkBoxs.Controls.Add(this.checkBox2); this.checkBoxs.Controls.Add(this.checkBox3); this.checkBoxs.Location = new System.Drawing.Point(44, 45); this.checkBoxs.Name = "checkBoxs"; this.checkBoxs.Size = new System.Drawing.Size(334, 31); this.checkBoxs.TabIndex = 11; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(42, 221); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(185, 12); this.label3.TabIndex = 12; this.label3.Text = "下拉可以绑定数据源或结构化数据"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 450); this.Controls.Add(this.label3); this.Controls.Add(this.checkBoxs); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.radioButtons1); this.Controls.Add(this.radioButtons2); this.Controls.Add(this.comboBox1); this.Name = "Form1"; this.Text = "Form1"; this.radioButtons1.ResumeLayout(false); this.radioButtons1.PerformLayout(); this.radioButtons2.ResumeLayout(false); this.radioButtons2.PerformLayout(); this.checkBoxs.ResumeLayout(false); this.checkBoxs.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.CheckBox checkBox1; private System.Windows.Forms.CheckBox checkBox2; private System.Windows.Forms.CheckBox checkBox3; private System.Windows.Forms.ComboBox comboBox1; private System.Windows.Forms.RadioButton radioButton1; private System.Windows.Forms.RadioButton radioButton2; private System.Windows.Forms.FlowLayoutPanel radioButtons1; private System.Windows.Forms.FlowLayoutPanel radioButtons2; private System.Windows.Forms.RadioButton radioButton3; private System.Windows.Forms.RadioButton radioButton4; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.FlowLayoutPanel checkBoxs; private System.Windows.Forms.Label label3; } }
博 主 :夏秋初
地 址 :https://www.cnblogs.com/xiaqiuchu/p/18886268
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。
地 址 :https://www.cnblogs.com/xiaqiuchu/p/18886268
如果对你有帮助,可以点一下 推荐 或者 关注 吗?会让我的分享变得更有动力~
转载时请带上原文链接,谢谢。