如果文本框为空,如何禁用按钮 [英] How to disable button if textbox is empty

查看:115
本文介绍了如果文本框为空,如何禁用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对不起我的英语不好.我是C#的初学者,我制作了Windows窗体应用程序,但如果文本框为空,则无法禁用一个按钮.我尝试了一些Enabled方法,但没有用.希望有人可以帮助我解决此问题.非常感谢

First of all sorry for my bad english. I'm beginner at C# and i made a Windows forms application but i can't disable one button if a textbox is empty. I tried some of the Enabled methods but they didn't work. Hope someone can help me fix this. Thank you very much

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 的文本更改时连接事件处理程序.

Overview
You can wire up an event handler for when your textboxPergjigja.Text's text has changed.

在您创建的处理程序中,然后可以使用 string.IsNullOrWhiteSpace()进行设置来评估按钮是否应启用 Enabled .

In the handler you create, you can then evaluate whether your button should be Enabled or not - using the string.IsNullOrWhiteSpace() check to set this.

第一:
在表单的构造函数中,订阅 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;
}

下一步:
添加与该事件的正确委托人签名相匹配的处理程序.

Next:
Add a handler that matches the correct delegate signature for that event.

赞:

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 文本框中的文本,就可以;运行评估,您的按钮将始终正确启用/禁用.

This way, whenever the text in the textBoxPergjigja text box is changed; the evaluation is run and your button will always be enabled/disabled correctly.

希望这会有所帮助!:)

Hope this helps! :)

其他信息
您还可以使用 textBox.Text.IsNullOrEmpty(),它仍然可以正常工作-如@Cody

Additional Info
You can also use textBox.Text.IsNullOrEmpty(), and it will still work - as suggested by @Cody

出于以下原因,我使用 string.IsNullOrWhiteSpace(),而不是 textBox.Text.IsNullOrEmpty():

I have used string.IsNullOrWhiteSpace(), as opposed to textBox.Text.IsNullOrEmpty() for the following reasons:

  • .IsNullOrEmpty()方法仅 检查 textBox.Text 是否为 null 或总计字符数等于0.
  • The .IsNullOrEmpty() method only checks if the textBox.Text is either null or the total amount of characters is equal to 0.

这可能引起的问题是,如果用户 only 在文本框中输入空格,则不再是 Empty null ;因此,此检查将返回 true .如果程序的逻辑要求在文本框中输入一个 actual 值,则该逻辑可能存在缺陷.

The problem this might pose is, if the user only enters a space in the textbox, it is no longer Empty or null; thus this check will return true. If the logic of the program requires that an actual value be entered into the textbox, this logic can be flawed.

  • 另一方面: string.IsNullOrWhiteSpace()检查将检查3种情况-输入的 string 是否为 null Empty 也仅包含空格字符(空格,换行符等).
  • On the other hand: The string.IsNullOrWhiteSpace() check will check on 3 conditions - if the input string is null, Empty and contains only whitespace characters (space, newline etc.), also.

我希望这会增加一些多余的绒毛,以便为您做出明智的未来决定.

I hope this adds a little bit of extra fluff to give you an informed decision for future.

这篇关于如果文本框为空,如何禁用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