2012-12-12 72 views
0

我已經爲文本設置了標準文本和顏色,並且當單擊文本框時,我清除了用戶類型文本的文本並定義了黑色。 上單擊事件:C#文本框焦點丟失

if (txtbox.Text == "Ex.: [text test]") 
      { 
       txtbox.Text = string.Empty; 
       txtbox.ForeColor = Color.Black; 
      } 

我想設置一個默認的文本,如果文本框爲空,重點是在另一個文本框,因此,如果用戶點擊或壓片。

+0

您是使用WPF還是Winforms? – HichemSeeSharp

回答

1
private void textBox1_Validating(object sender, EventArgs e) 
    { 
     if (string.IsNullOrWhiteSpace(textBox1.Text)) 
     { 
      //Your logic here or the color you want 
     } 
    } 
0

您可以在Leave事件中設置默認文本。這會在任何時候文本框失去焦點時運行。

private void textBox1_Leave(object sender, EventArgs e) 
    { 
     if (textBox1.Text == String.Empty) 
     { 
      //Set default text here. 
     } 
    } 
0

請填寫在點擊和按鍵事件功能,下面的代碼(用於製表的工作)

If(txtbox.Text == "") 
{ 
    txtbox.Text = "Default"; 
    txtbox1.focus(); //focus will be set to another textbox. 
} 
0

如果有兩個文本框txt1中和TXT2在窗體Form1然後

form1_Load(object sender, System.EventArgs e) 
{ 
    txt2.SetFocus; 
    txt1.text = "Default Text"; 
} 
txt1_Click(object sender, System.EventArgs e) 
{ 
    if(txt1.text == "Default Text") 
    { 
     txt1.text = ""; 
    } 
} 
txt1_Leave(object sender, System.EventArgs e) 
{ 
    if(txt1.text == "") 
    { 
     txt1.text = "Default Text"; 
    } 
} 

我認爲它會工作。讓我知道是否有任何錯誤發生。

相關問題