【问题标题】:How would I use and implement a MaskedTextBox into my Calculator application? C#我将如何在我的计算器应用程序中使用和实现 MaskedTextBox? C#
【发布时间】:2011-04-06 00:30:49
【问题描述】:

所以我正在 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” - 如果这是正确的?我该如何实施?是什么让它发挥作用?

谢谢!

【问题讨论】:

  • 您使用的是 WinForms 还是 WPF?它看起来像 WinForms,但你应该标记它。

标签: c# calculator maskedtextbox


【解决方案1】:

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

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

【讨论】:

  • 问题确实指的是人们输入字符。
  • 是的。我没注意。
  • 这确实解决了我的主要问题,非常感谢!非常感谢您的帮助!
  • 没问题 - 朋友不要让朋友使用MaskedTextBox。 :)
【解决方案2】:

除了MusiGenesis的sn-p,还可以使用文本框的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

【讨论】:

  • 如果光标位于已输入文本的中间,这将失败。
  • 并添加到 Bala 的 sn-p:这仅在 textBox1 具有焦点时才有效,而在您第一次单击其中一个按钮时将不再有效。既然您想必无论如何都希望得到键盘按下,您可以改为将表单的 KeyPreview 属性设置为 true,并为 form's KeyPress 事件添加一个处理程序(并将 Bala 的代码)。
  • @Greg 光标位置无关紧要。由于 e.Handled 设置为 true,因此在此事件处理程序中手动添加字符。
【解决方案3】:

如果你只编写一次点击事件的代码,并为它分配每个数字按钮的点击事件处理程序,你也可以节省大量代码:

    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) (...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-06
    • 1970-01-01
    • 2017-11-03
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多