2017-05-07 4 views
-1

私は子供のための数学の練習プログラムを作ることになっています。彼らは1つの操作と数字が持つ数字の量(1、2または3桁)を選択できる必要があります。子供の選択に応じて10のランダムな質問を出してから、クイズを完了すると、その結果が表示され、どの質問が間違っているかが表示されます。C#のform1とform2の間でデータを渡す

フォーム1、操作、桁数には、番号1(*)2(/)3.(+)4( - ))の2つの選択肢があります。私がする必要があるのは、質問を生成して表示するform2に操作番号と桁数を伝えることだけです。ここではかなり多く、裸のForm2だ

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(); 
    } 
} 
} 

はここで、これまでに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 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

変数を間違ったフォームに入れます。 'public static int operation = 0; (Form2 settingsForm = new Form2(); ' – rudib

答えて

0

これはうまくいくかもしれません。 コードにはコメントがあります。

ワークフローは、クラスForm2の新しいインスタンスを作成し、2つのパブリック変数を設定しています。パブリックとは、クラスの外部からアクセスできることを意味します(必要に応じて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(); 
    } 
} 
} 

のForm2:

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) 
    { 


    } 
} 
} 
関連する問題