一、 文本类控件
1 标签控件
2 按钮控件
3 文本框控件
this.textBox1.ReadOnly = true;
this.textBox1.Text = "只读文本";
this.textBox2.PasswordChar = '@';
this.textBox2.UseSystemPasswordChar = true; //textBox2中的文本为密码字符 * 显示
this.textBox3.Multiline = true; //多行文本输入
this.textBox3.Height=100;
4 richTextBox有格式文本控件
richTextBox1.Multiline = true;
this.richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical; //垂直滚动条
richTextBox1.SelectionFont = new Font("楷体", 12, FontStyle.Bold); //字体
richTextBox1.SelectionColor = System.Drawing.Color.Blue; //字体颜色
二、选择类控件
1 下拉组合框控件
(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);
}
}
}
}