1、首先,新建项目,新建窗体。
2、拖入两个textbox框。一个用于输入textBox7,一个用于显示textBox8。其它添加Lable控件,显示单位。
3、用输入文本框添加事件,一个为了控制输入数字为字符。一个实现在输入完成以后,文本显示控件textBox8立刻显示结果。
4、具体事件代码如下: private void textBox7_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13 && e.KeyChar != 45 && e.KeyChar != 46) { e.Handled = true; } //输入为负号时,只能输入一次且只能输入一次 if (e.KeyChar == 45 && (((TextBox)sender).SelectionStart != 0 || ((TextBox)sender).Text.IndexOf("-") >= 0)) e.Handled = true; if (e.KeyChar == 46 && ((TextBox)sender).Text.IndexOf(".") >= 0) e.Handled = true; } private void textBox7_TextChanged(object sender, EventArgs e) { float n1 = 0; if (this.textBox7.Text == "") { n1 = 0; } else { n1 = float.Parse(this.textBox7.Text); } this.textBox8.Text = Convert.ToString(n1 * 2.2046); }
5、其它关于长度,重量等的计算方法类似。请开发人员自己扩展。显示效果如图。
6、如果对您有所帮助,请继续阅读我的其它经验,也可以投票支持我。谢谢。