Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having a trouble figuring out how to manage the handlers.
Problem is i have 5 picture box and they have shared handlers for example when I hover over them they get BorderStyle.Fixed3D if mouse leaves then BorderStyle.None occurs that works fine. But here is the problem.. I need to left click the mouse to keep the picturebox Stuck in BorderStyle.Fixed3D but I think it doesn't work because of the MouseLeave event any suggestion how to fix that ?

What I have tried:

private void SetEvents() //Initilize events
        {
            teningur1.MouseHover += PictureBoxes_MouseHover;
            teningur2.MouseHover += PictureBoxes_MouseHover;
            teningur3.MouseHover += PictureBoxes_MouseHover;
            teningur4.MouseHover += PictureBoxes_MouseHover;
            teningur5.MouseHover += PictureBoxes_MouseHover;

            teningur1.MouseLeave += PictureBoxes_MouseLeave;
            teningur2.MouseLeave += PictureBoxes_MouseLeave;
            teningur3.MouseLeave += PictureBoxes_MouseLeave;
            teningur4.MouseLeave += PictureBoxes_MouseLeave;
            teningur5.MouseLeave += PictureBoxes_MouseLeave;

            teningur1.MouseClick += PictureBoxes_MouseClick;
            teningur2.MouseClick += PictureBoxes_MouseClick;
            teningur3.MouseClick += PictureBoxes_MouseClick;
            teningur4.MouseClick += PictureBoxes_MouseClick;
            teningur5.MouseClick += PictureBoxes_MouseClick;
        }


        private void PictureBoxes_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ((PictureBox)sender).BorderStyle = BorderStyle.Fixed3D;
                
            }
            
        }


        //Event handlers
        static private void PictureBoxes_MouseHover(object sender, EventArgs e)
        {
            ((PictureBox)sender).BorderStyle = BorderStyle.Fixed3D;
        }

        static private void PictureBoxes_MouseLeave(object sender, EventArgs e)
        {
            ((PictureBox)sender).BorderStyle = BorderStyle.None;
        }
Posted
Updated 5-Feb-18 5:55am

Every control has a Tag property: you can use it to hold any object.
Set the Tag property of all your pictureboxes to BorderStyle.None
Then in your Hover method:
C#
PictureBox p = sender as PictureBox;
if (p != null)
    {
    p.BorderStyle = BorderStyle.Fixed3D;
    }

In your Leave hander:
C#
PictureBox p = sender as PictureBox;
if (p != null)
    {
    p.BorderStyle = (BorderStyle) p.Tag;
    }
And your Click handler:
C#
PictureBox p = sender as PictureBox;
if (p != null)
    {
    p.BorderStyle = BorderStyle.Fixed3D;
    p.Tag = p.BorderStyle;
    }
 
Share this answer
 
Comments
ThisandThatorWhat 5-Feb-18 11:30am    
NullReferenceException was unhandled

An unhandled exception of type 'System.NullReferenceException' occurred

Additional information: Object reference not set to an instance of an object.

This happens when I hover over the picturebox.

I dont understand this p.Tag

static private void PictureBoxes_MouseLeave(object sender, EventArgs e)
{
PictureBox p = sender as PictureBox;
if (p != null)
{
p.BorderStyle = (BorderStyle)p.Tag;<---- here is the error
}
}
OriginalGriff 5-Feb-18 11:37am    
That's because you ignored the first instruction:
"Set the Tag property of all your pictureboxes to BorderStyle.None"
ThisandThatorWhat 6-Feb-18 17:15pm    
I think I got it but it says
Additional information: Specified cast is not valid. System.InvalidCastException'

PictureBox p = sender as PictureBox;

if (p != null)
{
p.BorderStyle = (BorderStyle)p.Tag;
}
ThisandThatorWhat 6-Feb-18 17:20pm    
I have all the PictureBoxes tags set to BorderStyle.None
But I still get a error InvalidCastException Specified cast is not valid.
OriginalGriff 7-Feb-18 4:43am    
Use the debugger to look at exactly what you have in the Tag when the exception occurs - I don't think it is what you expect, because when I do it here, it works. I suspect that you are setting the tag value in the wrong place, but I can't see your code to be sure.
OriginalGriff's solution is very good, but maybe this one's more convenient and easier to understand for you:

When you click on a PictureBox the border is already set to Fixed3D because of the MouseHover event (btw. better is to use MouseEnter).
So if you want to permanently keep this type of border you can just unsubscribe from the MouseLeave event (you can restore the border mechanism with the right mouse button):
C#
private void PictureBoxes_MouseClick(object sender, MouseEventArgs e)
{
    PictureBox pBox = sender as PictureBox;

    if (e.Button == MouseButtons.Left)
    {
        pBox.MouseLeave -= PictureBoxes_MouseLeave;                
    }
    else if (e.Button == MouseButtons.Right)
    {
        pBox.MouseLeave -= PictureBoxes_MouseLeave; // prevent double subscription
        pBox.MouseLeave += PictureBoxes_MouseLeave;
    }
}
 
Share this answer
 
Comments
ThisandThatorWhat 6-Feb-18 17:12pm    
This seems not to be working. I get the same error
[no name] 6-Feb-18 17:57pm    
You have to remove the 'static' keyword from your eventhandlers:

Change
static private void PictureBoxes_MouseHover(object sender, EventArgs e)
to
private void PictureBoxes_MouseHover(object sender, EventArgs e)

same with the second one (MouseLeave)

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