C#为什么我的变量不能在表单之间正确地传递

本文关键字:之间 正确地 表单 为什么 我的 变量 不能 | 更新日期: 2023-09-27 17:50:56

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

然而,在form 3中打开并应用它时,它一直重置为0,我不知道为什么。以下是在form 2中使用/确定index的代码以及在form 3中调用它的代码。此外,它是在form 2开始时定义的公共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数据库来填充列表框。

C#为什么我的变量不能在表单之间正确地传递

您设置值:

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

在这一点上,不需要从外部设置值,所以它应该是私有的。(无论如何,数据成员在对象上都应该是私有的。(:

private int eID;