目录
一、控件
1.对齐控件:
菜单栏>格式>对齐
2.锁定控件:
1.属性栏中Locked改为true
2.右击控件锁定
3.锁定全部:菜单栏>格式>锁定控件
注:可避免误操作
属性visible:显示/隐藏控件
3.Button控件:
设置为窗体“接受”按钮(点击Enter键触发)
private void form1_load(object sender, EventArgs e)
{
this.AcceptButton = button2;
}
设置为窗体的“取消”按钮(Esc触发)
this.CancelButton = button3;
4.TextBox控件
设置加密文本框:属性PasswordChar(自定义显示符号)
属性UseSystemPasswordChar(使用“*”)
突出显示: SelectionStart(开始位置)
SelectionLength(长度)
文本更改响应事件: TextChanged
private void textbox1_cahnged(object sender, EventArgs e)
{
this.label1.Text = textBox1.Text;
}
5.RichTextBox控件
属性Multiline:设置是否显示滚动条
属性ScrollBars:如何显示滚动条
属性SelectionFont:设置字体属性
属性SelectionColor:设置字体颜色
事件LinkClicked:设置超链接
属性SelectionBullet:是否以符号列表形式显示内容
属性SelectionIndent:段落相对于控件左边缘缩进
属性SelectionRightIndent:相对于控件右边缘缩进
this.richTextBox1.SelectionBullet = true;
this.richTextBox1.SelectionIndent = 8;
this.richTextBox1.SelectionRightIndent = 17;
6.选择类控件
(下拉组合框ComboBox、复选框CheckBox、单选按钮RadioButton、数值选择NumericUpDown、列表ListBox)
ComboBox下拉组合框控件:
属性DropDownStyle:选择只可选类型
选择全部(前提DropDownStyle为DropDown)
comboBox1.SelectAll();
事件SelectedValueChanged:当下拉框的选择项发生改变时引发
CheckBox复选控件:
Click事件中判断是否选中
private void checkBox_Click(object sender, EventArgs e)
{
if (checkBox1.CheckState == CheckState.Checked)
{
MessageBox.Show("被选中", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("没选中");
}
}
事件CheckStateChanged:选择状态发生改变时响应
private void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
MessageBox.Show("复选框选择状态发生改变");
}
RadioButton单选控件
Click事件中判断是否选中
private void radiobutton1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
MessageBox.Show("选中苹果");
}
private void radioButton2_Click(object sender, EventArgs e)
{
if (radioButton2.Checked)
MessageBox.Show("选中西红柿");
}
CheckedChanged事件:单选选中状态发生改变
NumericUpDown数值选择控件
Value属性:获取NumericUpDown控件的数值
Maximum属性:设置控件最大值
Minimum属性:设置控件最小值
DecimalPlaces属性:设置小数点后显示几位
ThousandsSeparator属性:是否每三个十进制字位插入一个分隔符
Hexadecimal属性:是否可用十六进制显示值
ListBox列表控件
添加项
private void button1_Click(object sender, EventArgs e)
{
//label1.Text = textBox1.Text;
if (textBox1.Text == "")
MessageBox.Show("请输入内容");
else
{
listBox1.Items.Add(textBox1.Text);
textBox1.Text = "";
}
}
移除项
private void button4_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItems.Count == 0)
MessageBox.Show("没有要移除的内容");
else
listBox1.Items.Remove(listBox1.SelectedItem);//移除选中项
}
HorizontalScrollbar属性:是否总显示水平滚动条
ScrollAlwaysVisible属性:是否总显示垂直滚动条
MultiColumn属性:是否支持多列显示
SelectionMode属性:选择多项
7.分组类控件
(Panel容器控件、GroupBox分组框控件、TabControl选项卡控件)
Panel容器控件
Enabled属性:是否对用户交互做出响应( 是否有用)
if (textBox1.Text.Trim() == "Auston")//判断文本框是否输入“Auston”
panel1.Show();
GroupBox分组框控件
优:总是显示边框;可以显示标题;缺:没有滚动条
Text属性:设置标题
TabControl选项卡控件
TabPage:表示选项卡
TabPages属性:表示所有选项卡的集合
Appearance属性:设置选项卡按钮的样式
在选项卡中加按钮:TabPage的Control属性的Add()方法
tabControl1.ImageList = imageList1;//使用imageList控件图像
tabPage1.ImageIndex = 0;//使用索引0位置的图像作为选项卡图标
tabPage1.Text = "选项卡1";
tabPage2.ImageIndex = 0;
tabPage2.Text = "选项卡2";
//tabControl1.Appearance = TabAppearance.Buttons;//设置成三维按钮的样式
tabControl1.Appearance = TabAppearance.FlatButtons;//平面按钮的样式
Button but1 = new Button();
but1.Text = "新增按钮";
but1.Width=140;
but1.Height = 40;
tabPage1.Controls.Add(but1);
添加选项卡 :使用tabPages选项卡集合的Add()方法
string title="选项卡"+(tabControl1.TabCount+1).ToString();
TabPage newtabpage = new TabPage(title);
tabControl1.TabPages.Add(newtabpage);
移除选项卡:使用TabPages属性的Remove()方法
private void button6_Click(object sender, EventArgs e)
{
if (tabControl1.SelectedIndex == 0)
MessageBox.Show("未选中选项卡");//提示信息
else
tabControl1.TabPages.Remove(tabControl1.SelectedTab);//移除被选中的选项卡
}
删除所有选项卡:使用TabPages属性的Clear()方法
8.菜单、工具栏、状态栏控件
MenuStrip菜单控件:
支持多文档界面、菜单合并、工具提示和溢出
文本框输入“文件&F”,“&”被识别为确认快捷键的字符,快捷Alt+F,显示为“文件(F)”
ToolStrip工具控件
StatusStrip状态栏控件:
由ToolStripStatusLabel对象组成,还可以包含ToolStripDropDownButton、ToolStripSplitButton、ToolStripProgressBar控件
toolStripStatusLabel1.Text = DateTime.Now.ToString();//状态栏显示日期
private void button7_Click(object sender, EventArgs e)
{
this.toolStripProgressBar1.Maximum = 1000;//进度条最大值
this.toolStripProgressBar1.Minimum = 0;//进度条最小值
this.toolStripProgressBar1.Step = 1;//进度条的增值
for (int i = 0; i < 1000; i++)
{
System.Threading.Thread.Sleep(1);
this.toolStripProgressBar1.PerformStep();//按照step属性增加进度栏位置
}
}
二、事件
1.单击事件Click
2.加载事件Load
private void form2_load(object sender, EventArgs e)
{
DialogResult dir = MessageBox.Show("是否打开窗口?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dir == DialogResult.Yes)
{
//Form1.ActiveForm.Hide();
}
else
{
this.Close();
}
}
3.关闭事件FormClosing
4.MDI窗体(多文档界面窗体)
设置父窗体:IsMdiContainer设置成True即可
设置子窗体
public Form MdiParent{get;set}
排列MDI子窗口
public void LayoutMdi(MdiLayout value)
5.继承窗体
使用编码继承
public partial class Form2 : Form1
使用继承选择器
6.在继承窗体中修改继承的控件属性
修改属性中的Modifiers(对其对象可见级别)