【问题标题】:C# Verify that 2 textboxes contain textC# 验证 2 个文本框是否包含文本
【发布时间】:2015-08-01 22:49:30
【问题描述】:

我要做的是在文本框中没有文本并且文本确实占据所需文本框时禁用按钮

if (NamePath.Text.Length == 0 && ThemePath.Text.Length == 0)
{
    button1.Enabled = false;
}
else if (NamePath.Text.Length > 0 && ThemePath.Text.Length > 0)
{
     button1.Enabled = true;
     label7.Text = "Press Button To Find a Match";
     label7.ForeColor = Color.PaleGreen;
}

编辑:

我尝试了另一种方法,将文本添加到文本框以尝试触发它,但仍然没有运气:/

            if (NamePath.Text == "yes" && ThemePath.Text == "yes")
        {
            button1.Enabled = false;

        }

        if (NamePath.Text != "yes" && ThemePath.Text != "yes")
        {
            button1.Enabled = true;
            label7.Text = "Press Button To Find a Match";
            label7.ForeColor = Color.PaleGreen;

        }

【问题讨论】:

  • 你的问题是什么?
  • 您能否阐明您希望在代码中看到的内容以及实际看到的内容?我已经尝试过回答,但我正在做出一些假设。
  • 触发了什么事件(运行上面的代码)以及如何连接它们?
  • 我在公共 Form1() 中有它,所以它在打开时启动,它按照代码的指示禁用,它启用它的能力无法正常工作。

标签: c# if-statement text


【解决方案1】:

您是否希望在任一文本框中包含文本时启用该按钮?

如果是这样,您可以使用 else 语句:

if (NamePath.Text.Length == 0 && ThemePath.Text.Length == 0)
{
    button1.Enabled = false;
}
else
{
     button1.Enabled = true;
     label7.Text = "Press Button To Find a Match";
     label7.ForeColor = Color.PaleGreen;
}

就目前而言,当只有一个字段中有文本时,没有任何处理。

如果您想验证两个框是否都包含文本,您可以翻转逻辑。

if (NamePath.Text.Length > 0 && ThemePath.Text.Length > 0)
{
     button1.Enabled = true;
     label7.Text = "Press Button To Find a Match";
     label7.ForeColor = Color.PaleGreen;
} else {
    button1.Enabled = false;
}

【讨论】:

  • 请描述您在问题中看到的内容与您期望看到的内容。我们不知道“不起作用”的实际含义,因此任何进一步的建议都只是猜测。
  • 当 2 个文本框包含文本时,没有任何反应,它们保持禁用状态
  • 可能是您没有使用正确的名称或类似名称来指代他们。我建议在 if 语句之前添加一行来记录框的内容,以查看您的代码认为其中的内容。
猜你喜欢
  • 2013-03-02
  • 2012-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-18
  • 1970-01-01
  • 2017-09-06
相关资源
最近更新 更多