PictureBox MouseHover

When I hover over a Picture Box control.....I want to change the border or something.....to indicate visually to the end-user that they are "selecting" the item if they click on it.

Suggestions?
LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom KnowltonWeb developerAuthor Commented:
For example, in the method

private void pictureMouseHover(object sender, System.EventArgs e)

below....how do I cast sender to a PictureBox control and set the .BorderStyle to   Fixed Single    or    None?



public void InitPictureBoxArray()
            {
                  for(int i=0;i<10;i++)
                  {
                        for(int j=0;j<10;j++)
                        {
                              pBoxArray[i,j] = new System.Windows.Forms.PictureBox();
                              pBoxArray[i,j].Image = gw.gamePieceArray[i,j].pic_box.Image;
                              pBoxArray[i,j].Location=new Point(i*33,j*33);
                              pBoxArray[i,j].Size=new Size(32,32);
                              pBoxArray[i,j].Tag=new Point(i,j);
                              pBoxArray[i,j].Click += new System.EventHandler(pictureClick);                                                            
                              pBoxArray[i,j].MouseHover += new System.EventHandler(pictureMouseHover);
                              this.Controls.Add(pBoxArray[i,j]);
                        }
                  }                  
            }


            private void pictureClick(object sender, System.EventArgs e)
            {                  
                  Point PointClicked;
                  PictureBox PicClicked;
                  PicClicked=(PictureBox)sender;
                  PointClicked=(Point)PicClicked.Tag;
            //      gw.gamePieceArray[PointClicked.X, PointClicked.Y].clickCount++;
                  //MessageBox.Show(PointClicked.X.ToString() + ", " + PointClicked.Y.ToString());
            }

            private void pictureMouseHover(object sender, System.EventArgs e)
            {
                  
            }
0
Tom KnowltonWeb developerAuthor Commented:
Okay.....I've refined it a little bit.......but it is slow.....and seems to not always fire correctly......meaning....I'll leave a picturebox control and the MouseLeave event does not fire and leaves the border style as FixedSingle instead of changing back to None:



            public void InitPictureBoxArray()
            {
                  for(int i=0;i<10;i++)
                  {
                        for(int j=0;j<10;j++)
                        {
                              pBoxArray[i,j] = new System.Windows.Forms.PictureBox();
                              pBoxArray[i,j].Image = gw.gamePieceArray[i,j].pic_box.Image;
                              pBoxArray[i,j].Location=new Point(i*33,j*33);
                              pBoxArray[i,j].Size=new Size(32,32);
                              pBoxArray[i,j].Tag=new Point(i,j);
                              pBoxArray[i,j].Click += new System.EventHandler(pictureClick);                                                            
                              pBoxArray[i,j].MouseEnter += new System.EventHandler(pictureMouseEnter);
                              pBoxArray[i,j].MouseLeave += new System.EventHandler(pictureMouseLeave);
                              this.Controls.Add(pBoxArray[i,j]);
                        }
                  }                  
            }

            private void pictureClick(object sender, System.EventArgs e)
            {                  
                  Point PointClicked;
                  PictureBox PicClicked;
                  PicClicked=(PictureBox)sender;
                  PointClicked=(Point)PicClicked.Tag;
            }

            private void pictureMouseEnter(object sender, System.EventArgs e)
            {
                  System.Windows.Forms.PictureBox pBox = new System.Windows.Forms.PictureBox();
                  pBox = (PictureBox)sender;
                  pBox.BorderStyle = BorderStyle.FixedSingle;
            }

            private void pictureMouseLeave(object sender, System.EventArgs e)
            {
                  System.Windows.Forms.PictureBox pBox = new System.Windows.Forms.PictureBox();
                  pBox = (PictureBox)sender;
                  pBox.BorderStyle = BorderStyle.None;
            }
0
eekjCommented:
>>Suggestions?
why not use a tooltip instead....
ToolTip t = new ToolTip();
t.SetToolTip(this.pBox, "This is a picture box that you can click");
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tom KnowltonWeb developerAuthor Commented:
I do not want to use a tooltip in this particular case.

The images are 32 x 32 squares.  I want to highlight the squares with a box when the mouse enters the PictureBox control...and remove the box when the mouse leaves the PictureBox control.
0
Tom KnowltonWeb developerAuthor Commented:
The code in my third posting above would be fine..........the only problem it has is it leaves the box there when I leave the PictureBox control instead of removing it.
0
MaximKammererCommented:
Just an idea: As you don't always get the MouseLeave event, you could put the code to undo the last highlighting in the MouseEnter event.
Something like this:

private System.Windows.Forms.PictureBox pbHighLighted = null;

private void pictureMouseEnter(object sender, System.EventArgs e)
{
    System.Windws.Forms.PictureBox pBox = (PictureBox) sender;
   
    if (pbHighLighted != null)
        pbHighLighted.BorderStyle = BorderStyle.None;

    pBox.BorderStyle = BorderStyle.FixedSingle;
    pbHighLighted = pBox;
}

private void pictureMouseLeave(object sender, System.EventArgs e)
{
    System.Windws.Forms.PictureBox pBox = (PictureBox) sender;
   
    pBox.BorderStyle = BorderStyle.None;
    pbHighLighted = null;
}

Best regards,
Maxim
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tom KnowltonWeb developerAuthor Commented:
This helped....but still having trouble.
0
Tom KnowltonWeb developerAuthor Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

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.