1.含义WinForm 是 Windows Form 的简称是基于 .NET Framework 平台的客户端PC软件开发技术一般使用 C# 编程 桌面应用程序 基于winforms开发。2.WinForm项目结构1引用包括所有的系统库文件的引用依赖2App.config当前项目的配置文件3Form1.cs当前窗体的对象1. 查看form窗口代码 方式 在窗体右键 查看代码2.partial修饰的类在编译的时候会统一编译成一个类。3.Form1.cs 手动写窗体代码区域- Form1.Designer.cs自动生成窗体代码区域- Form1.resx当前窗体的资源文件图片、图标、资源等4. winform 中 以控件来组成窗体的元素 分为可视化控件 和 功能性控件可视化控件手动拖动控件到窗体中 可显示其外观和位置功能性控件 手动拖动控件到窗体中 不显示外观 可以使用其功能控件的位置视图 --工具箱5. 控件包含 属性和事件6. 属性查看方式窗体相关的属性1、常用属性 1Name属性用来获取或设置窗体的名称在应用程序中可通过Name属性来引用窗体。 2 WindowState属性 用来获取或设置窗体的窗口状态。 取值有三种 Normal 窗体正常显示、 Minimized窗体以最小化形式显示和 Maximized窗体以最大化形式显示。 3StartPosition属性用来获取或设置运行时窗体的起始位置。 4Text属性该属性是一个字符串属性用来设置或返回在窗口标题栏中显示的文字。 5Width属性用来获取或设置窗体的宽度。 6Height属性用来获取或设置窗体的高度。 7Left属性用来获取或设置窗体的左边缘的x坐标以像素为单位。 8Top属性用来获取或设置窗体的上边缘的y坐标以像素为单位。 9ControlBox属性用来获取或设置一个值该值指示在该窗体的标题栏中是否显示控制框。值为true时将显示控制框值为false时不显示控制框。 10MaximizeBox属性用来获取或设置一个值该值指示是否在窗体的标题栏中显示最大化按钮。值 为 true时显示最大化按钮值为false时不显示最大化按钮。 11MinimizeBox 属性用来获取或设置一个值该值指示是否在窗体的标题栏中显示最小化按钮。值 为 true时显示最小化按钮值为false时不显示最小化按钮。 12AcceptButton 属性该属性用来获取或设置一个值该值是一个按钮的名称当按 Enter 键时就相当于单击了窗体上的该按钮。 13CancelButton 属性该属性用来获取或设置一个值该值是一个按钮的名称当按 Esc 键时就相当于单击了窗体上的该按钮。 14Modal 属性该属性用来设置窗体是否为有模式显示窗体。如果有模式地显示该窗体该属性值为 true否则为 false。当有模式地显示窗体时只能对模式窗体上的对象进行输入。必须隐藏或关闭模式窗 体通常是响应某个用户操作然后才能对另一窗体进行输入。有模式显示的窗体通常用做应用程序中 的对话框。 15ActiveControl属性用来获取或设置容器控件中的活动控件。窗体也是一种容器控件。 16ActiveMdiChild属性用来获取多文档界面MDI的当前活动子窗口。 17AutoScroll 属性用来获取或设置一个值该值指示窗体是否实现自动滚动。如果此属性值设置为 true则当任何控件位于窗体工作区之外时会在该窗体上显示滚动条。另外当自动滚动打开时窗体 的工作区自动滚动以使具有输入焦点的控件可见。 18BackColor属性用来获取或设置窗体的背景色。 19BackgroundImage属性用来获取或设置窗体的背景图像。 20Enabled 属性用来获取或设置一个值该值指示控件是否可以对用户交互作出响应。如果控件可 以对用户交互作出响应则为 true否则为false。默认值为true。 21Font属性用来获取或设置控件显示的文本的字体。 22ForeColor属性用来获取或设置控件的前景色。 23IsMdiChild属性获取一个值该值指示该窗体是否为多文档界面MDI子窗体。值为 true时 是子窗体值为false时不是子窗体。 24IsMdiContainer 属性获取或设置一个值该值指示窗体是否为多文档界面MDI中的子窗体的 容器。值为true时是子窗体的容器值为false时不是子窗体的容器。 25KeyPreview属性用来获取或设置一个值该值指示在将按键事件传递到具有焦点的控件前窗体 是否将接收该事件。值为true时窗体将接收按键事件值为false时窗体不接收按键事件。 26MdiChildren属性数组属性。数组中的每个元素表示以此窗体作为父级的多文档界面MDI子窗 体。 27MdiParent属性用来获取或设置此窗体的当前多文档界面MDI父窗体。 28ShowInTaskbar属性用来获取或设置一个值该值指示是否在Windows任务栏中显示窗体。 29Visible属性用于获取或设置一个值该值指示是否显示该窗体或控件。值为true 时显示窗体或控件为 false时不显示。 30Capture属性如果该属性值为true则鼠标就会被限定只由此控件响应不管鼠标是否在此控件的范围内。3.windows程序的事件机制事件现实中要做的或者已发生事情。 用户和windows与控件交互的“桥梁”用来响应用户的各种操作 点击事件用户输入文本框等1. 事件的3要素 事件的目标 事件属性 事件执行程序事件的目标 交互的控件对象事件属性事件目标和用户交互的不同状态事件执行程序事件目标和用户时执行的结果2、事件绑定方式1.控件绑定法找到对应控件事件属性 填入对应事件执行程序名称 在事件执行程序中完成对应逻辑2.自定义事件方法和绑定事件方法用于代码法创建控件时使用3.双击事件目标 自动生成控件的默认事件绑定12 适合静态控件的事件绑定 3适合动态控件事件绑定3常用窗口事件1Load事件该事件在窗体加载到内存时发生即在第一次显示窗体前发生。2Activated事件该事件在窗体激活时发生。3Deactivate事件该事件在窗体失去焦点成为不活动窗体时发生。4Resize事件该事件在改变窗体大小时发生。5Paint事件该事件在重绘窗体时发生。6Click事件该事件在用户单击窗体时发生。7DoubleClick事件该事件在用户双击窗体时发生。8Closed事件该事件在关闭窗体时发生。4.labelLabel控件是System.Windows.Forms.Label 类提供的控件。作用主要用来提供其他控件的描述文字。1、常用属性1Text属性用来设置或返回标签控件中显示的文本信息。2AutoSize 属性用来获取或设置一个值该值指示是否自动调整控件的大小以完整显示其内容。取值为 true时控件将自动调整到刚好能容纳文本时的大小取值为false时控件的大小为设计时的大小。默认值为false。3Anchor 属性用来确定此控件与其容器控件的固定关系的。所谓容器控件指的是这样一种情况往往在控件之中还有一个控件例如最典型的就是窗体控件中会包含很多的控件像标签控件、文本框等。4BackColor属性用来获取或设置控件的背景色。当该属性值设置为 Color.Transparent 时标签将透明显示即背景色不再显示出来。5BorderStyle 属性用来设置或返回边框。有三种选择BorderStyle.None 为无边框默认BorderStyle.FixedSingle 为固定单边框BorderStyle.Fixed3D 为三维边框。边框效果如图 9-8 所示。6TabIndex属性用来设置或返回对象的Tab键顺序。7Enabled 属性用来设置或返回控件的状态。值为 true 时允许使用控件值为 false 时禁止使用控件5.buttonButton控件是System.Windows.Forms.Button 类提供的控件。作用最常使用的就是编写处理按钮的Click事件及MouseEnter事件代码1、常用属性Button 控件也具有许多如 Text、ForeColor 等的常规属性此处不再介绍只介绍该控件有特色的属性。以后介绍的控件也采用同样的方法来处理。1DialogResult属性当使用ShowDialog方法显示窗体时可以使用该属性设置当用户按了该按钮后ShowDialog方法的返回值。值有OK、Cancel、Abort、Retry、Ignore、Yes、No等。2Image属性用来设置显示在按钮上的图像。3FlatStyle属性用来设置按钮的外观2、常用事件1Click事件当用户用鼠标左键单击按钮控件时将发生该事件。2MouseDown事件当用户在按钮控件上按下鼠标按钮时将发生该事件。3MouseUp事件当用户在按钮控件上释放鼠标按钮时将发生该事件。6.TextBoxTextBox控件是System.Windows.Forms.TextBox 类提供的控件。文本输入框提供了文本输入和编辑的功能1、主要属性1Text属性Text属性是文本框最重要的属性因为要显示的文本就包含在Text属性中。默认情况下最多可在一个文本框中输入2048个字符。如果将MultiLine属性设置为true则最多可输入32KB 的文本。Text属性可以在设计时使用【属性】窗口设置也可以在运行时用代码设置或者通过用户输入来设置。可以在运行时通过读取Text属性来获得文本框的当前内容。2MaxLength 属性用来设置文本框允许输入字符的最大长度该属性值为 0 时不限制输入的字符数。3MultiLine 属性用来设置文本框中的文本是否可以输入多行并以多行显示。值为 true 时允许多行显示。值为false时不允许多行显示一旦文本超过文本框宽度时超过部分不显示。4HideSelection属性用来决定当焦点离开文本框后选中的文本是否还以选中的方式显示值为true则不以选中的方式显示值为 false将依旧以选中的方式显示。5ReadOnly属性用来获取或设置一个值该值指示文本框中的文本是否为只读。值为 true时为只读值为 false时可读可写。6PasswordChar 属性是一个字符串类型允许设置一个字符运行程序时将输入到 Text 的内容全部显示为该属性值从而起到保密作用通常用来输入口令或密码。7 ScrollBars属性 用来设置滚动条模式 有四种选择 ScrollBars.None 无滚动条 ScrollBars.Horizontal水平滚动条ScrollBars.Vertical垂直滚动条ScrollBars.Both水平和垂直滚动条。注意只有当MultiLine属性为true时该属性值才有效。在WordWrap属性值为true时水平滚动条将不起作用8SelectionLength属性用来获取或设置文本框中选定的字符数。只能在代码中使用值为0 时表示未选中任何字符。9SelectionStart属性用来获取或设置文本框中选定的文本起始点。只能在代码中使用第一个字符的位置为0第二个字符的位置为1依此类推。10SelectedText 属性用来获取或设置一个字符串该字符串指示控件中当前选定的文本。只能在代码中使用。11Lines该属性是一个数组属性用来获取或设置文本框控件中的文本行。即文本框中的每一行存放在 Lines数组的一个元素中。12Modified用来获取或设置一个值该值指示自创建文本框控件或上次设置该控件的内容后用户是否修改了该控件的内容。值为true表示修改过值为 false表示没有修改过。13TextLength属性用来获取控件中文本的长度。14WordWrap用来指示多行文本框控件在输入的字符超过一行宽度时是否自动换行到下一行的开始值为 true表示自动换到下一行的开始值为false表示不自动换到下一行的开始。2、常用方法1AppendText方法把一个字符串添加到文件框中文本的后面调用的一般格式如下文本框对象.AppendText(str)参数 str是要添加的字符串。2Clear方法从文本框控件中清除所有文本。调用的一般格式如下文本框对象.Clear()该方法无参数。3Focus方法是为文本框设置焦点。如果焦点设置成功值为 true否则为false。调用的一般格式如下文本框对象.Focus()该方法无参数。4Copy方法将文本框中的当前选定内容复制到剪贴板上。调用的一般格式如下文本框对象.Copy()该方法无参数。5Cut方法将文本框中的当前选定内容移动到剪贴板上。调用的一般格式如下文本框对象.Cut()该方法无参数。6Paste方法用剪贴板的内容替换文本框中的当前选定内容。调用的一般格式如下文本框对象.Paste()该方法无参数。7Undo 方法撤销文本框中的上一个编辑操作。调用的一般格式如下文本框对象.Undo()该方法无参数。8ClearUndo方法从该文本框的撤销缓冲区中清除关于最近操作的信息根据应用程序的状态可以使用此方法防止重复执行撤销操作。调用的一般格式如下文本框对象.ClearUndo()该方法无参数。9Select方法用来在文本框中设置选定文本。调用的一般格式如下文本框对象.Select(start,length)该方法有两个参数第一个参数start用来设定文本框中当前选定文本的第一个字符的位置第二个参数length用来设定要选择的字符数。10SelectAll方法用来选定文本框中的所有文本。调用的一般格式如下文本框对象.SelectAll()该方法无参数。3、常用事件1GotFocus事件该事件在文本框接收焦点时发生。2LostFocus事件该事件在文本框失去焦点时发生。3TextChanged事件该事件在Text属性值更改时发生。无论是通过编程修改还是用户交互更改文本框的 Text属性值均会引发此事件。7.提示框Message.ShowMessage.Show(Text,Title,nType,MessageBoxlcon);参数1弹出框要显示的内容参数2弹出框的标题参数3也可写作MessageBoxButtons弹出框的按钮格式参数4弹出框的图标样式注意 4个参数除了Text外都可以省略Text也可以用输出无内容提示框DialogResult aa MessageBox.Show(Test,aa,MessageBoxButtons.OKCancel,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button2); if (aa DialogResult.OK) { Console.WriteLine(123); } if (aa DialogResult.Cancel) { Console.WriteLine(321); }7.0设计实例登录注册2、常用方法 下面介绍一些窗体的最常用方法。 1Show方法该方法的作用是让窗体显示出来其调用格式为 窗体名.Show(); 其中窗体名是要显示的窗体名称。 2Hide方法该方法的作用是把窗体隐藏出来其调用格式为 窗体名.Hide(); 其中窗体名是要隐藏的窗体名称。 3Close方法该方法的作用是关闭窗体。其调用格式为 窗体名.Close(); 其中窗体名是要关闭的窗体名称。 4ShowDialog方法该方法的作用是将窗体显示为模式对话框。其调用格式为 窗体名.ShowDialog();7.1.Form容器controls属性using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp2 { public partial class Form窗体容器 : Form { public Form窗体容器() { InitializeComponent(); } private void FormLoad(object sender, EventArgs e) { //获取窗体控件数量 Console.WriteLine(this.Controls.Count); Button button new Button(); button.Text 按钮new; button.Location new Point(100, 100); button.Size new Size(100, 50); //添加按钮到窗体 this.Controls.Add(button); Console.WriteLine(this.Controls.Count); //获取按钮在窗体中的索引 Console.WriteLine(this.Controls.IndexOf(button)); Console.WriteLine(this.Controls.IndexOf(button2)); //创建一个 Control类型的数组 Control[] controls new Control[5]; for (int i 0; i 5; i) { Button tempButton new Button(); tempButton.Text 按钮i; tempButton.Location new Point(i*60, 0); tempButton.Size new Size(50, 50); //添加按钮到窗体 controls[i] tempButton; } //批量添加按钮 this.Controls.AddRange(controls); //判断窗体是否包含指定按钮 Console.WriteLine(this.Controls.Contains(button)); //删除指定按钮 this.Controls.Remove(button); //删除指定索引的按钮 this.Controls.RemoveAt(0); //遍历窗体中的所有控件 foreach (Control item in this.Controls) { item.BackColor Color.Red; } foreach (Control item in this.Controls) { if (item is Button) { item.BackColor Color.Green; } else if (item is Label) { item.BackColor Color.Yellow; } } } } }8.panel 容器控件using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp3 { public partial class panel : Form { private object CurrentCount; public panel() { InitializeComponent(); Console.WriteLine(this.Controls.Count); //查看Panel是否是子控件 //foreach (var item in this.Controls) //{ // if (item is Panel) // { // Console.WriteLine(item是Panel); // } //} //把窗体容器中的按钮变色 //foreach (Control item in this.Controls) //{ // if (item is Button) // { // item.BackColor Color.Yellow; // } //} //把panel1中的按钮变色 //foreach (Control item in this.panel1.Controls) //{ // item.BackColor Color.Blue; //} //// 案例:设计一个包含10个按钮的panls 然后根据 让偶数位置元素背景色变化 /// CreatButton(); for (int i 0; i this.panel2.Controls.Count; i) { if (i % 2 0) { this.panel2.Controls[i].BackColor Color.Red; } } CreatButton1(); } public void CreatButton() { for (int i 0; i 10; i) { Button button new Button(); button.Location new Point(i * 50, 0); button.Size new Size(50, 50); button.Text i.ToString(); this.panel2.Controls.Add(button); } } public void CreatButton1() { for (int i 0; i 10; i) { Button button new Button(); button.Location new Point(i * 50, 0); button.Size new Size(50, 50); button.Text i.ToString(); button.Click ButtonClick; button.Tag i; this.panel3.Controls.Add(button); } } //案例:设计一个包含10个按钮的panls 然后根据 点击某个按钮 让颜色变红 其他还原本身 public void ButtonClick(object sender, EventArgs e) { } //创建5个按钮 然后删除 public void button1_Click(object sender, EventArgs e) { //创建5个按钮到按钮区中 for (int i 0; i 5; i) { Button buttonTemp1 new Button(); buttonTemp1.Location new Point(50 * i, 0); buttonTemp1.Name buttonTemp1; buttonTemp1.Size new Size(50, 50); buttonTemp1.Text 按钮1; this.panel4.Controls.Add(buttonTemp1); } } private void button2_Click(object sender, EventArgs e) { //清除所有控件 // this.panel4.Controls.Clear(); //移除某个控件 this.panel4.Controls.RemoveAt(0); } } }9.groupBox 容器控件 类同 panel10.控件布局属性 Anchor和Dock/* 控件的两个通用的布局属性 Anchor: 锚定将控件固定于某个位置 Dock: 停靠将控件停靠在一侧或中央 1.Anchor 为 Top | Right 与父窗口的上边距Top 和右边距 Right 保持不变 2.锚定于左下角 Anchor Left | Bottom 3.锚定于右下角 Anchor Right | Bottom 4.锚定于上边缘、水平拉伸 Anchor Top | Left | Right 5.锚定于上边缘、水平居中 、 Anchor Top 6.拉伸 Anchor Top | Right | Bottom | Left 7. 居中 Anchor: None */ //Dock: 停靠将控件停靠在一侧或中央 //上 Top //下 Bottom //左 Left //右 Right //中 Fill //无 None // 注意当设置 Dock 属性时Anchor属性无效 //布局的嵌套 /* 演示Panel, 面板 1 添加一个Panel停靠在上侧 2 添加一个Panel依靠在左侧 3 添加一个PictureBox依靠在中央 依靠于左右两侧时可以调整宽度上下两侧时可以调整高度。 */11. CheckBox和RadioButton同一个容器里的RadioButton之间是互斥的不同容器间是不相关的。private void radioButton1_Click(object sender, EventArgs e) { isChecked2 false; if (isChecked1false) { MessageBox.Show(radioButton1.Text); isChecked1 true; } } private void radioButton2_Click(object sender, EventArgs e) { isChecked1 false; if (isChecked2 false) { MessageBox.Show(radioButton2.Text); isChecked2 true; } } private void radioButton1_CheckedChanged(object sender, EventArgs e) { if (radioButton1.Checked true) { MessageBox.Show(radioButton1.Text); } } private void radioButton2_CheckedChanged(object sender, EventArgs e) { if (radioButton2.Checked true) { MessageBox.Show(radioButton2.Text); } }CheckBox//多选ListCheckBox listCheckBox new ListCheckBox();public Form1(){InitializeComponent();listCheckBox.Add(checkBox1);listCheckBox.Add(checkBox2);listCheckBox.Add(checkBox3);foreach (CheckBox item in listCheckBox){item.CheckedChanged Ck_CheckedChanged;}}private void quanXuancheckBox_CheckedChanged(object sender, EventArgs e){CheckBox checkBoxQ sender as CheckBox;if (checkBoxQ.Checked true){foreach (CheckBox ck in listCheckBox)ck.Checked true;}else{foreach (CheckBox ck in listCheckBox)ck.Checked false;}}private void Ck_CheckedChanged(object sender, EventArgs e){CheckBox c sender as CheckBox;if (c.Checked true){foreach (CheckBox ch in listCheckBox){if (ch.Checked false)return;}quanXuancheckBox.Checked true;}else{quanXuancheckBox.Checked false;}}//danxuanprivate void radioButton1_CheckedChanged(object sender, EventArgs e){RadioButton radioButton1 sender as RadioButton;if (radioButton1.Checked true) {MessageBox.Show($你是{radioButton1.Text});}}private void radioButton2_CheckedChanged(object sender, EventArgs e){RadioButton radioButton1 sender as RadioButton;if (radioButton1.Checked true){MessageBox.Show($你是{radioButton1.Text});}}}}12.listboxnamespace winform3{/*ListBox是WinForm中的 列表 控件它提供了一个项目列表(一组数据项)用户可以选择一个或者多个条目当列表项目过多时ListBox会自动添加滚动条、使用户可以滚动查阅所有选项.*/using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace WindowsFormsApp4{public partial class Form1 : Form{public Form1(){InitializeComponent();//获取listBox选项个数Console.WriteLine(listBox1.Items.Count);// 获取listBox选项内容foreach (var item in listBox1.Items){Console.WriteLine(item);}//修改listBox1.Items[0] 100;//动态添加元素listBox1.Items.Add(1000);//移除listBox1.Items.RemoveAt(0);//清空// listBox1.Items.Clear();//包含某个元素Console.WriteLine(listBox1.Items.Contains(1000));Console.WriteLine(comboBox1.Items.Count);//其他同上}//单选移动private void button5_Click(object sender, EventArgs e){// Console.WriteLine(listBox1.SelectedItem);if (listBox1.SelectedItem null){MessageBox.Show(未选中);}else {listBox2.Items.Add(listBox1.SelectedItem);listBox1.Items.Remove(listBox1.SelectedItem);}}//多选移动 selectionMode属性 控制 多选操作private void button6_Click(object sender, EventArgs e){if (listBox2.SelectedItems.Count 0){MessageBox.Show(未选中);}else {foreach (var item in listBox2.SelectedItems){listBox1.Items.Add(item);}// 0 - 3// 1 - 2// 2 - 1for (int i listBox2.SelectedItems.Count; i 0 ; i--){listBox2.Items.Remove(listBox2.SelectedItems[i-1]);}}}//listBox1属性更改时private void listBox1_SelectedIndexChanged(object sender, EventArgs e){MessageBox.Show(listBox1.SelectedItem.ToString());}}namespace winform3 { /* ListBox是WinForm中的 列表 控件它提供了一个项目列表(一组数据项) 用户可以选择一个或者多个条目当列表项目过多时ListBox会自动添加滚动条、 使用户可以滚动查阅所有选项. */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); //获取listBox选项个数 Console.WriteLine(listBox1.Items.Count); // 获取listBox选项内容 foreach (var item in listBox1.Items) { Console.WriteLine(item); } //修改 listBox1.Items[0] 100; //动态添加元素 listBox1.Items.Add(1000); //移除 listBox1.Items.RemoveAt(0); //清空 // listBox1.Items.Clear(); //包含某个元素 Console.WriteLine(listBox1.Items.Contains(1000)); Console.WriteLine(comboBox1.Items.Count); //其他同上 } //单选移动 private void button5_Click(object sender, EventArgs e) { // Console.WriteLine(listBox1.SelectedItem); if (listBox1.SelectedItem null) { MessageBox.Show(未选中); } else { listBox2.Items.Add(listBox1.SelectedItem); listBox1.Items.Remove(listBox1.SelectedItem); } } //多选移动 selectionMode属性 控制 多选操作 private void button6_Click(object sender, EventArgs e) { if (listBox2.SelectedItems.Count 0) { MessageBox.Show(未选中); } else { foreach (var item in listBox2.SelectedItems) { listBox1.Items.Add(item); } // 0 - 3 // 1 - 2 // 2 - 1 for (int i listBox2.SelectedItems.Count; i 0 ; i--) { listBox2.Items.Remove(listBox2.SelectedItems[i-1]); } } } //listBox1属性更改时 private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { MessageBox.Show(listBox1.SelectedItem.ToString()); } }13.comboBoxnamespace winfromComBox4{public partial class Form1 : Form{public Form1(){InitializeComponent();this.comboBox1.Text 请选择;comboBox1.Items.Add(1);comboBox1.Items.Add(2);comboBox1.Items.Add(3);comboBox1.Items.Add(4);comboBox1.Items.Add(5);comboBox1.Items.Add(6);comboBox1.Items.Insert(1, wangwu);//总结comboBox1.Items和 listBox1.Items 都是集合 而且都是相同类型 所以方法和属性通用// comboBox1.SelectedIndex 0;//this.comboBox1.SelectedItem.ToString();//当前选择的Item的显示值}private void comboBox1_SelectedIndexChanged(object sender, EventArgs e){//MessageBox.Show(Test);MessageBox.Show(this.comboBox1.SelectedItem.ToString());//当前选择的Item的显示值);}}14.PictureBoxPictureBox在窗体显示图片属性:Image:设置图片,使用项目资源文件的方式导入图片.SizeMode:设置图片的显示模式,StretchImage图片随控件大小平铺.Zoom按照等比例缩放随控件大小平铺.AutoSize控件随图片大小而定.using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;/*https://img0.baidu.com/it/u2642223955,2176778396fm253fmtautoapp138fJPEG?w889h500https://pic.616pic.com/photoone/00/03/96/618ce5441d6a75161.jpghttps://img0.baidu.com/it/u3059223957,798715771fm253fmtautoapp138fJPEG?w667h500*/namespace zhiyou_winform_PictureBox{public partial class Form1 : Form{//创建 保存图片的list数组private Liststring picUrlList new Liststring();//记录 当前图片的位置索引值private int count 0;public Form1(){InitializeComponent();//绝对路径//picUrlList.Add(C:\Users\renre\Desktop\CSharp\winform1\zhiyou_winform_PictureBox\Resources\1.jpg);//picUrlList.Add(C:\Users\renre\Desktop\CSharp\winform1\zhiyou_winform_PictureBox\Resources\2.jpg);//picUrlList.Add(C:\Users\renre\Desktop\CSharp\winform1\zhiyou_winform_PictureBox\Resources\3.jpg);//picUrlList.Add(C:\Users\renre\Desktop\CSharp\winform1\zhiyou_winform_PictureBox\Resources\4.jpg);//相对路径//保存图片的本地路径picUrlList.Add(..\..\resources\1.jpg);picUrlList.Add(..\..\resources\2.jpg);picUrlList.Add(..\..\resources\3.jpg);picUrlList.Add(..\..\resources\4.jpg);}//下一张private void button1_Click(object sender, EventArgs e){//查看项目的debug目录//C:\Users\renre\Desktop\CSharp\winform1\zhiyou_winform_PictureBox\bin\Debugstring path Application.StartupPath;Console.WriteLine(path);if (picUrlList.Count-1 count){count 0;// pictureBox1.ImageLocation picUrlList[count];pictureBox1.Image Image.FromFile(picUrlList[count]);}else{pictureBox1.SizeMode PictureBoxSizeMode.StretchImage;//方式1// pictureBox1.ImageLocation picUrlList[count];//方式2pictureBox1.Image Image.FromFile(picUrlList[count]);}count;}//上一张private void button2_Click(object sender, EventArgs e){if (count0){count picUrlList.Count-1;// pictureBox1.ImageLocation picUrlList[count];pictureBox1.Image Image.FromFile(picUrlList[count]);}else{pictureBox1.SizeMode PictureBoxSizeMode.StretchImage;//方式1// pictureBox1.ImageLocation picUrlList[count];//方式2pictureBox1.Image Image.FromFile(picUrlList[count]);}count--;}}}15.TabControl导航控制条using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace zhiyou_winform_TabControl{/*MultiLine是否允许多行选项卡AlignMent控制选项卡的显示位置可以在TabControl的上、下、左、右显示Appearance控制选项卡的外观ItemSize控制选项卡的高度和宽度注意不是TabControl的宽和高ItemSize的宽度设置后还需要设置SizeMode否则选项卡的宽度不变只有高度随设定的高度调整*/public partial class Form1 : Form{public Form1(){InitializeComponent();//添加一个选项AddTabPage();//设置当前选项卡默认的选项tabControl1.SelectedIndex 2;//选项卡绑定触发事件tabControl1.TabPages[0].Enter tabPage1_Event;tabControl1.TabPages[1].Enter tabPage2_Event;//tabControl绑定事件tabControl1.Selected TabControlEventHandler;}//手动添加选项卡private void AddTabPage(){// 通过代码添加一个简单TabPageTabPage tab1 new TabPage();tab1.Text 选项卡;tab1.Name tp1;tab1.BackColor Color.Orange;if (!tabControl1.TabPages.ContainsKey(tab1.Name))//如果有就不再添加{tabControl1.TabPages.Add(tab1);}}//手动删除选项卡private void RemoveTabPage() {tabControl1.TabPages.Remove(tabPage1);//方法1tabControl1.TabPages.RemoveAt(1);//方法2tabControl1.TabPages.RemoveByKey(用户管理);//方法3}private void tabPages() {//所有选项卡的集合// tabControl1.TabPages//某个选项卡的集合 可以找到对应选项卡里的子元素TabPage firstTabPage tabControl1.TabPages[0];Console.WriteLine(firstTabPage.Controls.Count);}private void SelectTabPage() {//设置选中页的三种方法tabControl1.SelectedIndex 0;// tabControl1.SelectedTab tabPage1;// tabControl1.SelectTab(tabPage1);}private void tabPage1_Event(object sender, EventArgs e){MessageBox.Show(111);}private void tabPage2_Event(object sender, EventArgs e){MessageBox.Show(222);}public void TabControlEventHandler(object sender, TabControlEventArgs e){MessageBox.Show(TabControl);}}}