2015-01-21 73 views
0

我創建了一個加載表單,但我想訪問加載表單並加載其他表單。我知道有一種方法可以實現這一點,但我認爲的方式是創建另一個加載表單並訪問另一個加載表單來加載不同的表單,即使加載表單內容相同。創建一個加載表單並加載其他表單

我該如何做到這一點?

這裏是加載方式的代碼:

public Loading() 
    { 
     InitializeComponent(); 

     this.timer1.Interval = SystemManager.RandomNumberGenerator(1000, 2000); 

     this.timer1.Tick += new EventHandler(this.CheckTimer); 
    } 

    private void Loading_Load(object sender, EventArgs e) 
    { 
     this.timer1.Start(); 
    } 

    private void CheckTimer(object sender, EventArgs e) 
    { 
     uint timeLeft = 1; 

     timeLeft--; 

     if (timeLeft == 0) 
     { 
      this.timer1.Stop(); 

      this.Hide(); 

      AgeConfirmation _ageConfirmation = new AgeConfirmation(); 

      _ageConfirmation.ShowDialog(); 

      this.Close(); 
     } 
    } 

上面的代碼是一個裝載形式和負載達到0.1

另一種形式的時候我已經試過這樣:

public class SystemManager 
{ 
public static void LoadForm(Form _form = null, Form _loadForm = null) 
{ 
    _form.Hide(); 

    _loadForm = new Form(); 

    _loadForm.ShowDialog(); 

    _form.Close(); 
} 
} 

並以如下方式訪問它:

SystemManager.LoadForm(this, AgeConfirmation);

但它引發以下錯誤:

'System.Windows.Forms.AgeConfirmation' is a 'type' but is used like a 'variable'

我的問題是:創建只有一種形式(載表),並獲得該期加載方式和時間,時間達到0,它將訪問不同的形式。

您的回答非常感謝!

非常感謝!

+1

'if(timeLeft == 0)'在此代碼中始終爲真。 – BanksySan 2015-01-21 09:57:48

回答

0

您應該使用_ageConfirmation,它是表單對象,而不是AgeConfirmation,它是表單類型。

SystemManager.LoadForm(this, _ageConfirmation);

-1

變化

_ageConfirmation.ShowDialog(); 

_ageConfirmation.Show(); 
+0

你能解釋一下爲什麼你認爲這是這個問題的答案嗎? – 2015-01-22 04:18:08

0

由我自己解決了!我創建了一個getter和setter int值,並通過使用開關的情況下,代碼將是這樣的:

public class UserInformation 
{ 
    public static int Value 
    { 
     get; 
     set; 
    } 
} 

public partial class Loading : Form 
{ 
    public Loading() 
    { 
     InitializeComponent(); 

     this.timer1.Interval = SystemManager.RandomNumberGenerator(1000, 2000); 

     this.timer1.Tick += new EventHandler(this.CheckTimer); 
    } 

    private void Loading_Load(object sender, EventArgs e) 
    { 
     this.timer1.Start(); 
    } 

    private void CheckTimer(object sender, EventArgs e) 
    { 
     uint timeLeft = 1; 

     timeLeft--; 

     if (timeLeft == 0) 
     { 
      this.timer1.Stop(); 

      this.Hide(); 

      switch (UserInformation.Value) 
      { 
       case 0: 
       AgeConfirmation _ageConfirmation = new AgeConfirmation(); 

       _ageConfirmation.ShowDialog(); 
       break; 

       case 1: 
       MainForm _mainForm = new MainForm(); 

       _mainForm.ShowDialog(); 
       break; 

       case 2: 
       Event _event = new Event(); 

       _event.ShowDialog(); 
       break; 
      } 

      this.Close(); 
     } 
    } 
} 

private void AgeConfirmation_Load(object sender, EventArgs e) 
    { 
     UserInformation.Value = 1; 
    } 

private void button1_Click(object sender, EventArgs e) 
    { 
     this.Hide(); 

     Loading _loading = new Loading(); 

     _loading.ShowDialog(); 

     this.Close(); 
    } 

private void MainForm_Load(object sender, EventArgs e) 
    { 
     UserInformation.Value = 2; 
    } 

private void button1_Click(object sender, EventArgs e) 
    { 
     this.Hide(); 

     Loading _loading = new Loading(); 

     _loading.ShowDialog(); 

     this.Close(); 
    } 

通過程序運行的時候,UserInformation.Value爲0

非常感謝您對於那些誰回答我的問題。