Userform ArrowKey KeyPreview Issues

XL Jedi

Active member
Joined
Apr 2, 2007
Messages
44
Location
Florida
Programming Experience
10+
This one's been an annoying thorn in my side...

I've developed a graphics based application on a userform with the GDI. I've set KeyPreview to True so I can add shortcut hotkeys to my app on the keydown event. So far so good...

I've built a "Nudge" tool that moves selected graphic shapes one pixel if the appropriate arrow key is pressed.

Even though I have KeyPreview set to true I'm having 2 problems with the arrow key nudge.

1) If I don't have a textbox on the userform, the arrowkeys are not detected regardless of the KeyPreview set to True.

2) Now that I do have a half-dozen or so textboxes on my userform, the arrowkeys are registering with the form as if I want to use them to advance between the textboxes. After 3 or 4 keypresses it seems to "break-thru" and my intended Nudge starts to work.


At the moment, I'm most interested in overcoming item number 2 above, as I hate the fact that I have to press the arrowkey repeatedly before my nudget is recognized. However, I'd also like to know the answer to item 1 because I may very well want to use the arrowkeys on a form in the future that does not contain textboxes.

Here's a snippet showing my arrow key assignments for the userform keydown event:
VB.NET:
Private Sub Frm_MoBo_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        ' Sets Handled to true to prevent other controls from 
        ' receiving the key if an arrow key was pressed
        Select Case e.KeyCode
            Case Keys.Right, Keys.Left, Keys.Up, Keys.Down
                TxtArrowKeyEnable.Select()
                Nudge(e.KeyCode)
                e.Handled = True

I thought by setting e.Handled to true I was prohibiting the userform textboxes from receiving the arrowkey?
 
Last edited:
I don't fully understand your scenario with form/controls and the order of events so I have no particular suggestion.

Thanks for the link John! I'll review that later today... I have a pretty strong programming background but I find myself in this vb.net transition phase where I can program the graphics and underlying engine, but hooking that engine into the display controls has been a tad frustrating, but I'm gradually overcoming it.

There's a picture of my app, with the 4 textboxes on this thread...
http://www.vbdotnetforums.com/showthread.php?t=19416

You've already been a great help with your TextRenderingHint suggestion.

I'm plotting in the main window, but since the program is heavily graphic oriented, there isn't much need for keyboard data input. When I plot these gadgets I can click to activate and move em around, but I also need to be able to nudge them for fine tuning. That's where the arrowkeys were activating my scaling textboxes outside of the plotting area... which has been very irritating.
 
OK, I've re-read your posts and I think I kind of understand. You have graphic shapes on some sort of control that when selected should move according to the arrow key pressed. The problem is that the arrow keys are moving the focus to the textboxes. Sound correct?
What is the control that the graphic shapes are being drawn on? I'll assume it's a usercontrol. Regardless of what type of control it is, I believe you can solve your problem by subclassing that control. The subclassed control will be very simple, implementing the original control type and containing only the overridden IsInputKey procedure I posted above (you can of course leave out the Enter and Tab key if you wish to use those as originally designed). Then you can simply replace the original control with the subclassed control.

I'm a little confused though. Shouldn't the control that the shapes are on be handling the key processing itself and not the form? It seems that would be a more object-oriented approach. But I don't fully understand how you have implemented the application.

You have mentioned userform several times. I'm not familiar with the term userform. Do you mean the System.Windows.Forms.Form class?
 
OK, I've re-read your posts and I think I kind of understand. You have graphic shapes on some sort of control that when selected should move according to the arrow key pressed. The problem is that the arrow keys are moving the focus to the textboxes. Sound correct?
What is the control that the graphic shapes are being drawn on? I'll assume it's a usercontrol. Regardless of what type of control it is, I believe you can solve your problem by subclassing that control. The subclassed control will be very simple, implementing the original control type and containing only the overridden IsInputKey procedure I posted above (you can of course leave out the Enter and Tab key if you wish to use those as originally designed). Then you can simply replace the original control with the subclassed control.

I'm a little confused though. Shouldn't the control that the shapes are on be handling the key processing itself and not the form? It seems that would be a more object-oriented approach. But I don't fully understand how you have implemented the application.

You have mentioned userform several times. I'm not familiar with the term userform. Do you mean the System.Windows.Forms.Form class?

I've been writing code for over 10 years in VBA... yeah they're forms just used to calling em UserForms in VBA. I've got some stuff now that I'd like to migrate to standalone apps and visual studio was free and after playing with it a bit I like the IDE and all the gadgets exposed to vb.net, the GDI is a fun sandbox to play in.

I've implemented the graphics as follows...

I do all my drawing via the GDI directly to a bitmap array object in memory. I use the mousemove event of the picturebox to update the display by setting the picturebox.image equal to bitmap array Layer1. I keep a background image in Layer0 and use the picturebox.backgroundimage for that part.

I think to answer your question though it's the picturebox that I'm drawing on. The picturebox free-floats within a panel. So I've got a form, containing a panel, containing a picturebox, I'm drawing on the picturebox and using it's mousemove event to redraw.

So you think maybe subclassing the picturebox then?
 
Oh, I see. The problem is that the picturebox control can not receive the focus.
I think the best solution would be to create a usercontrol containing the panel and picturebox. Of course this will lead to other concerns.
 
Oh, I see. The problem is that the picturebox control can not receive the focus.
I think the best solution would be to create a usercontrol containing the panel and picturebox. Of course this will lead to other concerns.

That sounds logical... I haven't played with creating usercontrols yet. I've seen a couple interesting examples. ...and I can see where further study in that direction could probably benefit my project.

I appreciate the diagnosis and suggestions for further study.
 
Back
Top