Results 1 to 5 of 5

Thread: Form Key Down - RESOLVED

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2003
    Location
    Porto Alegre, RS
    Posts
    210

    Form Key Down - RESOLVED

    How can i make to add the key down event to all objects on my form?

    I made a form with just 6 buttons, and i have to add to each buton manually.

    But on the main form, i have a lot of objects, and just add key down to main form object don´t work.

    Any tips?!

    Thank you,
    Guilherme Costa
    Last edited by gccosta; Aug 24th, 2004 at 08:13 AM.

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    It is possible to have multiple handles statements for the same event, i.e. see the below example:
    VB Code:
    1. Private Sub Form1_KeyDown(ByVal sender As Object, _
    2. ByVal e As System.Windows.Forms.KeyEventArgs) _
    3. Handles MyBase.KeyDown, Button1.KeyDown, Button2.KeyDown
    4.  
    5. End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2003
    Location
    Porto Alegre, RS
    Posts
    210
    It have some way to do all this handles automatically?


    Thank you,
    Guilherme

  4. #4
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, _
    2. ByVal e As System.EventArgs) Handles MyBase.Load
    3.     Dim intCtlCounter as Integer
    4.    
    5.     For intCtlCounter = 0 to me.Controls.Count - 1
    6.         addhandler me.Controls(intCtlCounter).KeyDown, _
    7.         addressof KeyPressed
    8.     next intCtlCounter
    9. End Sub
    10.  
    11. Private sub KeyPressed (ByVal sender As Object, _
    12. ByVal e As System.Windows.Forms.KeyEventArgs)
    13.     msgbox (ctype(sender, control).name & _
    14.     " had the event!")
    15. End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  5. #5
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    this?
    VB Code:
    1. Class clicker
    2.       Sub New(ByVal c() As Control)
    3.          Dim i As Integer
    4.          For i = 0 To c.Length - 1
    5.             AddHandler c(i).KeyDown, AddressOf c_keydown
    6.          Next
    7.       End Sub
    8.  
    9.       Sub c_keydown(ByVal sender As Object, ByVal e As KeyEventArgs)
    10.          ' do something
    11.       End Sub
    12.    End Class
    13.  
    14.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    15.       Dim dummy As New clicker(New Control() {Me, Button1, Button2, Button3, _
    16.          Button4, Button5, TextBox1, TextBox2, TextBox3})
    17.    End Sub

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