2017-05-07 68 views
-1

我應該爲孩子們做一個數學練習程序。他們應該能夠選擇1次操作和數字的數量(1位,2位或3位)。然後根據孩子的選擇輸出10個隨機問題,然後一旦他們完成了測驗,就應該顯示他們的結果以及他們錯誤的問題。 (*)2.(/)3.(+)4.( - ))。我有兩個選擇正在form1,操作和位數,這是分配數字(1.(*)2.(/)3.(+)4.( - ))。我所需要做的就是將操作編號和#位數字傳送給form2,問題將在這裏生成和顯示。在C中的form1和form2之間傳遞數據#

這是我爲Form1的代碼至今:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace FinalProject 
{ 
public partial class Form1 : Form 
{ 
    public static int operation = 0; 
    public static int digits = 0; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 
    // this is to make sure only one box is checked for both selections. Starts here 
    private void label1_Click(object sender, EventArgs e) 
    { 

    } 

    private void button2_Click(object sender, EventArgs e) 
    { 

    } 

    private void MulCB_CheckedChanged(object sender, EventArgs e) 
    { 


     if (MulCB.Checked == true) 
     { 
      operation = 1; 
      DivCB.Checked = false; 
      AddCB.Checked = false; 
      SubCB.Checked = false; 
     } 
    } 

    private void DivCB_CheckedChanged(object sender, EventArgs e) 
    { 
     if (DivCB.Checked == true) 
     { 
      operation = 2; 
      MulCB.Checked = false; 
      AddCB.Checked = false; 
      SubCB.Checked = false; 
     } 
    } 

    private void AddCB_CheckedChanged(object sender, EventArgs e) 
    { 
     if (AddCB.Checked == true) 
     { 
      operation = 3; 
      DivCB.Checked = false; 
      SubCB.Checked = false; 
      MulCB.Checked = false; 
     } 
    } 

    private void SubCB_CheckedChanged(object sender, EventArgs e) 
    { 
     if (SubCB.Checked == true) 
     { 
      operation = 4; 
      DivCB.Checked = false; 
      AddCB.Checked = false; 
      MulCB.Checked = false; 
     } 
    } 

    private void oneDCB_CheckedChanged(object sender, EventArgs e) 
    { 
     if(oneDCB.Checked == true) 
     { 
      digits = 1; 
      twoDCB.Checked = false; 
      threeDCB.Checked = false; 
     } 
    } 

    private void twoDCB_CheckedChanged(object sender, EventArgs e) 
    { 
     if (twoDCB.Checked == true) 
     { 
      digits = 2; 
      oneDCB.Checked = false; 
      threeDCB.Checked = false; 
     } 
    } 

    private void threeDCB_CheckedChanged(object sender, EventArgs e) 
    { 
     if (threeDCB.Checked == true) 
     { 
      digits = 3; 
      oneDCB.Checked = false; 
      twoDCB.Checked = false; 
     } 
    } 
    private void button8_Click(object sender, EventArgs e) 
    { 
     // operations: 1. (*) 2. (/) 3. (+) 4. (-) 
     // digits are as number indicates. 



     // Second window popup. 
     Form2 settingsForm = new Form2(); 
     settingsForm.Show(); 
    } 
} 
} 

這裏的窗口2,赤裸裸的漂亮多了。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace FinalProject 
{ 

public partial class Form2 : Form 
{ 


    public Form2() 
    { 
     InitializeComponent(); 
    } 

    private void FinishedBtn_Click(object sender, EventArgs e) 
    { 


    } 
} 

}

+0

嗨薩爾!你可以編輯你的問題:1.包含更少的代碼,2.說出你已經嘗試了什麼,有沒有工作,3.具體說明你想要發送/接收什麼信息,以及你需要什麼幫助用?另外,我認爲@DourHighArch可能是正確的,這個問題是重複的。 –

+0

