Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Unable to cast object of type problem

$
0
0
Hi

First of all, this is my bit of code:
PictureBox p = new PictureBox();
p.Name = "p" + i.ToString();
p.MouseEnter += (s, ex) => { ((Panel)flowLayoutPanel2.Controls[((PictureBox)sender).Parent.Name]).BorderStyle = BorderStyle.FixedSingle; };
p.MouseLeave += (s, ex) => { ((Panel)flowLayoutPanel2.Controls[((PictureBox)sender).Parent.Name]).BorderStyle = BorderStyle.None; };

When I enter the area of the PictureBox I get this error:

Quote

Unable to cast object of type System.Windows.Forms.Button to type System.Windows.Forms.PictureBox.

If I do the following it works without a flaw:
PictureBox p = new PictureBox();
p.Name = "p" + i.ToString();
p.MouseEnter += p_MouseEnter;
p.MouseLeave += p_MouseLeave;
.
.
.
void p_MouseEnter(object sender, EventArgs e)
{
    ((Panel)flowLayoutPanel2.Controls[((PictureBox)sender).Parent.Name]).BorderStyle = BorderStyle.FixedSingle;
}
void p_MouseLeave(object sender, EventArgs e)
{
    ((Panel)flowLayoutPanel2.Controls[((PictureBox)sender).Parent.Name]).BorderStyle = BorderStyle.None;
}

How can I get the first bit of code to work?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>