PictureBox MouseHover

the mousehover events does not work as per in the code.

it is suppose to be when mousehover, a picturebox will appear and when mouseLeave the picture will automatically hide it away.

pls guide. thank u.
private Point mouseLocation = new Point(0, 0);


 public Form1()
        {
            InitializeComponent();

            string ImagePath = Application.StartupPath + @"\PP\";

            string[] Images = new string[] { ImagePath + "Button.jpg"};

            foreach (string img in Images)
            {
                PictureBox PB = new PictureBox();
                PB.Width = 43;
                PB.Height = 43;
                PB.Location = new Point(12, 24);
                PB.Image = Image.FromFile(img);
                PB.MouseDown += new MouseEventHandler(PB_MouseDown);
                this.Controls.Add(PB);

            }
        }

        int x;
        int y;

        void PB_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                x = e.X;
                y = e.Y;


                PictureBox pictureBox = new PictureBox();
                pictureBox.Image = ((PictureBox)sender).Image;
                pictureBox.Width = ((PictureBox)sender).Width;
                pictureBox.Height = ((PictureBox)sender).Height;
                pictureBox.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
                pictureBox.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
                this.Controls.Add(pictureBox);
                pictureBox.BringToFront();

                MouseUPDOWN();

            }
        }



///////the MOUSEHOVER AND MOUSELEAVE codes.
     void PB_MouseHover(object sender, EventArgs e)
        {
            mouseLocation = this.PointToClient(Cursor.Position);
            pictureBox3.Location = mouseLocation;
            pictureBox3.Show();
        }

        void PB_MouseLeave(object sender, EventArgs e)
        {
            pictureBox3.Hide();
        }

Open in new window

securedungeonAsked:
Who is Participating?
 
SiliconXPConnect With a Mentor Commented:
it looks to me like you are trying to make a UI with rollover effects.i stand to be corrected. if that is the case, then you are better off with a ToolStrip.

however for the code you gave, make the following changes and it should work fine

private Point mouseLocation = new Point(0, 0);


 public Form1()
        {
            InitializeComponent();

            string ImagePath = Application.StartupPath + @"\PP\";

            string[] Images = new string[] { ImagePath + "Button.jpg"};

            foreach (string img in Images)
            {
                PictureBox PB = new PictureBox();
                PB.Width = 43;
                PB.Height = 43;
                PB.Location = new Point(12, 24);
                PB.Image = Image.FromFile(img);
                PB.MouseDown += new MouseEventHandler(PB_MouseDown);
                PB.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
                PB.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
                this.Controls.Add(PB);

            }
        }

        int x;
        int y;

        void PB_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                x = e.X;
                y = e.Y;


                PictureBox pictureBox = (PictureBox)sender);
               // pictureBox.Image = ((PictureBox)sender).Image;
               //// pictureBox.Width = ((PictureBox)sender).Width;
                //pictureBox.Height = ((PictureBox)sender).Height;
                //pictureBox.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
                //pictureBox.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
               // this.Controls.Add(pictureBox);
                pictureBox.BringToFront();

                MouseUPDOWN();

            }
        }



///////the MOUSEHOVER AND MOUSELEAVE codes.
     void PB_MouseHover(object sender, EventArgs e)
        {
            mouseLocation = this.PointToClient(Cursor.Position);
            pictureBox3.Location = mouseLocation;
            pictureBox3.Show();
        }

        void PB_MouseLeave(object sender, EventArgs e)
        {
            pictureBox3.Hide();
        }

Open in new window

0
 
mikebirtCommented:
Hi,

rather than calling Show and Hide, use the Visible property. Set it to false to hide your control and true to show it.

HTH

Mike
0
 
securedungeonAuthor Commented:
eh, i wanted the mousehover events handler to imply on the multiple pictureboxs that i had created.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
mikebirtCommented:
Hi,

I Guess i misunderstood your question? I'm not sure i understand what you're trying to achieve and what you want help with exactly. You want to apply your event handlers to all the picture boxes you've created? Or the handlers don't work? Also, in your sample you're calling a method MouseUPDOWN, which is not included in the sample.

If you're able to further clarify your problem we may be able to better target any responses.

HTH

Mike
0
 
securedungeonAuthor Commented:
apply the mousehover and mouseleave events for all the pictureboxes that i had created and applying the codes above does not work for it at all..
0
 
securedungeonAuthor Commented:
it does not work.

what i meant was i had created multiple pictureboxs and they are suppose to have all mousehover events.

however, it does not mousehover for all pictureboxes only got one that has mousehover events.
0
 
securedungeonAuthor Commented:
it been solved.
0
 
SiliconXPCommented:
the solution is provided should be marked as answer. the issue of the getting the events on one picturebox is due to the author load one picture in the image array as seen in the code he submited.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.