Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

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?
Avatar of Tom Knowlton
Tom Knowlton
Flag of United States of America image

ASKER

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)
            {
                  
            }
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;
            }
Avatar of eekj
eekj

>>Suggestions?
why not use a tooltip instead....
ToolTip t = new ToolTip();
t.SetToolTip(this.pBox, "This is a picture box that you can click");
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of MaximKammerer
MaximKammerer
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This helped....but still having trouble.