private void checkDesktopItems()
{
SqlCeConnection sqlCnn = new SqlCeConnection("Data Source=" + Application.StartupPath + "\\mainDB.sdf");
SqlCeCommand sqlCmd = new SqlCeCommand("SELECT * FROM desktopItems", sqlCnn);
sqlCnn.Open();
SqlCeDataReader reader = sqlCmd.ExecuteReader();
while (reader.Read())
{
if (top + 52 < DesktopBounds.Size.Height)
{
PictureBox Shapes = new PictureBox();
Shapes = new PictureBox();
Shapes.Size = new Size(32, 32);
Shapes.BackColor = Color.Transparent;
Shapes.Location = new Point(left, top);
Shapes.Image = ReturnIcon(reader[1].ToString()).ToBitmap();
Shapes.Visible = true;
this.Controls.Add(Shapes);
Shapes.DoubleClick += new EventHandler((o, a) => Process.Start(reader[1].ToString()));
Shapes.DoubleClick += new EventHandler((o, a) => this.WindowState = FormWindowState.Minimized);
top += 52;
y++;
}
else
{
x++;
top = 13;
left += 52;
}
}
sqlCnn.Close();
}
我有这段代码,一切正常......除了这一行Shapes.DoubleClick += new EventHandler((o, a) => Process.Start(reader[1].ToString()));
错误说
行/列没有数据。
我想要一个修复,以便每个创建的图片框都有自己的事件处理程序来启动一个过程。
答案 0 :(得分:2)
你需要创建局部变量,试试这个:
var file = reader[1].ToString();
Shapes.DoubleClick += new EventHandler((o, a) => Process.Start(file));
否则reader[1].ToString()
会在您DoubelClick
时进行评估,这已经太晚了。