PictureBox GotFocus/Enter events

I have a Picturebox and TextBox on a usercontrol.  I want to draw a border around the the usercontrol when the usercontrol (or anything else in it) is selected.  I have a sub that draws the border around the control.  The sub is called from the Enter event of the UserControl and the TextControl.  However, when I try to do the same thing with the PictureBox, I don't see an Enter event listed for the PictureBox (strange as it is inherited from control) and while the GetFocus event is available, it does not seem to fire.  Do I have to enable the focus events in the PictureBox somehow   The PictureBox is already set to both Visible and Enabled = True.

Any suggestions   Is their an easier way to draw a border around a user control when it is selected that I am missing

I'm using VB2003, Framework 1.1

Thanks


Answer this question

PictureBox GotFocus/Enter events

  • Beav1810

    Thank you.  I couldn't find anything about that behavior in the documentation and I'm sure I would have never found it without your help.

  • Kees van der Oord

    Actually, after I tried this solution, I found that it doesn't work -- at least by itself.  I had to force the picture control to select itself before the events would fire.  Thus I added:

    Private Sub PictureBox1_Mousedown(...) handles PictureBox1.MouseDown

        Me.PictureBox1.Select

    End Sub

    I don't know if this would have solved the problem without making the other change suggested above, since I didn't go back after making the suggested modification.

  • Mario02

    I'm not sure why this is happening. I just tried playing around with it and I couldn't get the mouse to select the control, whereas if I derived from Control I could.

    I had a look using Reflector, and if control sees a mouse down and the control is selectable (indicated by ControlStyles.Selectable) then focus is moved to it. So I'm not sure what is happening.

    Also one thing I forget: you want to also set TabStop to true in the constructor so this will allow you to press TAB to move to it.

  • _ _

    PictureBox does have a leave event, it is just hidden in the designer as the default picture box cannot have 'focus'.

    What you need to do is create a new class and derive from the PictureBox control. In its constructor do the following:


    Public Sub New()
            SetStyle(ControlStyles.Selectable, True);
    End

     


    This will indicate to the framework that it can get focus and therefore the focus events (Enter,  Leave, GotFocus and LostFocus) will be raised.

  • PictureBox GotFocus/Enter events