Alex
Alex

Reputation: 165

clear the controls and reset the form to its original state?

As the title says, how do i clear the controls and reset the form to its original state? I'm suppose to have it so when I click the button "Clear" it resets everything. So I need something inside the code below, but I'm not sure how to go about it, and everything I looked up wasn't clear.

 private void btnClear_Click(object sender, EventArgs e)
    {

    }

The rest of the code calculates grosspay, netpay, and a bunch of other tax deductions and display it on the form. So that is what I want to clear/reset on the form.

private void btnCalculatePay_Click(object sender, EventArgs e)
    {
        double hourlyRate = Convert.ToDouble(txtHourlyRate.Text);
        double hoursWorked = Convert.ToDouble(txtHoursWorked.Text);

        double grossPay = hourlyRate * hoursWorked;

        double stateIncome = grossPay * 0.035;
        double federalIncome = grossPay * 0.15;
        double socialSecurity = grossPay * 0.062;
        double medicare = grossPay * .029;
        double netPay = grossPay - (stateIncome + federalIncome + socialSecurity + medicare);

        lblDisplay.Text = "Gross Pay: " + grossPay + "\nState Income Tax Deduction: " + stateIncome + "\nFederal Income Tax Deduction: " + federalIncome + "\nSocial Security Deduction: " + socialSecurity + "\nMedicare Deduction: " + medicare + "\nNet Pay: " + netPay;


    }

Upvotes: 1

Views: 152

Answers (1)

Rahul
Rahul

Reputation: 77846

With clearing control if you mean clearing controls data then you can use controls Text property and set it to string.Empty like below. Again depending on the different controls you have in your form and not all controls do have a Text property.

 private void btnClear_Click(object sender, EventArgs e)
    {
        lblDisplay.Text = string.Empty;
        txtbox1.Text = string.Empty;
    }

Upvotes: 2

Related Questions