簡體   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