ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
1、点击button按钮,选中“属性”中的闪电标志,输入click的动作名字(假如命名为OnClick), ![](https://ws3.sinaimg.cn/large/006tKfTcgy1fr1dvszv7oj30n50cognn.jpg) 2、双击click处,在弹出的`Form1.cs`中输入以下第三行代码。得到如图结果。 ``` private void OnClick(object sender, EventArgs e) { MessageBox.Show("确认提交", "应用程序", MessageBoxButtons.OK, MessageBoxIcon.Information); } ``` ![](https://ws3.sinaimg.cn/large/006tKfTcgy1fr1dw9y9hqj30l908zt95.jpg) 3、在app中,默认中 `Program.cs`中的`static void Main()`开始运行。所以如果要更换启动窗口,则可以在该位置更换其他窗口form的名字。 ![](https://ws3.sinaimg.cn/large/006tKfTcgy1fr1dwn2qydj30md0c2mz6.jpg) 4、需要注意的是,上述动作被记录在`Form1.cs`文件中的 `private void InitializeComponent()`中,不要随意改动。 5、同一个窗体应用中要设置先启动哪一个?: 启动窗体 比如在EMS-windows-app这个应用中,有很多窗体,想先启动“审核登记”。这时候找到 EMS-windows-app 里面的 Program.cs,修改其中的代码, 改成“new 审核登记()”即可: `Application.Run(new 审核登记( ) );` ![](https://ws3.sinaimg.cn/large/006tKfTcgy1fs819n5q54j305n0850t5.jpg) ![](https://ws3.sinaimg.cn/large/006tKfTcgy1fs818jnn87j30n509ywgi.jpg) # 设置窗体属性 ![](https://ws4.sinaimg.cn/large/006tNc79gy1fs1s3z1zxlj30fv07g0ta.jpg) # 5、form 窗体是一个对象,所以在编辑按钮时,需要创建对象。 # 窗体事件 ![](https://ws4.sinaimg.cn/large/006tNc79gy1fs1tj5usqlj30hj07ugm9.jpg) ## 父窗体、子窗体 也就是单击打开第二个窗体以后第二个窗体总在第一个窗体内显示 父窗体:`isMdiContainer` 属性为 true 即可。 子窗体:父窗体设置完成后,“属性”中无法直接设置子窗体,需要通过编写代码设置`MdiParent`属性。代码: ``` Form2 frm2 = new Form2(); frm2.Show(); //显示窗体 frm2.MdiParent = this; //注意不要写成midparent ``` ## 平铺窗口: ``` layoutMdi( Mdilayout. ) ``` arrangelcons => cascade => titlehorizontal => 水平平铺 titlevertical => 垂直平铺 ## button 控件点击后生成新 txt 输入框: ``` private void button1_Click_1(object sender, EventArgs e)//用于点击时生成一个txt框 { TextBox txt = new TextBox(); //创建对象txt txt.Location = new Point(0, 0);//初始化坐标位置 this.Controls.Add(txt); // this.Controls指的是button这个控件,触发的事件是add新增 } ``` ## TextBox 控件 只读文本、密码、多行 ![](https://ws2.sinaimg.cn/large/006tNc79gy1fs2ahl9ppdj30g6073dgo.jpg) 默认只能水平改变大小,这时候把multiline属性设置为 true(多行),即可垂直方向改变大小。 passwordChart属性中,可以将输入的密码显示成**星号。 可以直接拖动创建,也可以写代码: ``` private void button1\_Click(object sender, EventArgs e) { TextBox txt = new TextBox(); this.Controls.Add(txt); //this.control的意思是,这个 button 控件中添加。 } ``` ![](https://box.kancloud.cn/80cb3bc52e9f0ca15064e037d5d912ba_394x254.png) ## radioButton 单选。一般用 check 属性: ![](https://ws1.sinaimg.cn/large/006tNc79gy1fs2b9a2zuqj30gs07raas.jpg) ``` private void radioButton_admin_CheckedChanged(object sender, EventArgs e) { if(radioButton_admin.Checked) { MessageBox.Show ("以管理员身份登录"); } } ``` ![](https://ws2.sinaimg.cn/large/006tNc79gy1fs2b72y63jj30mk07tjsb.jpg) ## checkBox 复选 ![](https://ws3.sinaimg.cn/large/006tNc79gy1fs2b9xzmtdj30f006yaak.jpg) ## Rich TextBox——超链接 > 如果要设置 richtextbox 控件的字体样式等,可以在 form 窗口的 load 中编写代码。 ![](https://ws4.sinaimg.cn/large/006tKfTcgy1fs4qsh71a7j30jn086mye.jpg) 滚动条、字体属性、超链接、段落格式 ### 1、设置字体样式 ``` private void 审核意见richTextBox1_TextChanged(object sender, EventArgs e) { 审核意见richTextBox1.SelectionFont = new Font("宋体", 15, FontStyle.Bold); 审核意见richTextBox1.SelectionColor = Color.Red; } ``` ### 2、设置超链接 在 richtextbox (这个名字是控件的名字)中输入以下代码: ``` richtextbox.Text = "http://www.baidu.com"; ``` 在转到属性-事件中,找到 LinkClicked,双击,再输入以下代码: ``` System.Diagnostics.Process.Start(e.LinkText); // 该命名空间是系统设置好的 ``` ![](https://ws2.sinaimg.cn/large/006tKfTcly1fs4s4e8kimj30iu08y754.jpg) ![](https://ws2.sinaimg.cn/large/006tKfTcgy1fs4scn9eszj30f707mq3c.jpg) ## comboBox控件 dropdownStyle属性 selectedValueChanged事件 ![](https://box.kancloud.cn/8903cbb5482320190b55961c8fabbf38_551x256.png) 错误: 若输入`messageBox.Show(comboBox1. selectedItem)`; 参数1:无法从“object”转换为“string”。可直接改成: `MessageBox.Show("岗位:" + comboBox1.SelectedItem);` 即可。 ## listBox ![](https://box.kancloud.cn/294217d09eed895d13f7d7686888fd40_694x340.png) listBox.items.add 属性 listBox.items.remove 属性 selectionMode: one \ multisimple \ multiextended ( 意思是用户可以用 shift 或 control 键盘来选中) ## GroupBox 分组 将“密码、登录、退出”等直接拖动到新建的“系统登录”框中,就变成一个组了。 ![](https://box.kancloud.cn/f9e21e77c3d097d188d3e185bfa761ef_474x181.png) ![](https://box.kancloud.cn/96bba143e314a99bb019354e3ce36196_658x432.png) ## listview 列表试图空间。用于显示带图标的项的列表。类似 windows 资源管理器右边窗口的用户界面。 ![](https://box.kancloud.cn/007b2db26e2d1731f46ce8b20c808092_609x288.png) 其中smallimagelist、largeimagelist、stageimagelist属性与imageList 组件——图片存储 一起搭配使用。 ![](https://box.kancloud.cn/3568671113276a57c530b5b9ef3fc842_803x412.png) 直接在items里面添加图片即可。 先设置group属性,然后双击 form 窗口,在 load 里面添加代码。 ![](https://box.kancloud.cn/78e531cffddbd3b2a84aee292c453095_648x251.png) 代码如下:将设置好的组件里面的元素进行分类。 ![](https://box.kancloud.cn/4f1f1937c34f43a83ef935af8daf7f5d_817x468.png) ## treeview控件 树形结构 ### 新建元素 ![](https://ws3.sinaimg.cn/large/006tNc79gy1fs9ycfsiafj30fg068js1.jpg) 可直接在属性中设置,也可以在 form 窗口的 load 代码区输入: 新建对象 ``` private void 审核登记_Load_1(object sender, EventArgs e) { TreeNode tr1 = treeView1.Nodes.Add("总工办"); } ``` 删除的话,可以用 remove。 ### 获取信息: 双击控件,在afterSelect 中输入 ``` MessageBox.Show(e.Node.Text); //这里的 e 就是上一行的 e ``` ![](https://ws2.sinaimg.cn/large/006tNc79gy1fs9yr2l6e6j30jk08hq3m.jpg) > node其实就是属性中的 Nodes(如图1)。点击后进行Node相关设置,如图2。所以如果将代码中的 Node.Text 换成 Node.Tag,则会显示我们在 Tag 中填入的“备注”(如图3)。 >![](https://ws1.sinaimg.cn/large/006tNc79gy1fs9yzop6nqj30nl0ao0uj.jpg) > >![](https://ws1.sinaimg.cn/large/006tNc79gy1fs9z0m359rj30k60dh0ub.jpg) > >![](https://ws2.sinaimg.cn/large/006tNc79gy1fs9z3is5h8j30il08odgi.jpg) ### 添加图标: imageList属性设置为 true。然后与imageList组件配合。 ## Timer组件 计时器,时间间隔以interval属性定义,以毫秒为单位。 ![](https://ws4.sinaimg.cn/large/006tNc79gy1fs9za8qi4qj30io09575q.jpg) Enabled 与 start 和 stop 互相替换。enable 为 true,就等于 start。 ![](https://ws1.sinaimg.cn/large/006tNc79gy1fs9zciif91j30dy0870t4.jpg) ![](https://ws3.sinaimg.cn/large/006tNc79gy1fsbklgz75pj30mt0esac9.jpg) ![](https://ws4.sinaimg.cn/large/006tNc79gy1fsbkm7i972j30tb0c20ur.jpg) ## menustrip ![](https://ws2.sinaimg.cn/large/006tNc79gy1fsbpbk086lj30gz07qmxu.jpg) 菜单栏。如果要设置“文件”快捷菜单,在属性=>text => “文件(&F)”即可。 ![](https://box.kancloud.cn/a1e0b77eb8444f55225e1605d30549ef_243x366.png) ## toolstrip 工具栏。默认添加到 menustrip 下面。 可以显示图标和可以显示文字,右击,在 display 选项中进行设置。 ![](https://ws2.sinaimg.cn/large/006tNc79gy1fsbpnpdra9j30ff0753yy.jpg) ![](https://ws2.sinaimg.cn/large/006tNc79gy1fsbpkzfg7zj30cd0c9gmv.jpg) ## statusStrip 状态栏。 ![](https://ws4.sinaimg.cn/large/006tNc79gy1fsbpnwkabzj30g007i0t6.jpg) 比如添加时间:在 form 窗体的 load 语句中,添加: ``` toolStripStatusLabel1.Text = dateTime.Now.ToString(); ``` ![](https://ws2.sinaimg.cn/large/006tNc79gy1fsbpyv8x5oj30dy09gaas.jpg) ## 消息框 messagebox 可以通过提示来一步步设置。 ![](https://ws2.sinaimg.cn/large/006tNc79gy1fsbqbcxj9rj30k00720tu.jpg) ## 打开对话框 控件 openFileDialog 指定打开位置:`openFileDialog.InitialDirectory = "C:\\";` 指定文件类型:`openFileDialog.Filter = "文本文件(*.txt)|*.txt|Word文件(*.doc)|*.doc"` 若要所有格式:`openFileDialog.Filter = "所有文件(*.*)|*.*"` ![](https://ws2.sinaimg.cn/large/006tNc79gy1fsjkoy5s72j30jc09m75f.jpg) ![](https://ws1.sinaimg.cn/large/006tNc79gy1fsjl0t2o31j30fb063js9.jpg) > 最后记得写上 openFile.Dialog.ShowDialog(); ## 另存为对话框 控件 saveFileDialog 同上 ![](https://ws2.sinaimg.cn/large/006tNc79gy1fsjkrj613mj30ik08k753.jpg) ## 浏览文件夹 控件 foldBrowserDialog ![](https://ws1.sinaimg.cn/large/006tNc79gy1fsjl5l86rgj30j0098js8.jpg) `textBox.Text = folderBrowserDialog1.SelectedPath;` # 使用右击快捷键功能 添加contextMenustrip 控件,并在 datagridview 的属性中把contextmenustrip控件关联上。