using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 计算器 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void comboBox1_DrawItem(object sender, DrawItemEventArgs e) //修改combox 信息 中间显示 { string s = this.comboBox1.Items[e.Index].ToString(); SizeF ss = e.Graphics.MeasureString(s, e.Font); float l = (float)(e.Bounds.Width - ss.Width) / 2; if (l < 0) l = 0f; float t = (float)(e.Bounds.Height - ss.Height) / 2; if (t < 0) t = 0f; t = t + this.comboBox1.ItemHeight * e.Index; e.DrawBackground(); e.DrawFocusRectangle(); e.Graphics.DrawString(s, e.Font, new SolidBrush(e.ForeColor), l, t); } private void Form1_Load(object sender, EventArgs e) { this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; } private void button1_Click(object sender, EventArgs e) { int str1 = Convert.ToInt32(textBox1.Text); int str2 = Convert.ToInt32(textBox2.Text); Jisuan jisuan = new Jisuan(Convert.ToInt32(str1), Convert.ToInt32(str2)); switch (comboBox1.Text) { case "+": label1.Text = jisuan.Add().ToString(); break; } } } }
类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 计算器 { public class Jisuan { public Jisuan(int str1, int str2) { this.Number1 = str1; this.Number2 = str2; } public int Number1 { get ; set; } public int Number2 { get; set; } public int Add() { return Number1 + Number2; } } }