C#为什么我的变量在表单之间没有正确执行?

时间:2014-03-19 19:43:46

标签: c# forms ms-access listbox

我有几种形式,其中一些形式依赖于每种形式之间的信息。在这种情况下,Form 2(AKA testSelect)中所选选项的所选索引是确定Form 3(AKA testPresent)中将发生什么的关键。这被放入一个名为index的整数中。关闭form 2后,index的值肯定是列表框的selectedindex

然而,在form 3打开并应用它时,它一直重置为0,我无法弄清楚原因。以下是在index中使用/确定form 2的代码以及在form 3中调用它的代码。此外,它是在form 2;

开头定义的public int
     private void lstExams_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            //Create a connection object to the DB via string location
            con = new OleDbConnection(connectionString);
            //Open the connection to the DB
            con.Open();

            String sql = "SELECT typeID, description FROM TestConfiguration WHERE examID = " +(lstExams.SelectedIndex +1);
            OleDbDataAdapter da = new OleDbDataAdapter(sql, con);
            //DataSet ds = new DataSet();
            DataTable testConfig = new DataTable();
            //Set the SQL command text

            da.Fill(testConfig);
            lstTestType.DisplayMember = "description";
            lstTestType.ValueMember = "typeID";

            lstTestType.DataSource = testConfig;

            index = lstExams.SelectedIndex + 1;

            MessageBox.Show("INDEX = " + index);

        }
        catch (Exception err)
        {
            //If cannot connect to DB, display the error;
            MessageBox.Show("A Database error has occurred: " + Environment.NewLine + err.Message);
        }
    }

    private void btnStart_Click(object sender, EventArgs e)
    {

        //var testPresent = new testPresent(this);

        testPresent testForm = new testPresent();
        testForm.Show();


        //testForm.difficulty = lstTestType.ValueMember;
        this.Close();

        MessageBox.Show("INDEX IS " + index);
        testForm.eID = (index);


    }

适用于form 3

    public partial class testPresent : Form
{
    public int eID = 0;
    public String difficulty;
    testSelect select = new testSelect();


    //Get the connection path to the DB from the static class
    String connectionString = staticConnectionString.connectionString;
    //Declare connection object
    OleDbConnection con;

    public testPresent()
    {
        InitializeComponent();
    }


    public void testPresent_Load(object sender, EventArgs e)
    {
        try
        {

            int qID;
            Random random = new Random();
            int examID;
            bool valid = false;
            String theQuestion;

            eID += select.index;

            //Create a connection object to the DB via string location
            con = new OleDbConnection(connectionString);
            //Open the connection to the DB
            con.Open();

            MessageBox.Show("eID = " + select.index);

            if (eID == 1)
            {
                ...

            } 

            if (eID == 2)
            {
                ...

            } 

            if (eID == 3)
            {
                ...

            }



        }
        catch (Exception err)
        {
            //If cannot connect to DB, display the error;
            MessageBox.Show("A Database error has occurred: " + Environment.NewLine + err.Message);
        }
    }

哦,是的,这也使用Microsoft Access数据库来填充列表框。

1 个答案:

答案 0 :(得分:0)

您可以设置值:

testForm.eID = (index);

但是表单加载后

testPresent_Load

如果完全需要testPresent testForm = new testPresent(); testForm.Show(); // this is where it uses the value // ... testForm.eID = (index); // this is where you set it 表单的值,并且它需要立即使用该值,请尝试在构造函数中包含该值:

testPresent

然后,当您创建表单的实例时,您必须将该值传递给它:

public testPresent(int eid)
{
    InitializeComponent();
    eID = eid;
}

此时不需要在外部设置值,因此它应该是私有的。 (无论如何,数据成员应该是对象的私有。):

testPresent testForm = new testPresent(index);
testForm.Show();