Results 1 to 2 of 2

Thread: How to use Up/Dn cursor keys (partially working)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2004
    Posts
    64

    How to use Up/Dn cursor keys (partially working)

    I'm very new to vb.NET.

    I have Label1, Label3 and Button1 on Form1. I want to use the cursor Up/Dn keys to control whether a counter counts up or counts down.

    The only code I found (that I had any clue about at all) checked the alpha keys but not the cursor Up/Dn keys. My code below uses the letters a and s for the up/dn functionality.

    The first Sub, changing the increment value Incr, works OK if the Button control is removed from the form but does nothing when the button is on the form -- NO Errors generated.

    Here's my code:

    Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim Incr As Integer

    Private Sub aKeyPressHandler(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
    '-Label1, Label3 and Button1 on Form1
    '-This Sub works OK if Button1 is removed from Form1 but DOES NOTHING
    '-if Button1 is on Form1.

    'Dim Incr As Integer (Correction - this is commented out - using the Global Dim)
    'Toggle Increment -- a: Incr positive, s: Incr negative
    If e.KeyChar = "a" Then Incr = 1
    If e.KeyChar = "s" Then Incr = -1
    If (e.KeyChar = "a" Or e.KeyChar = "s") Then Label3.Text = Label3.Text & e.KeyChar & " = " & Str(Incr)
    'Button1.PerformClick()
    End Sub

    'Private Sub Button1_Click(ByVal sender As System.Object, ByVal e1 As System.EventArgs) Handles Button1.Click
    'Dim count As Integer
    ' count = count + Incr
    ' Label1.Text = Str$(count)
    'End Sub

    End Class

    I want the counter to count ONLY when the Up or Dn cursor key is held down. I was going to use the Button1.PerformClick event to control the counter as shown by the Button1_Click code.

    I understand examples much better than a verbal description.

    Also, how do I neatly imbed my code on the page as I've seen done on other posts?

    Thanks for any help. jj2
    Last edited by jj2; Sep 28th, 2004 at 12:53 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Sep 2004
    Posts
    64
    OK, the following code works for the Up/Down keys. BUT, it still does not work if Button1 has Enable=True. The button can be on the form, just not enabled. It works fine if Enable = False.

    Private Sub a_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    '-Label1, Label3 and Button1 on Form1
    '-This Sub works OK if Button1 is removed from Form1 but DOES NOTHING
    '-if Button1 is on Form1.

    'Dim Incr As Integer
    'Toggle Increment -- a: Incr positive, s: Incr negative
    If e.KeyCode = Keys.Up Then Incr = 1
    If e.KeyCode = Keys.Down Then Incr = -1
    If (e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down) Then Label3.Text = Label3.Text & e.KeyCode & " = " & Str(Incr)
    'Button1.PerformClick()
    End Sub
    Last edited by jj2; Sep 28th, 2004 at 12:51 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width