1

所以我正在 C-Sharp 中开发一个计算器应用程序,我想防止人们一次输入超过 1 个句点/点。所以他们不能输入“.....”或“1..1”或“1.1.1”真的只是这样的东西......我还想通过输入来阻止他们添加字母字符在用键盘输入像“a, b, c”这样的字符。

有人告诉我使用 MaskedTextBox,我想知道这是否正确。另外,如果它是正确的,我将如何将它实现到我的代码中?当谈到 C# 时,我是一个完整的初学者,所以我想要一些帮助(对于初学者来说要低调一些)。

到目前为止,我编写的代码是:

    double total1 = 0;
    double total2 = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void btnOne_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnOne.Text;
    }

    private void btnTwo_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnTwo.Text;
    }

    private void btnThree_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnThree.Text;
    }

    private void btnFour_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnFour.Text;
    }

    private void btnFive_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnFive.Text;
    }

    private void btnSix_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnSix.Text;
    }

    private void btnSeven_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnSeven.Text;
    }

    private void btnEight_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnEight.Text;
    }

    private void btnNine_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnNine.Text;
    }

    private void btnZero_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnZero.Text;
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        txtDisplay.Clear();
    }

    private void btnPlus_Click(object sender, EventArgs e)
    {
        total1 = total1 + double.Parse(txtDisplay.Text);
        txtDisplay.Clear();
    }

    private void btnEquals_Click(object sender, EventArgs e)
    {
        total2 = total1 + double.Parse(txtDisplay.Text);
        txtDisplay.Text = total2.ToString();
        total1 = 0;

    }

    private void btnPoint_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + btnPoint.Text;
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void txtDisplay_TextChanged(object sender, EventArgs e)
    {

    }
}

所以我问......我如何/在哪里添加这个“MaskedTextBox” - 如果这是正确的?我该如何实施?是什么让它发挥作用?

谢谢!

4

3 回答 3

1

如果您只编写一次单击事件的代码并将每个数字按钮的单击事件处理程序分配给它,您还可以节省大量代码:

    private void btnNumber_Click(object sender, EventArgs e)
    {
        if (sender is Button)
            txtDisplay.Text = txtDisplay.Text + ((Button)sender).Text;
    }

所以可以保存以下所有方法

private void btnZero_Click(object sender, EventArgs e) (...)
private void btnOne_Click(object sender, EventArgs e) (...)
.
.
.
private void btnNine_Click(object sender, EventArgs e) (...)
于 2013-05-24T10:49:25.557 回答
1

您不需要MaskedTextBox, 因为您正在使用按钮模拟键盘。只需将这样的内容放入btnPoint_Click

private void btnPoint_Click(object sender, EventArgs e)     
{         
    if (!txtDisplay.Text.Contains("."))
    {
        txtDisplay.Text = txtDisplay.Text + btnPoint.Text;    
    } 
} 
于 2011-04-06T00:45:23.343 回答
1

除了MusiGenesis的片段外,您还可以使用文本框的KeyPress事件来防止非数字和多个句点。

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if(char.IsDigit(e.KeyChar) || ((e.KeyChar == '.' && textBox1.Text.IndexOf(".") < 0) ) )
    {
        textBox1.Text += e.KeyChar;
    }
    e.Handled = true;
}

或将ReadOnly文本框的属性设置为true.

于 2011-04-06T00:52:23.273 回答