2013-04-16 65 views
0

有沒有一種方法可以讓我的主窗體form1在窗體2載入後顯示??我創建了兩個窗體窗體1是主窗體,窗體2是登錄表單我想要當用戶有正確creditials形式1應該自動加載 這是形式2代碼加載主窗體在c sharp中給出兩種形式

private void button1_Click(object sender, EventArgs e) 
     { 
      string username1 = "Richard"; 
      string password1 = "Peugeot"; 

      if (this.textBox1.Text == username1 && this.textBox2.Text == password1) 
       MessageBox.Show("Welcome Richard!", "Welcome"); 
      else 
       MessageBox.Show("Incorrect username or password", "Bad credentials"); 
     } 

這是Form 1代碼

private void Form1_Load(object sender, EventArgs e) 
     { 
      StringBuilder sb = new StringBuilder(String.Empty); 

      sb.AppendLine("Operation System Information"); 
      sb.AppendLine("----------------------------"); 
      sb.AppendLine(String.Format("Name = {0}", OSVersionInfo.Name)); 
      sb.AppendLine(String.Format("Edition = {0}", OSVersionInfo.Edition)); 
      if (OSVersionInfo.ServicePack!=string.Empty) 
       sb.AppendLine(String.Format("Service Pack = {0}", OSVersionInfo.ServicePack)); 
      else 
       sb.AppendLine("Service Pack = None"); 
      sb.AppendLine(String.Format("Version = {0}", OSVersionInfo.VersionString)); 
      sb.AppendLine(String.Format("ProcessorBits = {0}", OSVersionInfo.ProcessorBits)); 
      sb.AppendLine(String.Format("OSBits = {0}", OSVersionInfo.OSBits)); 
      sb.AppendLine(String.Format("ProgramBits = {0}", OSVersionInfo.ProgramBits)); 

      textBox1.Text = sb.ToString(); 
     } 

這是Program.cs的代碼

static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new Form2()); 
     } 
    } 

我想第1到Form2的感謝後自動加載提前

+4

'新Form1的()。展()'? – I4V

+0

我不明白你的問題 – alexzandria

+0

oooooooooooh我明白了讓我試試 – alexzandria

回答

0

哦,我想這是你需要

private void button1_Click(object sender, EventArgs e) 
     { 
      string username1 = "Richard"; 
      string password1 = "Peugeot"; 

      if (this.textBox1.Text == username1 && this.textBox2.Text == password1) 
      { 
       MessageBox.Show("Welcome Richard!", "Welcome"); 
       Form1 frm = new Form1(); 
       frm.Show(); 
       this.Hide(); //If you want to hide the log in form after form1 has loaded 
      } 
      else 
       MessageBox.Show("Incorrect username or password", "Bad credentials"); 
     } 
+0

哦對不起@ I4V – Pyromancer

1

什麼一旦登錄憑據進行驗證和正確,則可以使用以下內容:

Hide(); 
Form1 mainForm = new Form1(); 
mainForm.Show(); 
0

我懷疑你在這裏之後是否只有在用戶驗證時才顯示form1?
如果是這樣,只是在這裏展示的形式:

if (this.textBox1.Text == username1 && this.textBox2.Text == password1) 
{ 
    Form1 form1 = new Form1(); 
    form1.Show(); 
    // close form2? 
} 
else 
... 

你可能想保留一個計數器,如果用戶不中X權的嘗試得到密碼/用戶名組合,只需卸載窗體2(即從其他)。

0

如果Form1是你的主要Form它可能是一個更好的選擇加載Form1第一和顯示Form2的對話框中,如果從Form2DialogResult不是OK退出應用程序否則繼續。

public Form1() 
{ 
    if (new Form2().ShowDialog() != System.Windows.Forms.DialogResult.OK) 
    { 
     Close(); 
    } 
    InitializeComponent(); 
} 

窗體2按鈕事件

private void button1_Click(object sender, EventArgs e) 
{ 
    string username1 = "Richard"; 
    string password1 = "Peugeot"; 

    if (this.textBox1.Text == username1 && this.textBox2.Text == password1) 
    { 
     MessageBox.Show("Welcome Richard!", "Welcome"); 
     DialogResult = System.Windows.Forms.DialogResult.OK; 
    } 
    else 
    { 
     MessageBox.Show("Incorrect username or password", "Bad credentials"); 
     DialogResult = System.Windows.Forms.DialogResult.No; 
    } 
}