C#为什么我的变量不能在表单之间正确传递?
我有几个表单,其中一些依赖于每个表单之间的信息。在这种情况下,C#为什么我的变量不能在表单之间正确传递?,c#,forms,ms-access,listbox,C#,Forms,Ms Access,Listbox,我有几个表单,其中一些依赖于每个表单之间的信息。在这种情况下,表格2(又名testSelect)中所选选项的所选索引是决定表格3(又名testPresent)中将发生什么的关键。这被放入一个称为索引的整数中。关闭表单2后,索引的值肯定是列表框的所选索引的值 然而,在表单3中打开并应用它时,它总是重置为0,我不明白为什么。下面是表格2中使用/确定索引的代码,以及表格3中调用索引的代码。此外,它是在表格2开头定义的公共int private void lstExams_SelectedIn
表格2
(又名testSelect)中所选选项的所选索引是决定表格3
(又名testPresent)中将发生什么的关键。这被放入一个称为索引的整数中。关闭表单2
后,索引的值肯定是列表框的所选索引的值
然而,在表单3中打开并应用它时,它总是重置为0,我不明白为什么。下面是表格2中使用/确定索引的代码,以及表格3中调用索引的代码。此外,它是在表格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);
}
对于表格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数据库填充列表框。您可以设置以下值:
testForm.eID = (index);
但是您在表单已经加载后执行此操作,因此testPresent\u 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;