The keydown event

G

Gerry Viator

Hi all,

I'm trying to do keyboard events and having some problems.
I'm basically switching between two listbox controls. I want to use tab key
to move selected item down and the ctrl key to move up in the list boxes. I
also want
to move from one control to the other with the right and left arrow keys.

Ok, I'm close to this all working:

I start up in the left listbox and the keys work good, when I press the
right arrow the ctrl key doesn't work
in that listbox but, still the selected item is moving in the first listbox?
I have notice the both controls still
have the focus = true? not sure I understand that(I thought when you set one
control to focus the other one looses it's focus).
I do set all tabstops = false when I first load the program so I can
capture the tab event.

***********************************
Dim ctrl As Control
Dim subctrl As Control
Dim subsubctrl As Control

For Each ctrl In Me.Controls
ctrl.TabStop = False
For Each subctrl In ctrl.Controls
subctrl.TabStop = False
For Each subsubctrl In subctrl.Controls
subsubctrl.TabStop = False
Next subsubctrl
Next subctrl
Next ctrl

*************************************

Friend Sub MainListbox_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MainListbox.KeyDown,
ListEntries.KeyDown

If e.KeyCode = Keys.Tab Then

If MainListbox.Focus = True Then

If MainListbox.SelectedIndex < MainListbox.Items.Count - 1
Then
MainListbox.SetSelected(MainListbox.SelectedIndex + 1,
True)
End If

ElseIf ListEntries.Focus = True Then

If ListEntries.SelectedIndex < ListEntries.Items.Count - 1
Then
ListEntries.SetSelected(ListEntries.SelectedIndex + 1,
True)
End If
End If

ElseIf e.KeyCode = Keys.ControlKey Then

If MainListbox.SelectedIndex <> 0 Then
MainListbox.SetSelected(MainListbox.SelectedIndex - 1, True)
Else
If txtmrn.Visible = True Then
MainListbox.SetSelected(0, False)
txtmrn.Select()
End If

End If

ElseIf e.KeyCode = Keys.Left Then

e.Handled = True

ListEntries.SetSelected(0, False)
MainListbox.Focus()

ElseIf e.KeyCode = Keys.Right Then

e.Handled = True

If GroupSublist.Visible = True Then
'Just set the first item.
If ListEntries.Items.Count <> Nothing Then
ListEntries.SetSelected(0, True)
ListEntries.Focus()
End If

End If

End If

End Sub

Thanks for any help
Gerry
 
Y

Ying-Shen Yu[MSFT]

Hi Gerry,

I suspect this problem is might be caused by the order of the underlying
message.
However, we may use SendKey class to send "Up" key when "Tab" key is
pressed, send "Down" key when "Ctrl" is pressed, let the form help us
dispatch it to the right list box and move the right direction.
I modified this the snippet a bit, it works fine in my test program,
<code>
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.ControlKey Then
SendKeys.Send("{DOWN}")
End If
If e.KeyCode = Keys.Tab Then
SendKeys.Send("{UP}")
End If
If e.KeyCode = Keys.Right Then
e.Handled = True
If Me.ActiveControl Is Me.MainListBox Then
Me.MainListBox.ClearSelected()
Me.ListEntries.Select()
If Me.ListEntries.Items.Count <> 0 Then
Me.ListEntries.SetSelected(0, True)
End If
End If
End If
If e.KeyCode = Keys.Left Then
e.Handled = True
If Me.ActiveControl Is Me.ListEntries Then
Me.ListEntries.ClearSelected()
Me.MainListBox.Select()
If Me.MainListBox.Items.Count <> 0 Then
Me.MainListBox.SetSelected(0, True)
End If
End If
End If
End Sub
</code>

Does this way work for you?


Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
G

Gerry Viator

Thanks for your help,

yes this was a big help, just a few questions.

I did change a few things on it, i wanted the tab key to move the selected
down and the Ctrl to move the selected
up. When the form first loads the cursor is on a textbox. When the tab key
is pressed I'm moving
to the MainListBox. There is a beep that sounds when I press the tab key the
first time, from the txtmrn textbox?
Can I disable that beep?

also I had to add: Handles MainListbox.KeyDown, ListEntries.KeyDown this
to the sub
from what you had sent.

Everything seems to work fine except that beep tabbing off the textbox to
the listbox.



Friend Sub FrmMain_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MainListbox.KeyDown,
ListEntries.KeyDown

If e.KeyCode = Keys.Tab Then
SendKeys.Send("{DOWN}")
End If
If e.KeyCode = Keys.ControlKey Then

If Me.MainListbox.SelectedIndex <> 0 Then
SendKeys.Send("{UP}")
Else
If txtmrn.Visible = True Then
Me.MainListbox.SetSelected(0, False)
Me.txtmrn.Select()
End If
End If

End If
If e.KeyCode = Keys.Right Then
e.Handled = True
If Me.ActiveControl Is Me.MainListbox Then
Me.ListEntries.Select()
If Me.ListEntries.Items.Count <> 0 Then
Me.ListEntries.SetSelected(0, True)
End If
End If
End If
If e.KeyCode = Keys.Left Then
e.Handled = True
If Me.ActiveControl Is Me.ListEntries Then
Me.ListEntries.ClearSelected()
Me.MainListbox.Select()
End If
End If

End Sub
Private Sub txtmrn_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles txtmrn.KeyDown
If e.KeyCode = Keys.Tab Then
'Just set the first item.
MainListbox.Select()
MainListbox.SetSelected(0, True)

End If
End Sub


thanks
Gerry




"Ying-Shen Yu[MSFT]" said:
Hi Gerry,

I suspect this problem is might be caused by the order of the underlying
message.
However, we may use SendKey class to send "Up" key when "Tab" key is
pressed, send "Down" key when "Ctrl" is pressed, let the form help us
dispatch it to the right list box and move the right direction.
I modified this the snippet a bit, it works fine in my test program,
<code>
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.ControlKey Then
SendKeys.Send("{DOWN}")
End If
If e.KeyCode = Keys.Tab Then
SendKeys.Send("{UP}")
End If
If e.KeyCode = Keys.Right Then
e.Handled = True
If Me.ActiveControl Is Me.MainListBox Then
Me.MainListBox.ClearSelected()
Me.ListEntries.Select()
If Me.ListEntries.Items.Count <> 0 Then
Me.ListEntries.SetSelected(0, True)
End If
End If
End If
If e.KeyCode = Keys.Left Then
e.Handled = True
If Me.ActiveControl Is Me.ListEntries Then
Me.ListEntries.ClearSelected()
Me.MainListBox.Select()
If Me.MainListBox.Items.Count <> 0 Then
Me.MainListBox.SetSelected(0, True)
End If
End If
End If
End Sub
</code>

Does this way work for you?


Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
G

Gerry Viator

ok, i did this and it seems to work fine to disable the beep

Private Sub txtmrn_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles txtmrn.KeyPress

If e.KeyChar = Chr(9) Then
e.Handled = True
Exit Sub
End If

End Sub

thanks
Gerry
 
Y

Ying-Shen Yu[MSFT]

Hi Gerry,

I'm glad to see you have solved this problem.
The beep is caused by "Tab" key , the "Tab" key now is not handled in
default window procedure, since you disabled the TabStop on it. So you
need set the e.Handled = true for it. I think your workaround is a good way
to go.
This remind me that I missed adding e.Handled = True in my previous snippet
when handling the "Tab" and "Ctrl" key, you may add them as well, although
it seems that they will not cause the beep.

Have a nice Day!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top