简体   繁体   English

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

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

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.TxtQuantityTxtPrice文本框中没有任何内容(包括程序启动时)时,我需要禁用BtnCalculateBtnMessageBox按钮。 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.可能很简单,但 IDK 我在做什么。

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

use TextChanged event on textbox在文本框上使用 TextChanged 事件

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,如果您希望在加载的 window 上执行此操作,

use利用

textBox1_TextChanged(null,null);

on loaded event加载事件

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:我只想编写一个方法,根据两个文本框控件的文本框文本长度设置按钮启用属性,然后从表单 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