简体   繁体   中英

controlling the button enable property in C# with if statements

I've been trying make an if statement which will enable the button, but I'm not sure where I made the mistake.

private void btnPayment_Click(object sender, EventArgs e)
    {
        double MonthlyPayment = 0;
        double interest = cboInterest.SelectedIndex;
        double term = cboTerm.SelectedIndex;
        double principal = Double.Parse(txtPrincipal.Text);
        double expN = Math.Pow(1+interest, term*12)-1;

        MonthlyPayment = principal * (((interest / 12) * expN) / (expN - 1));

        string strMP = MonthlyPayment.ToString();
        string intrst = interest.ToString();
        string trm = term.ToString();
        string princ = principal.ToString();

        if (principal >= 0)
        {
            this.btnPayment.Enabled = true;
        }
        lblResult.Text = "The monthly payment for a loan of $" + princ + " at " + intrst + "% for " + trm + " years is $" + strMP; 
    }
}

}

This is never going to work, you're trying to enable the button from the click event of a button you have disabled? Atleast that is what i can see.

I think you need to put a check on txtPrincipal.Text leave event. ie when a user leaves this text box, there should be function that checks whether a value is present and is greater than 0. If these conditions are met than your button should be enabled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM