Allowing Focus to a User Control

vendiddy

Well-known member
Joined
May 14, 2006
Messages
64
Programming Experience
1-3
I have created a user control which displays some graphics that update in response to keyboard events. The problem is that I only want the user control to respond to keyboard events when it has focus.

How do I make it focusable? I've looked at the CanFocus property, but that's ReadOnly.

When I press the down arrow key while my user control has focus, I want the graphics in the user control to change. I don't want the selected cell in the datagrid view to the right of my user control to change.

Thanks! :cool:
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,603
Location
Norway
Programming Experience
10+
The .Focus() method will give the usercontrol focus (if there isn't another control on it like a Textbox that will take the focus). About the arrow you can use the usercontrols PreviewKeyDown event and set e.IsInputKey=True when arrow is detected, then redirect the keys request to your graphics handler.
 

ruben.vuittonet

New member
Joined
Jan 15, 2022
Messages
1
Programming Experience
10+
I have found that this is not necessary.
Put a textbox on the UserControl. When the control is entered, focus the text box.
All key and focus events from that standpoint.
Move it to a location such as -1000, so that it is never visible.
This technique has worked for me 100% of the time
 
Top Bottom