C#动态生成简便计算器
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.Windows.Forms; 9 10 namespace 计算器 11 { 12 public partial class Form2 : Form 13 { 14 public Form2() 15 { 16 InitializeComponent(); 17 } 18 Point p; 19 Point pp; 20 private void Form2_Load(object sender, EventArgs e) 21 { 22 Button btn; 23 Label lbl = new Label(); 24 lbl.Name = "lblname"; 25 lbl.Location = new Point(50,10);//定位置 26 lbl.Size = new Size(250,30);//定尺寸 27 TextBox txt = new TextBox(); 28 txt.Name = "txtName"; 29 txt.BorderStyle = BorderStyle.FixedSingle;//设置样式 30 txt.Location = new Point(50,30); 31 txt.Size = new Size(250,50); 32 txt.Multiline = true;//设置为多行文本框 33 Button btn0 = new Button(); 34 btn0.SetBounds(p.X+50,p.Y+230,150,50);//设置位置和尺寸 35 btn0.Tag = 0; 36 btn0.Visible = true; 37 btn0.Text = 0 + ""; 38 btn0.Click += new EventHandler(btn_Click);//手动添加事件 39 Button btnJ = new Button(); 40 btnJ.SetBounds(p.X+200,p.Y+80,50,50); 41 btnJ.Tag = "+"; 42 btnJ.Text = "+"; 43 btnJ.Click += new EventHandler(btn_Click); 44 Button btnJan = new Button(); 45 btnJan.SetBounds(p.X + 200, p.Y + 130, 50, 50); 46 btnJan.Tag = "-"; 47 btnJan.Text = "-"; 48 btnJan.Click += new EventHandler(btn_Click); 49 Button btnC = new Button(); 50 btnC.SetBounds(p.X + 200, p.Y + 180, 50, 50); 51 btnC.Tag = "*"; 52 btnC.Text = "*"; 53 btnC.Click += new EventHandler(btn_Click); 54 Button btnChu = new Button(); 55 btnChu.SetBounds(p.X + 200, p.Y + 230, 50, 50); 56 btnChu.Tag = "/"; 57 btnChu.Text = "/"; 58 btnChu.Click += new EventHandler(btn_Click); 59 Button btnQ = new Button(); 60 btnQ.SetBounds(p.X + 250, p.Y + 80, 50, 50); 61 btnQ.Tag = "C"; 62 btnQ.Text = "C"; 63 btnQ.Click += new EventHandler(btn_Click); 64 Button btnD = new Button(); 65 btnD.SetBounds(p.X + 250, p.Y + 130, 50, 150); 66 btnD.Tag = "="; 67 btnD.Text = "="; 68 btnD.Click += new EventHandler(btn_Click); 69 for (int i = 1; i <=9; i++) 70 { 71 btn = new Button(); 72 btn.SetBounds(p.X+50, p.Y+80, 50, 50); 73 btn.Text = i+""; 74 btn.Tag = i ; 75 btn.Visible = true; 76 btn.Click += new EventHandler(btn_Click); 77 this.Controls.Add(btn); 78 p.X += 50; 79 if (p.X >= 150)//如果横向尺寸大于150,那么就换行(3个按钮刚好150尺寸) 80 { 81 p.X = 0; 82 p.Y += 50;//纵向移动 83 } 84 } 85 this.Controls.Add(txt);//在面板中添加文本框 86 this.Controls.Add(btn0);//添加按钮0 87 this.Controls.Add(btnJ);//添加+加号按钮 88 this.Controls.Add(btnJan);//添加-减号按钮 89 this.Controls.Add(btnC);//添加*乘法按钮 90 this.Controls.Add(btnChu);//添加/除法按钮 91 this.Controls.Add(btnQ);//添加C清空按钮 92 this.Controls.Add(btnD);//添加=号按钮 93 this.Controls.Add(lbl);//添加lable 94 } 95 private void btn_Click(object sender, EventArgs e) 96 { 97 98 Button btn = (Button)sender; 99 /*linq写法*/ 100 string str = this.Controls.OfType<TextBox>().Single(item => item.Name == "txtName").Text; 101 string str1 = this.Controls.OfType<Label>().Single(item => item.Name == "lblname").Text; 102 /*一般的写法*/ 103 TextBox txt = this.Controls.Find("txtName", true)[0] as TextBox; 104 Label lbl = this.Controls.Find("lblname", true)[0] as Label; 105 str1+=btn.Tag.ToString(); 106 lbl.Text = str1; 107 if (str1.IndexOf('=') != -1)//如果字符里面有=号 108 { 109 str1 = str1.Substring(0,str1.Length-1);//把=号截取掉 110 111 if (str1.IndexOf('+') != -1)//如果剩下的字符串里面有+号 112 { 113 string[] nums = str1.Split('+');//把+号的前后字符串分割并装到数组 114 int sum = 0; 115 foreach (var num in nums)//循环遍历数组 116 { 117 sum += Convert.ToInt32(num); 118 } 119 txt.Text = sum.ToString();//文本框获得结果 120 lbl.Text = "";//清空lable里的内容 121 } 122 if (str1.IndexOf('-') != -1) 123 { 124 string[] nums = str1.Split('-'); 125 int subtr = 0; 126 foreach (var num in nums) 127 { 128 subtr -= Convert.ToInt32(num); 129 } 130 txt.Text = subtr.ToString(); 131 lbl.Text = ""; 132 } 133 if (str1.IndexOf('*') != -1) 134 { 135 string[] nums = str1.Split('*'); 136 int mult = 1; 137 foreach (var num in nums) 138 { 139 mult *= Convert.ToInt32(num); 140 } 141 txt.Text = mult.ToString(); 142 lbl.Text = ""; 143 } 144 if (str1.IndexOf('/') != -1) 145 { 146 string[] nums = str1.Split('/'); 147 int division = 1; 148 for (int i = 0; i < nums.Length-1; i++) 149 { 150 division= Convert.ToInt32(nums[i])/Convert.ToInt32(nums[nums.Length-1]); 151 } 152 //foreach (var num in nums) 153 //{ 154 // division /= Convert.ToInt32(num); 155 //} 156 txt.Text = division.ToString(); 157 lbl.Text = ""; 158 } 159 } 160 if (str1.IndexOf('C') != -1) 161 { 162 lbl.Text = ""; 163 txt.Text = ""; 164 } 165 166 167 168 169 // MessageBox.Show(str); 170 } 171 } 172 }