【发布时间】:2014-03-19 19:43:46
【问题描述】:
我有几个表格,其中一些依赖于每个表格之间的信息。在这种情况下,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# forms ms-access listbox