Form1.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace WindowsFormsPartial
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. partial class account //分部类第一部分
  19. {
  20. public int addition(int a, int b) //创建一个整型方法
  21. {
  22. return a + b; //方法中的加法运算
  23. }
  24. }
  25. partial class account //分部类第二部分
  26. {
  27. public int multiplication(int a, int b) //创建一个整型方法
  28. {
  29. return a * b; //方法中的乘法运算
  30. }
  31. }
  32. partial class account //减法
  33. {
  34. public int subtration(int a, int b)
  35. {
  36. return a - b;
  37. }
  38. }
  39. partial class account //除法
  40. {
  41. public int division(int a, int b)
  42. {
  43. return a / b;
  44. }
  45. }
  46. private void Form1_Load(object sender, EventArgs e)
  47. {
  48. comboBox1.SelectedIndex = 0;
  49. comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
  50. }
  51. private void button1_Click(object sender, EventArgs e)
  52. {
  53. try
  54. {
  55. //创建分部类的实例
  56. account at = new account();
  57. //读取两个运算数,并转换为整型
  58. int M = int.Parse(txtNo1.Text.Trim());
  59. int N = int.Parse(txtNo2.Text.Trim());
  60. //读取ComboBox中的运算符
  61. string str = comboBox1.Text;
  62. switch (str)
  63. {
  64. case "加": txtResult.Text = at.addition(M, N).ToString(); break;//调用加法
  65. case "减": txtResult.Text = at.subtration(M, N).ToString(); break;//调用减法
  66. case "乘": txtResult.Text = at.multiplication(M, N).ToString(); break;//调用乘法
  67. case "除": txtResult.Text = at.division(M, N).ToString(); break;//调用除法
  68. }
  69. }
  70. catch (Exception ex)
  71. {
  72. MessageBox.Show(ex.Message);
  73. }
  74. }
  75. }
  76. }