这是代码
[C#] 纯文本查看 复制代码 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 Operator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Maximized)
{
this.WindowState = FormWindowState.Normal;
}
}
private void txtNum1_KeyPress(object sender, KeyPressEventArgs e)
{
TextBox currentTextBox = sender as TextBox;
if (e.KeyChar < '0' || e.KeyChar > '9') //不是数字
{
e.Handled = true;
}
if (e.KeyChar == 8)
{
e.Handled = false;
}
if (e.KeyChar == 46)
{
if (currentTextBox.Text.IndexOf(".") == -1)
{
if (currentTextBox.SelectionStart > 0)
{
e.Handled = false;
}
}
}
}
private void btnCompute_Click(object sender, EventArgs e)
{
double num1 = Convert.ToDouble(txtNum1.Text);
double num2 = Convert.ToDouble(txtNum2.Text);
switch (cboOperator.Text)
{
case "+":
lblResult.Text = (num1 + num2).ToString();
break;
case "-":
lblResult.Text = (num1 - num2).ToString();
break;
case "*":
lblResult.Text = (num1 * num2).ToString();
break;
case "/":
lblResult.Text = (num1 / num2).ToString();
break;
}
}
}
}
这是应用界面
代码的目的是让两个textbox开头不能输入小数点且只能输入一个小数点,为什么我的第二个textbox达不到这样的效果
|