Reputation: 15
I need the buttons BtnCalculate
and BtnMessageBox
to be disabled when there is nothing in the TxtQuantity
and TxtPrice
Text boxes including when the program starts. Does anyone know how to do this? Obviously the "Quantity and price must not be empty" message will not be needed in the code anymore after changes are made. Thank you very much in advance! May be simple but IDK what I am doing.
Here is the CODE:
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;
}
}
Upvotes: 1
Views: 160
Reputation: 37080
I would just write a method that sets the button enabled property based on the textbox text length of the two textbox controls, and then call it from the form Load event and the TextChanged event of the textboxes:
private void Form1_Load(object sender, EventArgs e)
{
ButtonEnabler();
}
private void txtPrice_TextChanged(object sender, EventArgs e)
{
ButtonEnabler();
}
private void txtQuantity_TextChanged(object sender, EventArgs e)
{
ButtonEnabler();
}
private void ButtonEnabler()
{
bool enabled = txtPrice.TextLength > 0 && txtQuantity.TextLength > 0;
btnCalculate.Enabled = enabled;
btnMessageBox.Enabled = enabled;
}
Upvotes: 0
Reputation: 861
use TextChanged event on textbox
like this
private void textBox1_TextChanged(object sender, EventArgs e)
{
button1.Enabled = (textBox1.Text.Length > 0);
}
if you wish to do this on window loaded,
use
textBox1_TextChanged(null,null);
on loaded event
Upvotes: 1