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

I have this code, anything is working ... except this line Shapes.DoubleClick += new EventHandler((o, a) => Process.Start(reader[1].ToString()));

The error says

No data exists for the row/column.

I want a fix so that every created picturebox to have it`s own eventhandler to start a process.

Paradox
  • 91
  • 1
  • 8

1 Answers1

2

You need to create local variable, try this:

var file = reader[1].ToString();

Shapes.DoubleClick += new EventHandler((o, a) => Process.Start(file));

otherwise reader[1].ToString() will be evaluated when you DoubelClick, which is already too late.

Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78