Reputation: 55
I'm creating an income tax calculator. I created a new event that clears the data in the income textbox when the user types new data. It is not clearing the data when you type, it just continues the number.
// 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);
The above coding is what I gathered from the textbook to add for the event to work.
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 = "";
}
A textbox that clears when there is new data being entered
Upvotes: 1
Views: 80
Reputation: 18975
You should implement click event and use clear method like this
this.txtIncome.Click += new System.EventHandler(clearIncome);
private void clearIncome(object sender, EventArgs e)
{
txtIncome.Clear();
}
Upvotes: 0
Reputation: 242
This might help. The below code will clear the textbox txtIncome
when it is double clicked
.
Inculde MouseDoubleClick
event and Name
in txtIncome
as below.
<TextBox MouseDoubleClick="txtIncome_MouseDoubleClick" Name="txtIncome" />
private void txtIncome_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
txtIncome.Clear();
}
Upvotes: 1
Reputation: 81473
I am not sure if this will help you out. However, you can use the onclick event, and SelectAll(). This will have a similar affect, and is very common in windows apps
private void TextBoxOnClick(object sender, EventArgs eventArgs)
{
var textBox = (TextBox)sender;
textBox.SelectAll();
textBox.Focus(); // might not need this
}
Upvotes: 0