用户开始输入新数据时如何清除文本框

时间:2019-02-14 05:46:51

标签: c#

我正在创建一个所得税计算器。我创建了一个新事件,当用户键入新数据时,该事件将清除收入文本框中的数据。键入时不会清除数据,而只是继续编号。

        // txtIncome
        // 
        this.txtIncome.Location = new System.Drawing.Point(125, 41);
        this.txtIncome.Name = "txtIncome";
        this.txtIncome.Size = new System.Drawing.Size(106, 20);
        this.txtIncome.TabIndex = 4;
        this.txtIncome.TextChanged += new System.EventHandler(clearIncome);

上面的编码是我从教科书中收集的,用于使活动正常进行。

    private void btnCalculate_Click(object sender, EventArgs e)
    {
        income = Convert.ToDecimal(txtIncome.Text);
        incomeCalcualtor();
        txtOwed.Text = Convert.ToString(owed);
        txtIncome.Focus();

    }

    private void incomeCalcualtor()
    {
        if (income <= 9225)

            owed = (int)income * .10m;

        else if ((int)income > 9225m && (int)income <= 37450)
            owed = 922.50m + (int)((income - 9225) * .15m);

        else if (income > 37450 && income <= 90750)
            owed = 5156.25m + (int)((income - 37450) * .25m);

        else if (income > 90750 && income <= 189300)
            owed = 18481.25m + (int)((income - 90750) * .28m);

        else if (income >= 189300 && income <= 411500)
            owed = 46075.25m + (int)((income - 189300) * .33m);

        else if (income >= 411500 && income <= 413200)
            owed = 119401.25m + (int)((income - 411500) * .35m);

        else if (income <= 413200)
            owed = 11996.25m + (int)((income - 413200) * 39.6m);
    }


    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void clearIncome(object sender, EventArgs e)
    {
        txtIncome.Text = "";
    }

在输入新数据时清除的文本框

3 个答案:

答案 0 :(得分:1)

这可能会有所帮助。以下代码将清除txtIncome文本框double clicked

如下所示植入MouseDoubleClick事件和Name中的txtIncome

<TextBox MouseDoubleClick="txtIncome_MouseDoubleClick" Name="txtIncome" />

private void txtIncome_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
  txtIncome.Clear();
}

答案 1 :(得分:1)

您应该实现click事件并使用像这样的清除方法

 this.txtIncome.Click += new System.EventHandler(clearIncome);

private void clearIncome(object sender, EventArgs e)
    {
        txtIncome.Clear();
    }

答案 2 :(得分:0)

我不确定这是否对您有帮助。但是,您可以使用onclick事件和SelectAll()。这将产生类似的影响,并且在Windows应用程序中很常见

private void TextBoxOnClick(object sender, EventArgs eventArgs)
{
    var textBox = (TextBox)sender;
    textBox.SelectAll();
    textBox.Focus(); // might not need this
}