[英]How to disable button if textbox is empty
首先很抱歉我的英語不好。 我是 C# 的初學者,我制作了一個 Windows 表單應用程序,但如果文本框為空,我無法禁用一個按鈕。 我嘗試了一些 Enabled 方法,但它們不起作用。 希望有人能幫我解決這個問題。 非常感謝
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);
}
}
}
}
}
感謝@Cody Gray 已經提出了這一點; 我剛剛擴展了它,所以你可以看到如何實現以及它是如何工作的
概述
當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。 這可能帶來的問題是,如果用戶只在文本框中輸入一個空格,它就不再是Empty
或null
; 因此此檢查將返回true
。 如果程序的邏輯要求在文本框中輸入實際值,則此邏輯可能存在缺陷。
string.IsNullOrWhiteSpace()
檢查將檢查 3 個條件 - 如果輸入string
為null
、 Empty
並且僅包含空白字符(空格、換行符等)。我希望這會增加一些額外的絨毛,讓您對未來做出明智的決定。
希望它起作用!
private void YourTextBox_TextChanged(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(YourTextBox.Text))
YourButton.Enabled = false;
else
YourButton.Enabled = true;
}
在 TextBox 的 TextChanged 事件上處理它。 (在設計時雙擊您的文本框控件,它將自動為您創建文本更改事件)。
private void textboxPyetja_OnTextChanged(..blah blah)
{
if(String.IsNullOrWhitespace(txtTargetTextbox.Text)
{
//disable your control
}
else
{
//enable your control
}
}
4 年后編輯 - 出於某種原因:這是一個單行版本,有些人只是喜歡它們......
private void textboxPyetja_OnTextChanged(..blah blah)
{
btnILoveButtons.Enabled = string.IsNullOrWhitespace(txtTargetTextbox.Text);
{
試試這個:
if (String.IsNullOrEmpty(textboxPergjigja.Text))
butoniVerteto.Enabled = false;
else
butoniVerteto.Enabled = true;
在txtbox中添加編輯文本的事件。 當文本更改時,啟用按鈕
將textBox1
更改為您的文本框名稱,然后將button1
名稱更改為您的按鈕名稱
if (textBox1.Text == "")
{
button1.Enabled = false;
}
else
button1.Enabled = true;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.