2011-12-13 63 views
1

我有3個類:Form1,LoginForm和程序。如何關閉this.form

程序包含我的主要方法,然後運行loginform,如果登錄窗體內的條件滿足,然後form1運行。

我想要它做的就是在我展示form1之前隱藏loginform。

我該如何做,因爲我無法使用loginform.hide();

這裏是代碼:

namespace RepSalesNetAnalysis 
{ 
public partial class LoginForm : Form 
{ 
    public bool letsGO = false; 
    public LoginForm() 
    { 
     InitializeComponent(); 
    } 

    private static DataTable LookupUser(string Username) 
    { 
     const string connStr = "Server=10asaf;" + 
          "Database=dfafa;" + 
          "uid=bufaf;" + 
          "pwd=dridfsdf;" + 
          "Connect Timdf0;"; 

     //"Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;"; 

     const string query = "Select password From dbo.UserTable (NOLOCK) Where UserName = @UserName"; 
     DataTable result = new DataTable(); 
     using (SqlConnection conn = new SqlConnection(connStr)) 
     { 
      conn.Open(); 
      using (SqlCommand cmd = new SqlCommand(query, conn)) 
      { 
       cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = Username; 
       using (SqlDataReader dr = cmd.ExecuteReader()) 
       { 
        result.Load(dr); 
       } 
      } 
     } 
     return result; 
    } 

    private void buttonLogin_Click(object sender, EventArgs e) 
    { 

     if (string.IsNullOrEmpty(textUser.Text)) 
     { 
      //Focus box before showing a message 
      textUser.Focus(); 
      MessageBox.Show("Enter your username", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); 
      //Focus again afterwards, sometimes people double click message boxes and select another control accidentally 
      textUser.Focus(); 
      return; 
     } 
     else if (string.IsNullOrEmpty(textPass.Text)) 
     { 
      textPass.Focus(); 
      MessageBox.Show("Enter your password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); 
      textPass.Focus(); 
      return; 
     } 

     //OK they enter a user and pass, lets see if they can authenticate 
     using (DataTable dt = LookupUser(textUser.Text)) 
     { 
      if (dt.Rows.Count == 0) 
      { 
       textUser.Focus(); 
       MessageBox.Show("Invalid username.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); 
       textUser.Focus(); 
       return; 
      } 
      else 
      { 
       string dbPassword = Convert.ToString(dt.Rows[0]["Password"]); 
       string appPassword = Convert.ToString(textPass.Text); //we store the password as encrypted in the DB 
       //MessageBox.Show 
       Console.WriteLine(string.Compare(dbPassword, appPassword)); 

       if (string.Compare(dbPassword, appPassword) == 0) 
       { 
        DialogResult = DialogResult.OK; 
        this.Close(); 
       } 
       else 
       { 
        //You may want to use the same error message so they can't tell which field they got wrong 
        textPass.Focus(); 
        MessageBox.Show("Invalid Password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); 
        textPass.Focus(); 
        return; 
       } 
      } 
     } 

    } 
} 

}

我這麼想嗎?繼承我的主類;

namespace RepSalesNetAnalysis 
{ 
static class Program 
{ 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     LoginForm fLogin = new LoginForm(); 
     if (fLogin.ShowDialog() == DialogResult.OK) 
     { 
      Application.Run(new Form1()); 
     } 
     else 
     { 
      Application.Exit(); 
     } 
    } 
} 
} 
+0

你嘗試This.close()函數.. –

+0

'this.Hide();' – ispiro

+2

我建議將所有的認證代碼移到一個單獨的類中。從表單中取出它。 – Amy

回答

4

史蒂文這一切都是錯誤的。

還有其他方法可以做到你需要正確地爲你的Program類創建一個登錄表單的Main方法且僅當登錄成功實例化和顯示主申請表。

檢查的細節和例子這個提問/回答:How can I close a login form and show the main form without my application closing?

你其實需要這樣的方法:

static void Main() 
{ 
    LoginForm fLogin = new LoginForm(); 
    if (fLogin.ShowDialog() == DialogResult.OK) 
    { 
     Application.Run(new MainForm()); 
    } 
    else 
    { 
     Application.Exit(); 
    } 
} 
+0

好吧,所以我改變了主要的類程序爲你的例子,當我點擊登錄表單上的按鈕什麼也沒有發生,因爲在if語句我沒有實際做任何事情。那裏需要去哪些地方? – lemunk

+0

somthing like this.close? – lemunk

+0

如果通過在登錄表單中將DialogResult設置爲DialogResult.Ok登錄成功關閉表單。 –

0
static class Program 
{ 
    private static bool canLogin; 

    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     using (LoginForm loginForm = new LoginForm()){ 
      // show as dialog 
      // perform logic to check if successful 
      canLogin = SomeStaticClass.VerifyCredentials(loginForm.Credentials); 
      // grab any properties you may want here 
     } 
     //then run the application 
     if(canLogin){ 
      Application.Run(new Form1()); 
     } 
    } 
} 
} 
+0

你確定你可以調用沒有'()'的構造函數,並且沒有邏輯來檢查登錄是否真的完成並且成功 –

+0

OP可以提供其餘的的邏輯。感謝您指出缺少的'()'... – IAbstract

0

您可以設置DialogResultOKCancel在LoginForm的。這將關閉您的LoginForm並將DialogResult返回到Main方法。然後像這樣檢查Main方法的結果。

[STAThread] 
static void Main() 
{ 
    Application.EnableVisualStyles(); 
    Application.SetCompatibleTextRenderingDefault(false); 

    LoginForm form = new LoginForm(); 
    if (form.ShowDialog() == DialogResult.OK) 
     Application.Run(new Form1()); 
} 

希望這有助於