2013-03-18 32 views
1

我想要清除表單上的文本字段後,再次對焦。這裏是我的表單代碼中,我想清楚了名爲user_textboxpwd_textbox如何在返回表單時清除文本字段

namespace RDASMS 
{ 
    public partial class Login : Form 
    { 
     public Login() 
     { 
      InitializeComponent(); 
      user_textbox.Clear(); 
      pwd_textbox.Clear(); 
     } 

     private void register_Click(object sender, EventArgs e) 
     { 
      Registration newuser = new Registration(); 
      newuser.Show(); 
      this.Hide(); 
     } 

     private void submit_login_Click(object sender, EventArgs e) 
     { 
      int checkuser = string.CompareOrdinal(user_textbox.Text, "Admin"); 
      if (checkuser == 0) 
      { 
       int checkpwd = string.CompareOrdinal(pwd_textbox.Text, "rnsit123"); 
       if (checkpwd == 0) 
       { 
        Admin newuser = new Admin(); 
        newuser.RefToLogin = this; 
        newuser.Show(); 
        this.Hide(); 
       } 
       else 
        MessageBox.Show("Invalid Password"); 
      } 
      else 
       MessageBox.Show("Invalid Username"); 
     } 
    } 
} 
+0

[你有什麼嘗試?](http://whathaveyoutried.com) - 你遇到了什麼問題? – Blachshma 2013-03-18 12:35:39

+0

你如何回到主表格?您只顯示隱藏主要代碼並將其他表單帶到前臺的代碼。 – 2013-03-18 12:41:04

+0

好吧,沒有必要在構造函數中清除它們。在'InitializeComponent()'方法中,文本框是_created_。你有沒有看過[當表單獲得焦點並且觸發了什麼潛在的事件](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.gotfocus.aspx)? – Default 2013-03-18 12:47:44

回答

0

由於您的形式,這是等於設定Visiblefalse上調用Hide,您可以使用表格的情形VisibleChanged清除你的TextFields。

private void Form_VisibleChanged(object sender, EventArgs e){ 
    if (this.Visible == true) { 
    user_textbox.Clear(); 
    pwd_textbox.Clear(); 
    } 
} 
0

我使用窗體激活來確定窗體是否回到了焦點。