每一个类中都有一个最重要的成员

1. Lable控件(标签控件)

image.png

2. LinkLable控件(超链接标签控件)

image.png

1. 属性-linkArea

  1. //Identify what the first Link is.
  2. this. linkLabel1. LinkArea = new system. windows. Forms. LinkArea(o, 8);

4.属性-改变字体大小

  1. this.linkLabel2.Font = new System.Drawing.Font("Microsoft Sans Serif", 11F);

2. 方法-link.add() 超链接分成几个部分

  1. public Form1()
  2. {
  3. InitializeComponent();
  4. //一个超链接分段 可以实现两个功能
  5. linkLabel1.Links.Add(0, 7, @"I:\研究生\选修和必修课程");
  6. linkLabel1.Links.Add(8, 14, @"I:\研究生\选修和必修课程\高等渗流力学");
  7. }

3.click事件— 链接地址:网页 文件夹 应用程序

  1. //网页
  2. System.Diagnostics.Process.Start("http://cugb.edu.cn");
  3. // 文件夹
  4. System.Diagnostics.Process.Start(@"I:\校biao\班长");
  5. //应用程序
  6. System.Diagnostics.Process.Start(@"I:\功能强大的小软件\天若OCR文字识别V4.48\天若OCR开源版V5.0.0\天若OCR文字识别.exe");

3. TextBox控件(文本框控件)

4. RichTextBox控件(富文本框控件)

1. 常用方法:

文本框和富文本框.zip

1. 1. loadfile(文件名,文件类型)

  1. try
  2. {
  3. fileName = Convert.ToString(textBox1.Text); //文件名 为文本框内容
  4. richTextBox1.LoadFile(fileName, RichTextBoxStreamType.RichText); // 文件名(绝对地址) 文件类型
  5. }
  6. catch (Exception e1)
  7. {
  8. MessageBox.Show(e1.Message,"error");
  9. throw;
  10. }

1. 2. savefile(文件名,文件类型)

  1. if (richTextBox1.Modified == true) //如果文档的内容发生了变化
  2. {
  3. fileName = textBox1.Text;
  4. richTextBox1.SaveFile(fileName, RichTextBoxStreamType.RichText);
  5. richTextBox1.Modified = false;
  6. MessageBox.Show("已保存", "保存文件");
  7. richTextBox1.Focus(); //为richTextBox1设置焦点
  8. }

1.3 find(str,start,option)

  1. //查找
  2. string findcontx = textBox2.Text;// 查找内容
  3. start = richTextBox1.Find(findcontx, start, RichTextBoxFinds.MatchCase); // 区分大小写
  4. if (start==-1)
  5. {
  6. start = 0;
  7. MessageBox.Show("查找结束", "查找",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
  8. }
  9. //===================================================================================================
  10. private void button4_Click(object sender, EventArgs e)
  11. {
  12. // 替换
  13. string changeText = textBox3.Text; //替换的文本
  14. string oriText = textBox2.Text; //查找到 内容
  15. start = richTextBox1.Find(oriText, 0, RichTextBoxFinds.MatchCase); //从头开始查找
  16. while (start!=-1)
  17. {
  18. richTextBox1.SelectedRtf = changeText; //被选中的文本 替换
  19. start += changeText.Length; //下一次查找的起点
  20. start = richTextBox1.Find(oriText, start, RichTextBoxFinds.MatchCase); //继续查找
  21. }
  22. MessageBox.Show("全部替换完成", "文本替换");
  23. start = 0;
  24. }

1.4 focus

  1. richTextBox1.Focus();

2. 常用属性:

1. selectioncolor 选中文本的颜色

  1. richTextBox1.SelectionColor = Color.Red; //选中文本的颜色

2. selectionFont 选中文本的字体

  1. richTextBox1.SelectionFont = new Font("隶书", 18, FontStyle.Bold); // 选中文本的字体

3. SelectionLength 长度

  1. start += richTextBox1.SelectionLength; //属性:选中文本的长度
  1. Modified 控件中内容发生变化
    5. SelectedText 返回被选中的文本
    6. SelectionStart 本选中文本的开始位置

    5. PictureBox控件(图片框控件)


    小计算器

    运算器-合并事件版.zip
    https://www.jb51.net/article/83999.htm ```csharp

    1. private double x = 0; //字段用来存储第一个值
    2. private double y = 0; //字段用来存储第一个值
    3. private string flag = null; ////字段用来存储运算符
    4. private double res = 0; //字段用来存储最终结果

    private void button11_Click(object sender, EventArgs e)

    1. {
    2. // 输入数字,文本框的字符串边长
    3. textBox4.Text += button11.Text;
    4. }

    private void button12_Click(object sender, EventArgs e)

    1. {
    2. //开始计算 + 存储第一个数,清空文本框内容,记录计算符号
    3. x = Convert.ToDouble(textBox4.Text);
    4. textBox4.Text = "";
    5. flag = button12.Text;
    6. }

    private void button16_Click(object sender, EventArgs e)

    1. {
    2. Count();
    3. }
    4. private void Count()
    5. {
    6. y = Convert.ToDouble(textBox4.Text);
    7. textBox4.Text = "";
    8. switch (flag)
    9. {
    10. case "+":
    11. res = x + y;
    12. break; //必须有
    13. case "-":
    14. res = x - y;
    15. break;
    16. case "*":
    17. res = x * y;
    18. break;
    19. case "/":
    20. try
    21. {
    22. res = x / y;
    23. }
    24. catch (Exception)
    25. {
    26. throw;
    27. }
    28. break;
    29. default:
    30. break;
    31. }
    32. textBox4.Text = Convert.ToString(res);
    33. }

    ================================================================= // 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; }

  1. //点击运算符时
  2. private void button11_Click(object sender, EventArgs e)
  3. {
  4. Button but = (Button)sender;
  5. textBox1.Text += " " + but.Text +" "; // 太棒了,空格的妙用 这样方便查找运算符
  6. // textBox1.Text += " " + btn.Text + " ";
  7. }
  8. private void button16_Click(object sender, EventArgs e)
  9. {
  10. string str1 = null;
  11. string str2 = null;
  12. int a = 0;
  13. int b = 0;
  14. int res = 0;
  15. try
  16. {
  17. string text = textBox1.Text; // 用字符串存下来就是处理字符串了
  18. int space = text.IndexOf(' '); //查找字符串 空格 的位置
  19. str1 = text.Substring(0, space); // 截取 0-空格 这一段的字符串 获得第一个操作数
  20. char op = Convert.ToChar(text.Substring(space + 1, 1)); // 获得运算符
  21. str2 = text.Substring(space + 3); //截取 第二个空格 之后的字符串 获得第二个操作数
  22. a = Convert.ToInt32(str1);
  23. b = Convert.ToInt32(str2);
  24. switch (op)
  25. {
  26. case '+': res = a + b; break;
  27. case '-': res = a - b;break;
  28. case '*': res =a*b ; break;
  29. case '÷': res = a/b; break;
  30. default:
  31. break;
  32. }
  33. textBox1.Text = Convert.ToString(res);
  34. }
  35. catch (Exception)
  36. {
  37. throw;
  38. }
  39. }

```

image.png

image.png

添加热键

在大家给button,label,menuStrip等控件设置Text属性时在名字后边加&键名就可以了,比如button1.text=”确定(80)”,就会有快捷键了,这时候按Alt+0就可以执行按钮单击事件。

属性:autosize,自动调整控件大小 button(默认为false) label(默认是true) picturebox 都有这个属性