2016-04-05 42 views
0

我正在運行包含10個問題的問卷。第一個Form.cs只是開始按鈕,在新的Form.cs中打開第一個問題。在多個窗體窗口中保持分數

新窗體有3個單選按鈕,每個按鈕應該返回不同的點(0,5,10)。這些點應該積累在所有表單中,然後總計應該在最終Form.cs中顯示或上傳到SQL等。

我試過對它進行編碼,但不知道它是最好的方法。

namespace XX 
{ 
public partial class Form2 : Form 
{ 
    public Form2() 
    { 
     InitializeComponent(); 
    } 

    int points; 
    int totalscore = 0; 

    private void btnCANCEL_Click(object sender, EventArgs e) 
    { 
     this.DialogResult = DialogResult.Cancel; 
    } 

    private void zeropoint_CheckedChanged(object sender, EventArgs e) 
    { 
     if (zeropoint.Checked == true) 
     { 
      points = 0; 
      totalscore = totalscore + points; 
     } 

    } 

    private void fivepoint_CheckedChanged(object sender, EventArgs e) 
    { 
     if (fivepoint.Checked == true) 
     { 
      points = 5; 
      totalscore = totalscore + points; 
     } 
    } 

    private void tenpoint_CheckedChanged(object sender, EventArgs e) 
    { 
     if (tenpoint.Checked == true) 
     { 
      points = 10; 
      totalscore = totalscore + points; 
     } 
    } 

    Form3 thirdForm = new Form3(); 
    private void btnOK_Click(object sender, EventArgs e) 
    { 
     this.DialogResult = DialogResult.OK; 
     this.Hide(); 
     var form3 = new Form3(); 
     form3.FormClosed += (s, args) => this.Close(); 
     form3.Show(); 

    } 

} 
+1

您的文章是不明確關於我的問題或者你在問什麼。 – LarsTech

+2

您可以將分數傳遞給每個表單的構造函數 – n1ff

+0

如果您試圖在表單之間共享不斷變化的信息,則可能需要查看C#委託。 – user2867342

回答

1

我會創建一個類來保存你的分數作爲一個字段。您將在主窗體中存儲對該類對象的引用。當打開一個新窗體時,您可以將對象引用傳遞給該窗體。所有對該對象分數的更改在單獨的表單關閉後仍可通過主表單中的引用進行訪問。

此外,如果您發現您需要額外的共享數據,您可以簡單地將該字段添加到分數類別,以使它們在任何地方均可訪問。

0

你想要一個靜態類

static class Scoring 
{ 
    public static int CurrentPoints { get; set; } 
    public static int TotalScore { get; set; } 

} 

然後調用它

Scoring.CurrentPoints += 10;