1

I have a RichTextBox that I want to display a default (gray color) text "enter text here" before the user writes or when the user deletes his text. The problem is that user can edit the default text.

4

1 回答 1

1

这是您放置并使用GotFocus事件的默认值。

  public Form1()
  {
     InitializeComponent();
     richTextBox1.Text = "enter text here";
     richTextBox1.ForeColor = Color.Gray;
     richTextBox1.GotFocus += new EventHandler(richTextBox1_GotFocus);
     richTextBox1.LostFocus += new EventHandler(richTextBox1_LostFocus);
  }

  void richTextBox1_LostFocus(object sender, EventArgs e)
  {
     if (richTextBox1.Text.Equals(string.Empty))
     {
        richTextBox1.Text = "enter text here";
        richTextBox1.ForeColor = Color.Gray;
     }
  }

  void richTextBox1_GotFocus(object sender, EventArgs e)
  {
     if (richTextBox1.Text.Equals("enter text here"))
     {
        richTextBox1.Text = string.Empty;
        richTextBox1.ForeColor = Color.Black;
     }
  }
于 2013-09-08T11:45:43.913 回答