C# 为啥我的变量不能在表单之间正确传递?
Posted
技术标签:
【中文标题】C# 为啥我的变量不能在表单之间正确传递?【英文标题】:C# Why are my variables not carrying over properly between forms?C# 为什么我的变量不能在表单之间正确传递? 【发布时间】: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 数据库来填充列表框。
【问题讨论】:
【参考方案1】:你设置的值:
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;
【讨论】:
以上是关于C# 为啥我的变量不能在表单之间正确传递?的主要内容,如果未能解决你的问题,请参考以下文章