【问题标题】:How to disable button if textbox is empty如果文本框为空,如何禁用按钮
【发布时间】:2016-10-28 16:44:19
【问题描述】:

首先对不起我的英语不好。 我是 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);
            }
        }
    }
}

}

【问题讨论】:

  • 处理文本框的TextChanged 事件。在该事件处理程序中,检查文本框的 Text 属性是否为空字符串。如果为空,请禁用按钮:myButton.Enabled = !textBox.Text.IsNullOrEmpty();
  • 处理此类事情的优雅方法是将事件处理程序附加到 Application.Idle 事件并在其中执行您需要的任何状态更新。假设您使用的是 WinForms。

标签: c# windows forms winforms


【解决方案1】:

试试这个:

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

【讨论】:

  • 我刚试过,它是禁用按钮。但是当文本框不为空时,按钮不会改变,它保持禁用状态。
  • @Dren 如果你把这个放在哪里?
  • butoniVerteto_Click
  • 好吧,你需要一种方法来检查这一点,如果当用户与我猜想的文本框交互时,我可能会尝试考虑其他方式。
【解决方案2】:

感谢@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.Textnull 还是字符总数等于0。

这可能带来的问题是,如果用户在文本框中输入一个空格,它就不再是Emptynull;因此此检查将返回true。如果程序的逻辑要求在文本框中输入 实际 值,则此逻辑可能存在缺陷。

  • 另一方面:string.IsNullOrWhiteSpace() 检查将检查 3 个条件 - 如果输入 stringnullEmpty 并且 仅包含空格字符(空格、换行符等.),还有。

我希望这会增加一些额外的内容,以便您在未来做出明智的决定。

【讨论】:

  • 我想知道如果条件是要检查多个文本框,您如何“绑定”文本更改事件?谢谢。
  • @Stu_Dent - 在这种情况下,您需要跟踪所有文本框,并有一些逻辑来检查它们并断言您是否要启用按钮;然后基于此启用按钮。在绑定文本框事件方面,您只需为事件处理程序设置一个更“通用”的名称(例如Textbox_TextChanged),然后运行按钮是否启用的逻辑。然后,要绑定它们,将一个事件处理程序添加到每个文本框 - 例如textbox1.TextChanged += Textbox_TextChanged; textbox2.TextChanged += Textbox_TextChanged;...等
【解决方案3】:

在 txtbox 中添加一个编辑文本的事件。当文本发生变化时,启用按钮

【讨论】:

    【解决方案4】:

    希望它成功!

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

    【讨论】:

      【解决方案5】:

      在 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);
      {
      

      【讨论】:

        【解决方案6】:

        更改 textBox1 为您的文本框名称,然后更改 button1 名称为您的按钮名称

        if (textBox1.Text == "")
          {
            button1.Enabled = false;
          }
        else
          button1.Enabled = true;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-16
          • 2019-07-10
          • 1970-01-01
          • 2017-09-03
          • 2020-06-03
          • 1970-01-01
          相关资源
          最近更新 更多