KeyDown issue:

ALX

Well-known member
Joined
Nov 16, 2005
Messages
253
Location
Columbia, SC
Programming Experience
10+
Of the many forms in my app, there are a few that do not raise the KeyDown event on the 1st time that the Up or Down key is pressed. After the 1st Up key KeyPress, the KeyDown event works fine. The result is that initially the user must press the Up key twice to get any activity on the form. No big deal for the user, but it drives me crazy.... why some forms catch the KeyDown event 1st time, and others don't. They all have the same properties set (KeyPreview = TRUE)... etc.
'Anybody else experience this ?
 
Well, I have fixed this problem, but I don't really understand why, what I've done, resolves this issue.
In the form Load event handler for a base form, my code originally started out as:
VB.NET:
Protected Overridable Sub Graph_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
        Dim rect As New Rectangle
        rect = Screen.GetWorkingArea(New Point(8, 8))
        Me.Size = rect.Size
        Me.MinimumSize = Me.Size

        NumBox1.Location = New Point(296, 76)
        Me.Controls.Add(NumBox1)

	'....  and so on  ....

By changing the sequence of these lines to:
VB.NET:
 Protected Overridable Sub Graph_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        NumBox1.Location = New Point(296, 76)
        Me.Controls.Add(NumBox1)

        Dim rect As New Rectangle
        rect = Screen.GetWorkingArea(New Point(8, 8))
        Me.Size = rect.Size
        Me.MinimumSize = Me.Size

	'....  and so on  ....

solved the KeyDown event issue described in my previous post. NumBox1 is a usercontrol (Inherits System.Windows.Forms.UserControl). It seems that by defining my base form's size prior to adding this control to the form causes this KeyDown event issue.
 
Back
Top