繁体   English   中英

当文本框中没有文本时如何禁用按钮?

[英]How to make a button disabled when no text is in a textbox?

TxtQuantityTxtPrice文本框中没有任何内容(包括程序启动时)时,我需要禁用BtnCalculateBtnMessageBox按钮。 有谁知道如何做到这一点? 显然,在进行更改后,代码中将不再需要“数量和价格不能为空”消息。 非常感谢您提前。 可能很简单,但 IDK 我在做什么。

这是代码:

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;
    }
}

在文本框上使用 TextChanged 事件

像这样

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        button1.Enabled = (textBox1.Text.Length > 0);
    }

如果您希望在加载的 window 上执行此操作,

利用

textBox1_TextChanged(null,null);

加载事件

我只想编写一个方法,根据两个文本框控件的文本框文本长度设置按钮启用属性,然后从表单 Load 事件和文本框的 TextChanged 事件中调用它:

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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