2015-01-05 21 views
0

我正在C#中創建一個Windows窗體應用程序,我正在執行密碼保護。 對於可憐的頭銜和解釋,我很抱歉,因爲你可以告訴我是一個業餘愛好者。從對象C調用初始類#

加載登錄後的形式是被從內部Program.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace POS 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 

     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new Login()); 
     } 
    } 
} 

該類被簡單地稱爲登錄稱爲類。一旦用戶成功通過身份驗證,我將創建一個新類的對象,隱藏登錄表單並打開一個新類。

namespace POS 
{ 
    public partial class Login : Form 
    { 
     RiverbankTeaRooms main = new RiverbankTeaRooms(); 
     private void Enter_Click(object sender, EventArgs e) 
     { 
      bool found = false; 
      try 
      { 
       // Search through database through dr[1] (Table 2) for matching string. 
       foreach (DataRow dr in dra) 
       { 
        if (dr[1].ToString() == Code.Text) 
        { 
         found = true; 

         // Open main form. 
         main.SignedIn = true; 
         main.Show(); 
         this.Hide(); 
         break; 
        } 
       } 
       if (found == false) 
       { 
        // Incorrect password. 

        Code.Text = "Incor"; 
       } 
      } 
      catch 
      { 
       // Error, most likely with Database. 
       Code.Text = "Error"; 
      } 
     } 
    } 
} 

這工作完全正常......要打開該程序,但驗證用戶我希望能夠從RiverbankTeaRooms類中再次註銷,並打開登錄窗體。我不知道如何能夠重新打開表單登錄,因爲它只被隱藏,但我不知道如何去做Login.Show(); 我無法在裏面創建一個新的登錄實例主窗體,因爲RiverbankTeaRooms窗體將無法關閉。

這讓我瘋狂,對不起的解釋感到抱歉!

+0

我想反轉邏輯,以便調用Application.Run(新的RiverbankTeaRooms()),並在啓動時隱藏並顯示登錄。 –

+2

我並不意味着要成爲一個混蛋,但如果你正在寫一個生產POS系統,並且你的團隊中沒有高級人員,那麼你就會頭腦發熱。 – Yaur

+0

這只是一個簡單的計算機項目,它只是一個考試。我只在A2。 我也嘗試了倒邏輯,我不記得我是如何做到的,但不知何故,我認爲使用登錄表單會更好。 – alfers

回答

3

我想建議你改變程序的流程。

取而代之的是從Login成功顯示RiverbankTeaRooms,請在Program.Main中測試登錄結果,然後顯示RiverbankTeaRooms或任何錯誤消息。

以下內容添加到登錄:

public bool Success { get; private set; } 

    public static bool Authenticate() 
    { 
     var login = new Login(); 
     login.ShowDialog(); 

     return login.Success; 
    } 

並更新Enter_Click:

private void Enter_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      // Search through database through dr[1] (Table 2) for matching string. 
      foreach (DataRow dr in dra) 
      { 
       if (dr[1].ToString() == Code.Text) 
       { 
        Success = true; 
        break; 
       } 
      } 
      if (!Success) 
      { 
       // Incorrect password. 
       Code.Text = "Incor"; 
      } 
      else 
      { 
       this.Close(); 
      } 
     } 
     catch 
     { 
      // Error, most likely with Database. 
      Code.Text = "Error"; 
     } 
    } 
} 

,只是使用的身份驗證方法:

static void Main() 
{ 
    Application.EnableVisualStyles(); 
    Application.SetCompatibleTextRenderingDefault(false); 
    Application.Run(new RiverbankTeaRooms() { SignedIn = Login.Authenticate() }); 
} 

如果需要內再次認證RiverbankTeaRooms,你可以這樣做:

if (Login.Authenticate()) 
{ 
    // do stuff... 
} 
else 
{ 
    // show error, or stop the user 
} 
+0

這是一個好主意!你能否給我舉一個例子,說明如何運作。我從來沒有做過這樣的事情。 – alfers

+0

我已經爲您添加了一個示例。 – Xiaoy312

+0

太棒了!非常感謝,希望我能投票給你 – alfers

2

要回答這個掛名的問題,只需將Login實例傳遞給RiverbankTeaRooms對象:

RiverbankTeaRooms main = new RiverbankTeaRooms(this); 

//In other form 
Login myPrivateLoginReference; 
public RiverBanksTeaRoom(Login loginForm) 
{ 
    myPrivateLoginReference = loginForm; 
} 

針對您的特殊情況下,也有其他的辦法,比如從main引發一個事件,這也將工作:

main.ShowLoginRequested += ShowLogin; 

void ShowLogin() 
{ 
    this.Show(); 
} 

順便說一句,請從未存儲密碼作爲數據庫中的明文,因爲它似乎你在幹什麼,你應該總是比較反對散列。

+0

@StevenChorkley添加樣本構造函數,接受登錄對象 – BradleyDotNET

+0

非常感謝。 – alfers