2012-07-30 83 views
0

在我的winform應用程序中;我有登錄表單和主窗體。如何在前臺顯示登錄表單和登錄表單後面的Mainform?

當我運行程序時,我想要登錄窗體頂部和主窗體後面。

一件事是,直到我不使用用戶名和密碼登錄正確,主要形式不應該容易,只有登錄表單應該訪問。

我的語言是C#.Net。

請提供有關如何實現此目的的想法?

+0

而不是簡單地爲後面的登錄表單的主要形式,你可以把它隱藏,直到用戶名/密碼是否正確。 – Ari 2012-07-30 06:33:34

+0

請提及您是否使用母版頁? – user1102001 2012-07-30 06:38:21

+0

主頁出現在asp.net沒有的WinForms @ user1102001 – 2012-07-30 06:43:44

回答

1

使用Form.ShowDialog(顯示形式一個模式對話框)(當第一次顯示的形式出現):

private void Form1_Load(object sender, EventArgs e) 
{ 
    this.Shown += Form1_Shown; 
} 

private void Form1_Shown(object sender, EventArgs e) 
{ 
    LoginForm loginForm = new LoginForm(); 

    if (loginForm.ShowDialog() == DialogResult.Ok) 
    { 
    .... 
    } 
} 

ProgramLoginForm這樣的:

//Progrmm.cs 
Application.Run(new Form1()); 

//LoginForm.cs 
public partial class LoginForm : Form 
{ 
    public LoginForm() 
    { 
     InitializeComponent(); 
    } 

    private void buttonLogin_Click(object sender, EventArgs e) 
    { 
     //check username password 
     if(texboxUser == "user" && texboxPassword == "password") 
     { 
      DialogResult = DialogResult.OK; 
      Close(); 
     } 
     else 
     { 
      MessageBox.Show("Wrong user pass"); 
     } 
    } 
} 
+0

。這不會工作,猜測爲什麼:) – nawfal 2012-07-30 06:49:41

+0

這個代碼是主要形式,哪些代碼應該登錄表單 – 2012-07-30 06:54:55

+0

我應該在Program.cs中寫入文件 – 2012-07-30 07:09:37

0

我一個不喜歡你提出的設計,會想首先顯示登錄表單,那麼MainForm中。但是,如果你確實需要它,那麼你可以做下面的..

在主類:

Application.Run(new frmMain()); 

然後在窗體類:

private void frmMain_Load(object sender, EventArgs e) 
{ 
    //--------------------------------------------- 


    System.Windows.Forms.Timer t = new System.Windows.Forms.Timer(); 
    t.Tick +=new EventHandler(t_Tick); 
    t.Interval = 1000; 
    t.Start(); 
} 

void t_Tick(object sender, EventArgs e) 
{ 
    frmLogin l = new frmLogin(); 
    if (l.ShowDialog(this) == DialogResult.Ok) 
     ((System.Windows.Forms.Timer)sender.Dispose(); 
} 

雖然你必須進一步確保登錄表單不退出不正確的用戶名和密碼(應該是高達你)

使用System.Windows.Forms.Timer,因爲它在同一個線程中運行,因此會阻止CA LLS主窗體上的Form.OnShown事件(不像System.Timers.Timer)

+0

System.Timers.Timer t = new System.Timers.Timer(1000); t.Elapsed + = new System.Timers.ElapsedEventHandler(t_Elapsed); t.Start(); } void t_Elapsed(object sender,System.Timers.ElapsedEventArgs e) frmLogin l = new frmLogin(); if(l.ShowDialog()== DialogResult.Ok) ((System.Timers.Timer)sender.Dispose(); }其中我把這個代碼以登錄形式或主要形式 – 2012-07-30 06:41:35

+0

現在清楚嗎? – nawfal 2012-07-30 06:43:55

+0

如果(l.ShowDialog()== DialogResult.Ok) ((System.Timers.Timer)sender.Dispose();?這部分giveing我的錯誤應該是什麼的登錄表單代碼 – 2012-07-30 06:49:37