2016-06-26 80 views
0

首先抱歉我的英語不好。 我是C#的初學者,我製作了Windows窗體應用程序,但如果文本框爲空,則無法禁用一個按鈕。 我嘗試了一些啓用的方法,但他們沒有奏效。希望有人能幫我解決這個問題。非常感謝你如何禁用按鈕如果文本框爲空

public partial class ModulusForm : Form 
{ 
    public double nje; 
    public double dy; 
    public double pergjigja; 
    public double rezultati; 
    public ModulusForm() 
    { 

     InitializeComponent(); 
     Button btn = new Button(); 
     btn.Click += new EventHandler(butoniGjenero_Click); 
    } 
    private void butoniPerfundo_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 
    private void butoniGjenero_Click(object sender, EventArgs e) 
    { 
     Random random = new Random(); 
     nje = random.Next(1, 100); 
     dy = random.Next(1, 100); 
     if (nje > dy) 
     { textboxPyetja.Text = "X = " + nje + " " + "dhe" + " " + "Y = " + dy; } 
     else if (nje > dy) 
     { 
      nje = random.Next(1, 100); 
      dy = random.Next(1, 100); 
     } 
     rezultati = nje/dy; 
    } 
    private void butoniPastro_Click(object sender, EventArgs e) 
    { 
      textboxPyetja.Clear(); 
      textboxPergjigja.Clear(); 
      textboxPergjigjaSakt.Clear(); 
    } 
    private void butoniVerteto_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      pergjigja = double.Parse(textboxPergjigja.Text); 
     } 
     catch 
     { 
      var informim = MessageBox.Show("Rishiko fushat!", "Verejtje", MessageBoxButtons.OK, MessageBoxIcon.Information); 
     } 
     if (textboxPergjigja.Text == "") 
     { 
      //nothin' baby 
     } 
     else 
     { 
      if (textboxPyetja.Text == "") 
      { 
       var informim = MessageBox.Show("Fusha e pyetjes eshte null!", "Verejtje", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      } 
      else 
      { 
       if (pergjigja == rezultati) 
       { 
        textboxPergjigjaSakt.Text = "Pergjigja eshte e sakte"; 
       } 
       else 
       { 
        textboxPergjigjaSakt.Text = "Gabim." + " " + "Pergjigja e sakte eshte: " + "" + rezultati; 
       } 
       comboboxVargu.Items.Add(nje + "/" + dy + " = " + rezultati); 
      } 
     } 
    } 
} 

}

+2

處理文本框的TextChanged事件。在該事件處理程序內部,檢查文本框的Text屬性是否爲空字符串。如果它是空的,則禁用該按鈕:'myButton.Enabled =!textBox.Text.IsNullOrEmpty();' –

+0

處理這種事情的優雅方法是將事件處理程序附加到Application.Idle事件並執行任何狀態更新你需要在那裏。假設你正在使用WinForms。 –

回答

1

試試這個:

if (String.IsNullOrEmpty(textboxPergjigja.Text)) 
    butoniVerteto.Enabled = false; 
else 
    butoniVerteto.Enabled = true; 
+0

我剛試過,它是禁用按鈕。但是,當文本框不是空的按鈕不會改變,它保持禁用。 – Dren

+0

@Dren你是在哪裏放置的? –

+0

butoniVerteto_Click – Dren

3

感謝@Cody灰色爲已表明這一點;我剛纔已經擴大了,所以你可以看到如何實現和它是如何工作

概述
您可以連接起來,當你textboxPergjigja.Text的文本已更改爲事件處理程序。

在您創建的處理程序中,您可以評估您的按鈕是否應該是Enabled - 使用string.IsNullOrWhiteSpace()檢查來設置它。

第一:
在你的構造函數形式,訂閱textboxPergjigja.Text文本框的TextChanged事件。

像這樣:

public ModulusForm() 
{ 
    InitializeComponent(); 
    Button btn = new Button(); 
    btn.Click += new EventHandler(butoniGjenero_Click); 

    // Add the subscription to the event: 
    textboxPergjigja.TextChanged += textboxPergjigja_TextChanged; 
} 

下一頁:
添加,對於該事件的正確的委託簽名相匹配的處理程序。

像這樣:

public textboxPergjigja_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    // If the text box is not "empty", it will be enabled; 
    // If the text is "empty", it will be disabled. 
    butoniVerteto.Enabled = !string.IsNullOrWhiteSpace(textBoxPergjigja.Text);    
} 

這樣,無論何時textBoxPergjigja文本框中的文本改變;評估運行並且您的按鈕將始終正確啓用/禁用。

希望這會有所幫助! :)

附加信息
您還可以使用textBox.Text.IsNullOrEmpty(),它仍然可以工作 - 通過@Cody

我已經使用string.IsNullOrWhiteSpace()的建議,而不是textBox.Text.IsNullOrEmpty(),原因如下:

  • .IsNullOrEmpty()方法僅檢查是否textBox.Text要麼null或字符的總量等於0

這可能造成的問題是,如果用戶僅進入在文本框中的空間中,它不再Emptynull;因此該檢查將返回true。如果程序的邏輯要求將值輸入到文本框中,則此邏輯可能存在缺陷。

  • 在另一方面:本string.IsNullOrWhiteSpace()檢查將檢查3個條件 - 如果輸入stringnullEmpty僅包含空白字符(空格,新行等),也。

我希望這會增加一點額外的絨毛,讓您對未來做出明智的決定。

0

在txtbox中添加編輯文本的事件。當文字改變時,啓用按鈕

2

希望它的工作!

private void YourTextBox_TextChanged(object sender, EventArgs e) 
{ 
    if (String.IsNullOrEmpty(YourTextBox.Text)) 
     YourButton.Enabled = false; 
    else 
     YourButton.Enabled = true; 
} 
1

在TextBox的TextChanged事件上處理它。 (在設計時雙擊您的文本框控件,它會自動爲您創建文本更改事件)。

private void textboxPyetja_OnTextChanged(..blah blah) 
{ 
    if(String.IsNullOrWhitespace(txtTargetTextbox.Text) 
    { 
     //disable your control 
    } 
    else 
    { 
     //enable your control 
    } 
} 
相關問題