Мне нужно, чтобы кнопки BtnCalculate
и BtnMessageBox
были отключены, когда в текстовых полях TxtQuantity
и TxtPrice
ничего нет, в том числе и при запуске программы. Кто-нибудь знает как это сделать? Очевидно, что сообщение «Количество и цена не должны быть пустыми» больше не потребуется в коде после внесения изменений. Заранее большое спасибо! Может быть просто, но ИДК, что я делаю.
Вот код:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void BtnCalculate_Click(object sender, EventArgs e)
{
//declare Variables
int intQuantity;
Decimal decPrice;
Decimal decTotal;
//make sure quantity and price are the same
// if string is null or empty retuern textbox
Decimal TAX_RATE = 0.06m;
if (OosTax.Checked == true)
{ TAX_RATE = 0.09m; }
if ((TxtQuantity.Text.Trim().Length == 0 || (TxtPrice.Text == "")))
{ MessageBox.Show("Quantity and price must not be empty", "Calculator", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2); }
else
{
try
{
intQuantity = Int32.Parse(TxtQuantity.Text);
decPrice = Decimal.Parse(TxtPrice.Text);
decTotal = (intQuantity * decPrice) * (1 + TAX_RATE);
LblMessage.Text = decTotal.ToString("C");
}
catch (Exception ex)
{ // Send Focus to Quantity
TxtQuantity.Focus();
TxtQuantity.SelectAll();
}
}
}
private void BtnMessageBox_Click(object sender, EventArgs e)
{
//declare Variables
int intQuantity;
Decimal decPrice;
Decimal decTotal;
string message = "Your Total Is: ";
Decimal TAX_RATE = 0.06m;
if (OosTax.Checked == true)
{ TAX_RATE = 0.09m; }
//make sure quantity and price are the same
// if string is null or empty retuern textbox
if ((TxtQuantity.Text.Trim().Length == 0 || (TxtPrice.Text == "")))
{ MessageBox.Show("Quantity and price must not be empty", "Calculator", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2); }
else
{
try
{
intQuantity = Int32.Parse(TxtQuantity.Text);
decPrice = Decimal.Parse(TxtPrice.Text);
decTotal = (intQuantity * decPrice) * (1 + TAX_RATE);
// Display Total Currency as
MessageBox.Show(message + System.Environment.NewLine + decTotal.ToString("C"), "Chapter Two",
MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}
catch (Exception ex)
{ // Send Focus to Quantity
TxtQuantity.Focus();
TxtQuantity.SelectAll();
}
}
}
private void BtnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void BtnClear_Click(object sender, EventArgs e)
{
LblMessage.Text = String.Empty;
}
}