如果您願意,可以前往[幫助中心](https://stackoverflow.com/help/how-to-ask)獲取有關如何針對SO提出更合適問題的提示。祝你好運,謝謝! –

+0

您將變量放在錯誤的表單中。 '公共靜態int操作= 0; public static int digits = 0;'應該是在form2中,而不是1.它們是窗體的屬性,可以在調用新實例後設置,如果它(如'Form2 settingsForm = new Form2();' – rudib

回答

0

這可能會實現。 代碼中有評論。

工作流程正在創建類Form2的新實例並設置兩個公共變量。公共意味着他們可以從課外進行訪問(如果需要,請參見here)。然後調用方法Show()並顯示窗體。在Form2代碼中,公共變量現在具有先前指定的值並可以使用。

Form1中:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace FinalProject 
{ 
public partial class Form1 : Form 
{  
    public Form1() 
    { 
     InitializeComponent(); 
    } 
    // this is to make sure only one box is checked for both selections. Starts here 
    private void label1_Click(object sender, EventArgs e) 
    { 

    } 

    private void button2_Click(object sender, EventArgs e) 
    { 

    } 

    private void MulCB_CheckedChanged(object sender, EventArgs e) 
    { 


     if (MulCB.Checked == true) 
     { 
      operation = 1; 
      DivCB.Checked = false; 
      AddCB.Checked = false; 
      SubCB.Checked = false; 
     } 
    } 

    private void DivCB_CheckedChanged(object sender, EventArgs e) 
    { 
     if (DivCB.Checked == true) 
     { 
      operation = 2; 
      MulCB.Checked = false; 
      AddCB.Checked = false; 
      SubCB.Checked = false; 
     } 
    } 

    private void AddCB_CheckedChanged(object sender, EventArgs e) 
    { 
     if (AddCB.Checked == true) 
     { 
      operation = 3; 
      DivCB.Checked = false; 
      SubCB.Checked = false; 
      MulCB.Checked = false; 
     } 
    } 

    private void SubCB_CheckedChanged(object sender, EventArgs e) 
    { 
     if (SubCB.Checked == true) 
     { 
      operation = 4; 
      DivCB.Checked = false; 
      AddCB.Checked = false; 
      MulCB.Checked = false; 
     } 
    } 

    private void oneDCB_CheckedChanged(object sender, EventArgs e) 
    { 
     if(oneDCB.Checked == true) 
     { 
      digits = 1; 
      twoDCB.Checked = false; 
      threeDCB.Checked = false; 
     } 
    } 

    private void twoDCB_CheckedChanged(object sender, EventArgs e) 
    { 
     if (twoDCB.Checked == true) 
     { 
      digits = 2; 
      oneDCB.Checked = false; 
      threeDCB.Checked = false; 
     } 
    } 

    private void threeDCB_CheckedChanged(object sender, EventArgs e) 
    { 
     if (threeDCB.Checked == true) 
     { 
      digits = 3; 
      oneDCB.Checked = false; 
      twoDCB.Checked = false; 
     } 
    } 
    private void button8_Click(object sender, EventArgs e) 
    { 
     // operations: 1. (*) 2. (/) 3. (+) 4. (-) 
     // digits are as number indicates. 



     // Second window popup. 
     // it's the question form, right? 
     Form2 questionForm = new Form2(); 
     //"Write" your settings in the other form's variables 
     //You will have to write code that finds out which checkbox is which number! For now its fixed. 
     questionForm.operation = 2; 
     questionForm.digits = 1; 
     questionForm.Show(); 
     //Hide Form1 
     this.Hide(); 
    } 
} 
} 

窗體2:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace FinalProject 
{ 

public partial class Form2 : Form 
{ 
    public static int operation; 
    public static int digits; 


    public Form2() 
    { 
     InitializeComponent(); 

    } 

    //do NOT paste this. It can be added by creating an event handler 
    // you also might not need this, but this method is called when this Form appears. It's an example. 
    // https://msdn.microsoft.com/en-us/library/zwwsdtbk(v=vs.80).aspx 
    private void Form2_Load(object sender, EventArgs e) 
    { 
     //here you can use your variables for example (also anywhere within this class!) 
     //e.g. 
     Textbox1.Text = (string)operation; 
    } 

    private void FinishedBtn_Click(object sender, EventArgs e) 
    { 


    } 
} 
} 
相關問題