Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1307
  • Last Modified:

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?
0
Tom Knowlton
Asked:
Tom Knowlton
  • 6
1 Solution
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
Tom KnowltonWeb developerAuthor Commented:
This helped....but still having trouble.
0
 
Tom KnowltonWeb developerAuthor Commented:
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now