1.先在目標文本框中放置一個label,然后在窗體的代碼頁中添加標簽初始化的方法:
1 public void loadRegister() 2 { 3 lbelPaswrd.Text = "長度不得低於6位"; 4 lbelPaswrd.ForeColor = Color.Gray; 5 Font MyFont = new Font("宋體", 9, FontStyle.Italic); 6 lbelPaswrd.Font = MyFont; 7 }
然后在窗體的Load事件中添加該方法;
2.在該label的屬性頁雙擊MouseDown事件,然后在代碼設計器中添加如下代碼:
1 private void lbelPaswrd_MouseDown(object sender, MouseEventArgs e) 2 { 3 lbelPaswrd.Visible = false; 4 tBoxRPasWd.Focus(); 5 }
tBoxRPaswd就是裝label的目標文本框。
3.在目標文本框的Leave事件中添加如下代碼:
1 private void tBoxRPasWd_Leave(object sender, EventArgs e) 2 { 3 if (tBoxRPasWd.Text == string.Empty) 4 lbelPaswrd.Visible = true; 5 }
4.運行程序,看是否達到效果
我自己用這個方法達到了目的,所以這個方法應該有效。