2015-12-07 123 views
0

我想要做的是:如何將規則添加到textBox textchanged事件?

  1. 當用戶開始在TextBox2中鍵入任何先讓它空,然後開始顯示用戶正在打字。

  2. 然後,如果用戶刪除他正在鍵入的內容,然後再次顯示原始文本。

現在他們做的是,我需要刪除我自己裏面的文字,但後來當我嘗試鍵入任何我再次看到原來的文本,因爲它是空的。

private void textBox2_TextChanged(object sender, EventArgs e) 
     { 
      if (textBox2.Text != "" || textBox2.Text == "Enter Email Account") 
      { 
       textBox2.Text = "Enter Email Account"; 
       button2.Enabled = false; 
      } 
      else 
      { 
       button2.Enabled = true; 
      } 
     } 
+1

你真正想要做的就是叫'水印',使用這個關鍵字來搜索答案。例如http://stackoverflow.com/questions/4902565/watermark-textbox-in-winforms – Eric

+2

實際上它被稱爲CueText。水印是該鏈接中的海報所稱的。 – Plutonix

回答

1

您應該使用水印。但是,如果您希望在開始時執行此操作,則可以在屬性中設置默認值。 您需要首先創建您的事件處理程序輸入請假事件;

private void Form1_Load() 
    { 
     textBox1.Text = "Enter Email Account"; 
     textBox1.GotFocus += new EventHandler(textBox1_GotFocus); 
     textBox1.LostFocus += new EventHandler(textBox1_Leave); 
    } 

然後在輸入,水印文本應當刪除;

protected void textBox1_GotFocus(object sender, EventArgs e) 
    { 
     if (textBox1.Text == "Enter Email Account") 
     { 
      textBox1.Text = ""; 

     } 
    } 

然後你再檢查上離開事件;

protected void textBox1_Leave(object sender, EventArgs e) 
     { 
      if (textBox1.Text != "") 
      { 
       textBox1.Text = "Enter Email Account"; 
      } 
     } 
相關問題