0

我在一个表单上有一个下拉列表,它从 sql 表中填充拼图 id 代码。当用户选择一个拼图代码说“puzzle2”时,我想要下一个表单(播放:表单)来显示这个拼图。目前我只能让它在页面上显示第一个拼图“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。像这样 :

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.通过delegate传递数据

将委托签名添加到 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 回答