![](/img/trans.png)
[英]enable or disable button based on comparing textboxes values of gridview in c#
[英]How to enable button after all textboxes are not empty in c# winforms?
在我的所有文本框都不為空之后,如何將按鈕屬性設置為 enabled=true? 我正在學習編程,我的應用程序很簡單。
當我的一個文本框有文本時,我知道如何啟用此屬性,但事實並非如此。
用例是用戶需要將數據放入兩個文本框中,然后才能單擊 btn。 如何以最簡單的方式驗證所有表單然后啟用按鈕?
只有 2 tb: https://i.imgur.com/JUslNWE.png
您需要創建一個TextBox_TextChanged
事件並訂閱所有文本框。
private void TextBox_TextChanged(object sender, EventArgs e)
{
int notEmptyTextBoxCount = 0;
int textBoxCount = 0;
foreach (var item in Controls)
{
if (item is TextBox txtb)
{
textBoxCount++;
if (txtb.Text != String.Empty)
notEmptyTextBoxCount++;
}
}
if (textBoxCount == notEmptyTextBoxCount)
button.Enabled = true;
else
button.Enabled = false;
}
感謝大家的所有反饋。
我設法做到了這種方式:
private void ValidateTextBoxes()
{
if (loginTextBox.Text.Length != 0 && passTextBox.Text.Length != 0)
{
generateHashBtn.Enabled = true;
}
else
{
generateHashBtn.Enabled = false;
}
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
ValidateTextBoxes();
}
private void TextBox2_TextChanged(object sender, EventArgs e)
{
ValidateTextBoxes();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.