0

SQL テーブルからパズル ID コードを入力する 1 つのフォームにドロップダウン リストがあります。ユーザーがパズル コードを選択すると、「puzzle2」と言うと、次のフォーム (play:Form) でこのパズルを表示する必要があります。現時点では、ページに最初のパズル「puzzle1」を表示することしかできません。

データベースは、パズルの種類である puzzleID と、パズルの質問そのものである puzzle で構成されています。

public partial class Play : Form

{

    public Play()
    {
        InitializeComponent();


        SqlConnection conn = new SqlConnection("Data Source=LAURA-PC;Initial Catalog=Sudoku;Integrated Security=True");
        conn.Open();

        SqlCommand command = conn.CreateCommand();
        command.CommandText = "Select puzzle from Puzzle";
        command.CommandType = CommandType.Text;


        SqlDataReader reader = command.ExecuteReader();
        if (reader.Read())
        {
            string[] tokens = ((string)reader[0]).Split(';');

            textBox1.Text = tokens[0];
            textBox2.Text = tokens[1];
            textBox3.Text = tokens[2];
            textBox4.Text = tokens[3];
            textBox5.Text = tokens[4];
            textBox6.Text = tokens[5];
            textBox7.Text = tokens[6];
            textBox8.Text = tokens[7];
            textBox9.Text = tokens[8];
            textBox10.Text = tokens[9];
            textBox11.Text = tokens[10];
            textBox12.Text = tokens[11];
            textBox13.Text = tokens[12];
            textBox14.Text = tokens[13];
            textBox15.Text = tokens[14];
            textBox16.Text = tokens[15];
        }
        conn.Close();

    }

前のフォームのドロップダウン リストからある種のグローバル変数を実装し、その変数を Where ステートメントにフィルター処理する必要があることはわかっています。しかし、運がありません。

ありがとう

4

2 に答える 2

0

オブジェクトを共有する共通の場所として静的クラスを作成できます。すべてのクラスがこのクラスを表示し、そのメンバーを使用できます。

public static class CommonClass
{
   public static String puzzleCode ;
}

また、パズル ID を受け入れるために、2 番目のフォームにプロパティを設定することもできます。このような :

public class SecondForm : Form
{
   private String _puzzleId ;

   public String PuzzleId
   {
      get { return _puzzleId ; }
      set { _puzzleId = value ; }
   }
}
于 2012-06-07T06:34:03.527 に答える
0

1.フォームに変数を渡す。

Play frmPlay = new Play(puzzleId);
frmPlay.Show();

your Play class will be changed by this

public Play(int puzzleId)
{ 
 //Unchanged code
 command.CommandText = "Select puzzle from Puzzle where puzzleId = "+puzzleId;

 //Unchanged code
}

2.デリゲートを介してデータを渡す

以下のように、委任署名を form1 に追加します。

public delegate void delPassData(int puzzleId);

Play frmPlay= new Play ();
delPassData del=new delPassData(frmPlay.SetPuzzleId);
del( Convert.ToInt32(cmBxPuzzleId.SelectedValue));
frmPlay.Show();

**In Play form write this code**

private int m_PuzzleId;
public void SetPuzzleId(int puzzleId )
{
    m_PuzzleId = puzzuleId; 
}
于 2012-06-07T06:50:56.733 に答える