Dre*_*ren 2 c# forms windows winforms
首先很抱歉我的英语不好。我是 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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
感谢@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;
}
Run Code Online (Sandbox Code Playgroud)
下一步:
添加与该事件的正确委托签名匹配的处理程序。
像这样:
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);
}
Run Code Online (Sandbox Code Playgroud)
这样,每当textBoxPergjigja文本框中的文本发生更改时;评估运行,您的按钮将始终正确启用/禁用。
希望这可以帮助!:)
附加信息
您也可以使用textBox.Text.IsNullOrEmpty(),它仍然可以工作 - 正如@Cody 所建议的
我使用string.IsNullOrWhiteSpace(), 而不是textBox.Text.IsNullOrEmpty()出于以下原因:
.IsNullOrEmpty()方法仅检查textBox.Text是null或字符总数是否等于 0。这可能带来的问题是,如果用户只在文本框中输入一个空格,它就不再是Empty或null; 因此,此检查将返回true。如果程序的逻辑要求在文本框中输入实际值,则此逻辑可能存在缺陷。
string.IsNullOrWhiteSpace()检查将检查 3 个条件 - 如果输入string是null,Empty 并且也仅包含空白字符(空格、换行符等)。我希望这会增加一些额外的绒毛,让您对未来做出明智的决定。
小智 6
希望它起作用!
private void YourTextBox_TextChanged(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(YourTextBox.Text))
YourButton.Enabled = false;
else
YourButton.Enabled = true;
}
Run Code Online (Sandbox Code Playgroud)