首页 > 解决方案 > 当用户开始输入新数据时如何清除文本框

问题描述

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

        // 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 = "";
    }

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

标签: c#

解决方案


这可能会有所帮助。下面的代码将清除文本txtIncomedouble clicked

包括MouseDoubleClick事件和Name如下txtIncome

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

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

推荐阅读