Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I am making a simple application and I noticed something quite strange so to help explain this problem I created a test application.

I created 2 pictureboxes, the first one I created using code and named it "pic". The second picturebox I created using the toolbox and just left it with the standard "picturebox1" name.

So moving along, I set both of the pictureboxes backcolor to black and I made an event for both of them that will change the backcolor to blue when I click on them.

Now heres the problem, when I click on "picturebox1" it changes to the color blue. But when I click on "pic" it doesn't, why does this happen? How can I make the event work on "pic"?

here is my sample code:

C#
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        PictureBox pic = new PictureBox();

        private void Form1_Load(object sender, EventArgs e)
        {
            pic.BackColor = Color.Black;

            panel1.Controls.Add(pic);

            pic.Location = new Point(200, 100);
        }
        private void pic_MouseClick(object sender, MouseEventArgs e)
        {
            pic.BackColor = Color.Blue;
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            pictureBox1.BackColor = Color.Blue;
        }
    }
}



I will appreciate all help given!

thanks,

MR.AngelMendez
Posted
Comments
vivektiwari97701 11-Oct-12 6:08am    
remove pic.BackColor = Color.Black; and try...
MR. AngelMendez 13-Oct-12 20:09pm    
Its not that, the problem is that I created the picturebox through code and not through the toolbox. I guess I need to find a way to turn that picturebox into an actual control or something.
Himanshu Yadav 11-Oct-12 6:22am    
Go to designer and check wther this code is there or not if not then copy and paste

this.pictureBox2.Click += new System.EventHandler(this.pictureBox2_Click);
Himanshu Yadav 11-Oct-12 6:24am    
this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);

Discard previously
MR. AngelMendez 14-Oct-12 20:55pm    
thanks all for the help, I appreciate it.

1 solution

Hi Inside this you have to initialize your event right click on InitializeComponent() method -> go to definition and copy and paste event handler

C#
public Form1()
       {
           InitializeComponent();

       }


C#
this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);

this.pic.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pic_MouseClick);
 
Share this answer
 
Comments
MR. AngelMendez 13-Oct-12 20:06pm    
hi, sorry I haven't been on lately. I tried your code as suggested but I get debugging error.
------------------------------------------------------------------------------------
'WindowsFormsApplication1.Form1' does not contain a definition for 'ANDpic_MouseDown' and no extension method 'ANDpic_MouseDown' accepting a first argument of type 'WindowsFormsApplication1.Form1' could be found (are you missing a using directive or an assembly reference?)
------------------------------------------------------------------------------------

Any suggestions?

Thanks
MR. AngelMendez 14-Oct-12 20:54pm    
never mind I got your solution to work thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900