7 Replies - 695 Views - Last Post: 25 January 2013 - 01:24 PM Rate Topic: -----

#1 KFredje  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 06-September 11

Unable to cast object of type problem

Posted 23 January 2013 - 02:38 PM

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?

This post has been edited by KFredje: 23 January 2013 - 02:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Unable to cast object of type problem

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Unable to cast object of type problem

Posted 23 January 2013 - 02:44 PM

Notice that you are trying to cast sender to a PictureBox in the first bit of code.

I'm guessing that code is in a button's click event handler method (or in some other event associated with a button), thus making sender a button, not a picture box.

You've called the "sender" argument for the picture box's MouseEnter and MouseLeave event handler lambdas, s, so you should be casting s to a PictureBox, not sender:

PictureBox p = new PictureBox();
p.Name = "p" + i.ToString();
p.MouseEnter += (s, ex) => { ((Panel)flowLayoutPanel2.Controls[((PictureBox)s).Parent.Name]).BorderStyle = BorderStyle.FixedSingle; };
p.MouseLeave += (s, ex) => { ((Panel)flowLayoutPanel2.Controls[((PictureBox)s).Parent.Name]).BorderStyle = BorderStyle.None; };


This post has been edited by CodingSup3rnatur@l-360: 23 January 2013 - 02:52 PM

Was This Post Helpful? 3
  • +
  • -

#3 KFredje  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 06-September 11

Re: Unable to cast object of type problem

Posted 23 January 2013 - 02:56 PM

Thanks a lot. It appears I could have fixed this myself if I looked a little longer into it.

PS. Is there a way to make this shorter?
((Panel)flowLayoutPanel2.Controls[((PictureBox)s).Parent.Name]).BorderStyle = BorderStyle.FixedSingle;

Was This Post Helpful? 0
  • +
  • -

#4 b0zhidar  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 21-December 12

Re: Unable to cast object of type problem

Posted 24 January 2013 - 02:25 AM

Can you post how you fix it? So if someone search about this question finds the right answer :^:
Was This Post Helpful? 0
  • +
  • -

#5 KFredje  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 06-September 11

Re: Unable to cast object of type problem

Posted 24 January 2013 - 06:47 AM

View Postb0zhidar, on 24 January 2013 - 02:25 AM, said:

Can you post how you fix it? So if someone search about this question finds the right answer :^:/>

Fix is in Post #2
Was This Post Helpful? 0
  • +
  • -

#6 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,676
  • Joined: 24-August 11

Re: Unable to cast object of type problem

Posted 24 January 2013 - 01:45 PM

Why are you casting flowLayoutPanel2 to a panel every single time you use it?

This post has been edited by h4nnib4l: 24 January 2013 - 01:46 PM

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,158
  • Joined: 05-May 12

Re: Unable to cast object of type problem

Posted 24 January 2013 - 07:46 PM

View PostKFredje, on 23 January 2013 - 04:56 PM, said:

Thanks a lot. It appears I could have fixed this myself if I looked a little longer into it.

PS. Is there a way to make this shorter?
((Panel)flowLayoutPanel2.Controls[((PictureBox)s).Parent.Name]).BorderStyle = BorderStyle.FixedSingle;


Yes.
(Panel)((PictureBox)s).Parent.BorderStyle = BorderStyle.FixedSingle;


Was This Post Helpful? 2
  • +
  • -

#8 KFredje  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 131
  • Joined: 06-September 11

Re: Unable to cast object of type problem

Posted 25 January 2013 - 01:24 PM

View Posth4nnib4l, on 24 January 2013 - 01:45 PM, said:

Why are you casting flowLayoutPanel2 to a panel every single time you use it?
What do you mean?


View PostSkydiver, on 24 January 2013 - 07:46 PM, said:

View PostKFredje, on 23 January 2013 - 04:56 PM, said:

Thanks a lot. It appears I could have fixed this myself if I looked a little longer into it.

PS. Is there a way to make this shorter?
((Panel)flowLayoutPanel2.Controls[((PictureBox)s).Parent.Name]).BorderStyle = BorderStyle.FixedSingle;


Yes.
(Panel)((PictureBox)s).Parent.BorderStyle = BorderStyle.FixedSingle;

Thanks a lot!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1