【问题标题】:c# How do i change the color of a specific textbox if it is empty?c#如果特定文本框为空,如何更改其颜色?
【发布时间】:2012-11-14 15:34:33
【问题描述】:

我正在尝试更改空文本框的颜色,我在此表单上有多个文本框,我希望在用户单击提交时突出显示空文本框。在检查所有文本框是否都有值之后,我在我的 btnSubmit 函数中编写了下面的循环。谁能帮我完成这个循环??

foreach (Control txtbxs in this.Controls)
{
    if (txtbxs is TextBox)
    {
        var TBox = (TextBox)txtbxs;
        if (TBox.Text == string.Empty)
        {
            TBox.ForeColor = Color.Red;
        }
    }

}
lblTopError.Text = "Please fill in the missing billing information";
pnlTopError.Visible = true;

【问题讨论】:

  • 我在这里看到的唯一一件事是您正在更改空字符串的文本颜色,因此您看不到更改。你想做什么?
  • @lc。我想将文本框的边缘更改为红色或以某种方式突出显示文本框,因为用户将其留空......这只是为了让用户清楚这个特定的文本框是空的
  • 考虑查看ErrorProvider,它将在文本框旁边放置一个漂亮的红色感叹号图标,并带有解释错误的工具提示。 (假设你在 winforms 中)

标签: c# error-handling colors textbox


【解决方案1】:

当您的字符串为空时,更改 ForeColor 将无济于事,因为您没有要以红色显示的文本。考虑使用BackColor,并记住在输入文本时有一个事件将其切换回适当的BackColor

【讨论】:

    【解决方案2】:

    如果这是您想要做的,您是否考虑过使用错误提供程序?这将帮助您向用户发出信号并提示他们输入信息。

            errorProvider= new  System.Windows.Forms.ErrorProvider();
            errorProvider.BlinkRate = 1000;
            errorProvider.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink;
    
    
    private void TextValidated(object sender, System.EventArgs e)
        {
           var txtbox = Sender as TextBox;
    
            if(IsTextValid(txt))
            {
                // Clear the error, if any, in the error provider.
                errorProvider.SetError(txtbox, String.Empty);
            }
            else
            {
                // Set the error if the name is not valid.
                errorProvider.SetError(txtbox, "Please fill in the missing billing information.");
            }
        }
    

    【讨论】:

      【解决方案3】:

      你可以像这样应用任何你想要的 CSS:

      TBox.Attributes.Add("style", "color: red; border: solid 1px #FC3000")
      

      我会用这个来代替:

      TBox.ForeColor = Color.Red;
      

      【讨论】:

        【解决方案4】:

        好吧,因为这种形式的文本框不多,所以我走的是简单的路线,它奏效了,代码如下:

        List<TextBox> boxes = new List<TextBox>();
        if (string.IsNullOrWhiteSpace(txtFname.Text))
        {
            //highlightTextBox= txtFname;
            boxes.Add(txtFname);
        }
        if (string.IsNullOrWhiteSpace(txtLname.Text))
        {
            //highlightTextBox = txtLname;
            boxes.Add(txtLname);
        }
        if (string.IsNullOrWhiteSpace(txtAddOne.Text))
        {
            //highlightTextBox = txtAddOne;
            boxes.Add(txtAddOne);
        }
        if (string.IsNullOrWhiteSpace(txtTown.Text))
        {
            //highlightTextBox = txtTown;
            boxes.Add(txtTown);
        }
        if (string.IsNullOrWhiteSpace(txtPostCode.Text))
        {
            //highlightTextBox = txtPostCode;
            boxes.Add(txtPostCode);
        }
        
        foreach (var item in boxes)
        {
            if (string.IsNullOrWhiteSpace(item.Text))
            {
                item.BackColor = Color.Azure;
            }
        }
        lblTopError.Text = "Please fill in the missing billing information highlighted below";
        pnlTopError.Visible = true;
        

        【讨论】:

          猜你喜欢
          • 2017-07-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多