代码之家  ›  专栏  ›  技术社区  ›  sam

检查文本框是否有数据,然后做些什么

  •  1
  • sam  · 技术社区  · 7 年前

    A groupbox 有几个 Textbox ExecuteReader 现在我想做的是如果只有一个 textbox 文本框 是只读的

    返回数据不象 ExecuteNonQuery 它返回检索到的行数,因此我无法利用它

    还有其他建议吗?

    4 回复  |  直到 7 年前
        1
  •  4
  •   Stephen Kennedy annamataws    7 年前

    您可以使用LINQ执行此操作:

    var textBoxes = groupbox.Controls.OfType<TextBox>();
    
    if (textBoxes.Any(tb => !string.IsNullOrEmpty(tb.Text)))
    {
        foreach (var t in textBoxes)
        {
            t.ReadOnly = true;
        }
    }
    
        2
  •  0
  •   Xnero    7 年前

    尝试:

     if (String.IsNullOrEmpty(textBox1.Text))
     {
        // Do something...
     }
    
        3
  •  0
  •   Goodies    7 年前

    foreach (Control c in groupBox1.Controls)
      if (c is TextBox)
        if (((TextBox)c).Text.Length>0)
          {        
            // there is one textbox with text
            // .. do something, like disabling textboxes ..
            break;
          }
    
        4
  •  0
  •   alxnull    7 年前

    您可以像这样循环浏览文本框(使用Linq):

    // Collection of all text boxes
    var textBoxes = groupbox.Controls.OfType<TextBox>();
    // Check if one text box is not empty
    bool hasText = false;
    foreach (TextBox tb in textBoxes)
        hasText |= !String.IsNullOrEmpty(tb.Text);
    // Set all text boxes to read only
    if (hasText)
    {
        foreach (TextBox tb in textBoxes) tb.ReadOnly = true;
    }
    
    推荐文章