每一个类中都有一个最重要的成员
1. Lable控件(标签控件)
2. LinkLable控件(超链接标签控件)

1. 属性-linkArea
//Identify what the first Link is.this. linkLabel1. LinkArea = new system. windows. Forms. LinkArea(o, 8);
4.属性-改变字体大小
this.linkLabel2.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F);
2. 方法-link.add() 超链接分成几个部分
public Form1(){InitializeComponent();//一个超链接分段 可以实现两个功能linkLabel1.Links.Add(0, 7, @"I:\研究生\选修和必修课程");linkLabel1.Links.Add(8, 14, @"I:\研究生\选修和必修课程\高等渗流力学");}
3.click事件— 链接地址:网页 文件夹 应用程序
//网页System.Diagnostics.Process.Start("http://cugb.edu.cn");// 文件夹System.Diagnostics.Process.Start(@"I:\校biao\班长");//应用程序System.Diagnostics.Process.Start(@"I:\功能强大的小软件\天若OCR文字识别V4.48\天若OCR开源版V5.0.0\天若OCR文字识别.exe");
3. TextBox控件(文本框控件)
4. RichTextBox控件(富文本框控件)
1. 常用方法:
文本框和富文本框.zip
1. 1. loadfile(文件名,文件类型)
try{fileName = Convert.ToString(textBox1.Text); //文件名 为文本框内容richTextBox1.LoadFile(fileName, RichTextBoxStreamType.RichText); // 文件名(绝对地址) 文件类型}catch (Exception e1){MessageBox.Show(e1.Message,"error");throw;}
1. 2. savefile(文件名,文件类型)
if (richTextBox1.Modified == true) //如果文档的内容发生了变化{fileName = textBox1.Text;richTextBox1.SaveFile(fileName, RichTextBoxStreamType.RichText);richTextBox1.Modified = false;MessageBox.Show("已保存", "保存文件");richTextBox1.Focus(); //为richTextBox1设置焦点}
1.3 find(str,start,option)
//查找string findcontx = textBox2.Text;// 查找内容start = richTextBox1.Find(findcontx, start, RichTextBoxFinds.MatchCase); // 区分大小写if (start==-1){start = 0;MessageBox.Show("查找结束", "查找",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);}//===================================================================================================private void button4_Click(object sender, EventArgs e){// 替换string changeText = textBox3.Text; //替换的文本string oriText = textBox2.Text; //查找到 内容start = richTextBox1.Find(oriText, 0, RichTextBoxFinds.MatchCase); //从头开始查找while (start!=-1){richTextBox1.SelectedRtf = changeText; //被选中的文本 替换start += changeText.Length; //下一次查找的起点start = richTextBox1.Find(oriText, start, RichTextBoxFinds.MatchCase); //继续查找}MessageBox.Show("全部替换完成", "文本替换");start = 0;}
1.4 focus
richTextBox1.Focus();
2. 常用属性:
1. selectioncolor 选中文本的颜色
richTextBox1.SelectionColor = Color.Red; //选中文本的颜色
2. selectionFont 选中文本的字体
richTextBox1.SelectionFont = new Font("隶书", 18, FontStyle.Bold); // 选中文本的字体
3. SelectionLength 长度
start += richTextBox1.SelectionLength; //属性:选中文本的长度
Modified 控件中内容发生变化
5. SelectedText 返回被选中的文本
6. SelectionStart 本选中文本的开始位置5. PictureBox控件(图片框控件)
小计算器
运算器-合并事件版.zip
https://www.jb51.net/article/83999.htm ```csharpprivate double x = 0; //字段用来存储第一个值private double y = 0; //字段用来存储第一个值private string flag = null; ////字段用来存储运算符private double res = 0; //字段用来存储最终结果
private void button11_Click(object sender, EventArgs e)
{// 输入数字,文本框的字符串边长textBox4.Text += button11.Text;}
private void button12_Click(object sender, EventArgs e)
{//开始计算 + 存储第一个数,清空文本框内容,记录计算符号x = Convert.ToDouble(textBox4.Text);textBox4.Text = "";flag = button12.Text;}
private void button16_Click(object sender, EventArgs e)
{Count();}private void Count(){y = Convert.ToDouble(textBox4.Text);textBox4.Text = "";switch (flag){case "+":res = x + y;break; //必须有case "-":res = x - y;break;case "*":res = x * y;break;case "/":try{res = x / y;}catch (Exception){throw;}break;default:break;}textBox4.Text = Convert.ToString(res);}
================================================================= // 0-9 数字按钮激发的事件是一回事
this.button1.Click += new System.EventHandler(this.button1_Click); this.button2.Click += new System.EventHandler(this.button1_Click); //同理,+-*/ 的事件也是一回事
//点击数字时 private void button1_Click(object sender, EventArgs e) { Button bt = (Button)sender; // sender为事件的激发者 textBox1.Text += bt.Text; }
//点击运算符时private void button11_Click(object sender, EventArgs e){Button but = (Button)sender;textBox1.Text += " " + but.Text +" "; // 太棒了,空格的妙用 这样方便查找运算符// textBox1.Text += " " + btn.Text + " ";}private void button16_Click(object sender, EventArgs e){string str1 = null;string str2 = null;int a = 0;int b = 0;int res = 0;try{string text = textBox1.Text; // 用字符串存下来就是处理字符串了int space = text.IndexOf(' '); //查找字符串 空格 的位置str1 = text.Substring(0, space); // 截取 0-空格 这一段的字符串 获得第一个操作数char op = Convert.ToChar(text.Substring(space + 1, 1)); // 获得运算符str2 = text.Substring(space + 3); //截取 第二个空格 之后的字符串 获得第二个操作数a = Convert.ToInt32(str1);b = Convert.ToInt32(str2);switch (op){case '+': res = a + b; break;case '-': res = a - b;break;case '*': res =a*b ; break;case '÷': res = a/b; break;default:break;}textBox1.Text = Convert.ToString(res);}catch (Exception){throw;}}
添加热键
在大家给button,label,menuStrip等控件设置Text属性时在名字后边加&键名就可以了,比如button1.text=”确定(80)”,就会有快捷键了,这时候按Alt+0就可以执行按钮单击事件。
属性:autosize,自动调整控件大小 button(默认为false) label(默认是true) picturebox 都有这个属性

