Windows窗体
-
属性:
- name:对象的名称
- windowsState:初始化窗体的大小,Normal,Minimized,Maximized
- StartPosition:窗体起始位置,Manual(由Location属性决定),CenterScreen(居中显示),WindowsDefaultLocation(Windows默认位置),WindowsDefaultBounds(Windows默认位置,边界由Windows决定),CenterParent(在父窗口居中)
- Text: 窗体的标题,给用户看
- MaximizeBox,MinimizeBox: 是否有最大化最小化的按钮
- BackColor:背景颜色
- BackgroundImage: 背景图片
- BackgroundImageLayout: 背景图片的布局, None(居左显示)、Tile(图像重复,默认值)、Stretch(拉伸)、Center(居中)、Zoom(按比例放大到合适大小)
- Enabled:窗体是否可用
- Font: 设置窗体上字体
- ForeColor:设置窗体上文字颜色
- Icon:设置窗体图标
-
事件
- load:窗体加载事件
- mouseClick:鼠标单击事件
- mouseDoubleClick:鼠标双击事件
- mouseMove:鼠标移动事件
- keyDown:键盘按下事件
- keyUp:键盘释放事件
- FormClosing:窗体关闭时事件
- FormClosed:窗体关闭后事件
-
消息框(DialogResult MessageBox.Show(提示语,窗体名,选择项按钮MessageBoxButtons ,提示语图标MessageBoxIcon)
- 选项按钮:OK,OKCancel,AbortRetryIgnore(中止,重试,忽略),YesNoCancel,YesNo,RetryCancel(重试,取消)
2.提示语图标:None,(Hand,Stop,Error)红x,Question问号,(Exclamation,Warning)警告,(Asterisk,Information)提示
3.返回值:None,Ok,Cancel,Abort(中止),Retry(重试),Ignore(忽略),Yes,No
- 选项按钮:OK,OKCancel,AbortRetryIgnore(中止,重试,忽略),YesNoCancel,YesNo,RetryCancel(重试,取消)
-
Label和LinkLabel:标签控件
- Name: 标签名,唯一标识
- Text:内容
- Font:文字样式
- FontColor:文本颜色
- BackColor:背景颜色
- Image:背景图片
- AutoSize:是否自动调整标签大小 true,false
- Size:标签大小
- Visible:标签是否可见
5.TextBox:文本框 - Text: 文本内容
- MaxLength:文本框最多输入字符个数
- WordWrap:自动换行
- PasswordChar:密码字符替换
- Multiline:多行文本文本框
- ReadOnly:只读
- Lines:文本行数
- ScrollBars:滚动条
-
Button:按钮控件
-
RadioButton:单选按钮
-
CheckBox:多选按钮
-
CheckedListBox:多选集合
-
ListBox:列表
- MultiColumn:列表是否支持多列
- Items:列表框的值
- SelectedItems:选中项的集合
- SelectedItem:选中项
- SelectedIndex:选中项的索引
- SelectionMode:列表选择模式
- One:只能选择一项
- MultiSimple :可选择多项
- None: 不可选择
- MultiExtended:可选择多项但要按下shift
-
方法:
- Add: 添加项
- Insert: 指定位置添加项
-
Remove: 移除项
/// <summary> /// 添加按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button1_Click(object sender,EventArgs e) { if(textBox1.Text != null && textBox1.Text != "") { listBox1.Items.Add(textBox1.Text); textBox1.Text = ""; } else { MessageBox.Show("不得为空"); } } /// <summary> /// 查看选中 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button2_Click(object sender,EventArgs e) { string s = ""; for(int i = 0;i < listBox1.SelectedItems.Count;i++) { s = s + " " + listBox1.SelectedItems[i].ToString(); } MessageBox.Show("选中项为" + s); } /// <summary> /// 删除 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button3_Click(object sender,EventArgs e) { List<string> list = new List<string>(); for(int i = 0;i < listBox1.SelectedItems.Count;i++) { list.Add(listBox1.SelectedItems[i].ToString()); } foreach(string s in list) { listBox1.Items.Remove(s); } }
- ComboBox:组合框
- DropDownStyle:外观 Simple(显示文本框和列表框,文本框可编辑),DropDown(只显示文本框,文本框可通过鼠标展开,可编辑),DropDownList(只显示文本框,文本框可通过鼠标展开,不可编辑)
- Items: 获取或设置组合中的值
- Text:获取或设置组合框中的文本
- MaxDropDownItems: 获取或设置最多显示的项数
- Stored: 是否排序
/// <summary> /// 初始化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ComboBox_Load(object sender,EventArgs e) { comboBox1.Items.Add("计算机网络技术"); comboBox1.Items.Add("软件工程"); comboBox1.Items.Add("生物制药"); comboBox1.Items.Add("会计"); comboBox1.Items.Add("平面设计"); } /// <summary> /// 添加按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button1_Click(object sender,EventArgs e) { if (textBox1.Text == "") { MessageBox.Show("不得为空"); return; } if (comboBox1.Items.Contains(textBox1.Text)) { MessageBox.Show("当前专业已存在"); return; } comboBox1.Items.Add(textBox1.Text); textBox1.Text = ""; } /// <summary> /// combox /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ComboBox1_SelectedIndexChanged(object sender,EventArgs e) { DialogResult dialogResult = MessageBox.Show("当前所选专业为" + comboBox1.Text + "是否删除?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question); if (dialogResult == DialogResult.Yes) { comboBox1.Items.Remove(comboBox1.Text); MessageBox.Show("删除完成"); } }
-
PictureBox:图片控件
- Image:获取或设置显示的图片
- ImageLocation: 获取或设置图片路径
- SizeMode: 设置图片显示大小和位置Normal(显示在左上角),Stretchimage(适应控件大小),AutoSize(控件大小适应图片大小),Centerimage(图片在图片控件居中),Zoom(图片自动缩放至符合图片控件的大小)
-
Timer:定时器控件
/// <summary> /// 当窗体最小化时 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SizeChange(object sender,EventArgs e) { if(WindowState == FormWindowState.Minimized) { this.Hide(); notifyIcon1.Visible = true; notifyIcon1.ShowBalloonTip(20,"demo","this is a demo",ToolTipIcon.Warning); } }
-
DateTime日期时间控件
- Short:短日期格式,例如2019/1/1
- Long:长日期格式,例如2019年1月1日
- Time:仅显示时间,例如22:00:00
- Custom:用户自定义显示格式
/// <summary> /// 定时器设置 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DateTime_Load(object sender,EventArgs e) { dateTimePicker1.Format = DateTimePickerFormat.Time; timer1.Interval = 1000; timer1.Start(); } /// <summary> /// 定时器触发 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Timer1_Tick(object sender,EventArgs e) { dateTimePicker1.ResetText(); }
-
ContextMeanStrip :右击菜单控件
- MonthCalendar: 时间控件
- treeView
private void TreeView_Load(object sender,EventArgs e) { treeView1.Nodes.Add("全部信息"); } /// <summary> /// 添加下级 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button1_Click(object sender,EventArgs e) { if(treeView1.SelectedNode == null) { MessageBox.Show("请选择节点!!!!"); } else if(textBox1.Text != "") { TreeNode treeNode = new TreeNode(textBox1.Text); treeView1.SelectedNode.Nodes.Add(treeNode); } else { MessageBox.Show("节点信息不得为空!"); } } /// <summary> /// 添加同级 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button3_Click(object sender,EventArgs e) { if(treeView1.SelectedNode.Parent == null) { MessageBox.Show("请添加下级"); return; } if(treeView1.SelectedNode == null) { MessageBox.Show("请选择节点!!!!"); } else if(textBox1.Text != "") { TreeNode treeNode = new TreeNode(textBox1.Text); treeView1.SelectedNode.Parent.Nodes.Add(treeNode); treeView1.ExpandAll(); } else { MessageBox.Show("节点信息不得为空!"); } } /// <summary> /// 删除节点 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button2_Click(object sender,EventArgs e) { if(treeView1.SelectedNode == null) { MessageBox.Show("请选择节点!!!!"); } else if(treeView1.SelectedNode.Nodes.Count == 0) { treeView1.SelectedNode.Remove(); } else { MessageBox.Show("请先删除子节点"); } }
-
listView
/// <summary> /// 初始化列表 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ListView_Load(object sender,EventArgs e) { //标题 ColumnHeader c1 = new ColumnHeader(); c1.Width = 100; c1.Text = "姓名"; ColumnHeader c2 = new ColumnHeader(); c2.Width = 50; c2.Text = "年龄"; ColumnHeader c3 = new ColumnHeader(); c3.Width = 100; c3.Text = "手机号"; //显示网格线 listView1.GridLines = true; //显示全行 listView1.FullRowSelect = true; //设置只能单选 listView1.MultiSelect = false; //显示详细信息 listView1.View = View.Details; //添加标题 listView1.Columns.Add(c1); listView1.Columns.Add(c2); listView1.Columns.Add(c3); } /// <summary> /// 添加 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button1_Click(object sender,EventArgs e) { if(label1.Text.Equals("") && label2.Text.Equals("") && label3.Text.Equals("")) { ListViewItem item = new ListViewItem(); //第一列的数据 item.Text = textBox1.Text; //第二列的数据 item.SubItems.Add(textBox2.Text); //第三列数据 item.SubItems.Add(textBox3.Text); listView1.Items.Add(item); MessageBox.Show("添加成功"); //添加后把文本输入框清空 foreach(Control c in Controls) { if(c is TextBox) { c.Text = ""; } } } else { MessageBox.Show("请填写内容"); } } /// <summary> /// 删除 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button2_Click(object sender,EventArgs e) { List<ListViewItem> list = new List<ListViewItem>(); for(int i = 0;i < listView1.SelectedItems.Count;i++) { list.Add(listView1.SelectedItems[i]); } foreach(ListViewItem item in list) { listView1.Items.Remove(item); } }
-
notifyIcon: 托盘控件
/// <summary> /// 当窗体最小化时 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SizeChange(object sender,EventArgs e) { if(WindowState == FormWindowState.Minimized) { this.Hide(); notifyIcon1.Visible = true; notifyIcon1.ShowBalloonTip(20,"demo","this is a demo",ToolTipIcon.Warning); } }
- toolTip:气泡控件
/// <summary> /// 设置气泡 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OpenAndSaveFile_Load(object sender,EventArgs e) { toolTip1.ToolTipTitle = "提示"; toolTip1.ToolTipIcon = ToolTipIcon.Info; toolTip1.SetToolTip(this.button1,"打开文件"); }
- MDI窗体: 窗体内窗体
- progressBar进度条控件
/// <summary> /// 开始跑进度条 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button1_Click_1(object sender,EventArgs e) { progressBar1.Maximum = 1000; progressBar1.Value = i; label1.Show(); while (progressBar1.Value!=progressBar1.Maximum){ label1.Text = "进度:"+progressBar1.Value*100 / progressBar1.Maximum+"%"; label1.Refresh(); i++; progressBar1.Value = i; Thread.Sleep(100); }
- panel容器
相关代码在GitHub地址/// <summary> /// 控制panel容器 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button1_Click(object sender,EventArgs e) { //清空容器容器中的控件 this.panel1.Controls.Clear(); //new一个进度条控件 progressBar progress = new progressBar(); //不设置或出现System.ArgumentException:“无法将顶级控件添加到控件。” progress.TopLevel = false; //让进度条控件的大小以容器大小为主 progress.Dock = DockStyle.Fill; //去除窗口边界 progress.FormBorderStyle = FormBorderStyle.None; //设置最大化 progress.WindowState = FormWindowState.Maximized; //隐藏工具栏 progress.Visible = false; //容器大小设置为窗口大小 panel1.Height = Screen.PrimaryScreen.Bounds.Size.Height; panel1.Width = Screen.PrimaryScreen.Bounds.Size.Width; this.panel1.Controls.Add(progress); progress.Show(); }