DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Form_KeyPress in Class

  1. #1
    Join Date
    Dec 2004
    Posts
    717

    Form_KeyPress in Class

    Form1.frm
    Code:
    Private Sub Form_Load()
      Call myFunction 
    End Sub
    Private Sub myFunction()
      Dim c as New MyClass
      Call  c.AddControl(Me) 
    End Sub
    MyClass.cls
    Code:
    Dim WithEvents oForm As Form
    Public Sub AddControl(ByRef objForm As Form)
        Set oForm = objForm
        oForm.KeyPreview = True   '' for KeyPress Event
    End Sub
    
    Private Sub oTestForm_KeyPress(KeyAscii As Integer)
      Debug.Print KeyAscii
    End Sub

    I like to fire oTestForm_KeyPress event but I dont want to write this event in Form1.frm. How can I make it work?
    Any Idea would be appreciated.
    Thanks.
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    I don't think you can; in VB6, event handlers must be in the form whose events they handle. The best you can do is have the Form_KeyPress event procedure in the form call a public method of your class.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    There are two problems:

    1) error is in the line:

    Private Sub oTestForm_KeyPress(KeyAscii As Integer)

    that musy be

    Private Sub oForm_KeyPress(KeyAscii As Integer)

    2) you declared an instance of MyClass (c) inside the Load event, therefore it goes out of scope as soon as the Load event ends. You must declare it static (outside the Load event) like

    private c as MyClass
    private Form_Load()
    set c = new MyClass
    ....

    @Phil: in VB this is a very common tecnique, to "delegate" a class to listen to event, to make the form less crowded. unfortunarelly it creates circolar reference problems, and cannot be used in AciveX projects

    Marco
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  4. #4
    Join Date
    Dec 2004
    Posts
    717
    Thank you so much. mstraf and Phil Weber. :-)


    Form1.frm
    Code:
    Dim c as New MyClass
    Private Sub Form_Load()
      Call myFunction 
    End Sub
    Private Sub myFunction()
      Set  c = New MyClass
      Call  c.AddControl(Me) 
    End Sub
    MyClass.cls
    Code:
    Dim WithEvents oForm As Form
    Public Sub AddControl(ByRef objForm As Form)
        Set oForm = objForm
        oForm.KeyPreview = True   '' for KeyPress Event
    End Sub
    
    Public Sub oForm_KeyPress(KeyAscii As Integer)
      Debug.Print KeyAscii
    End Sub
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

Similar Threads

  1. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 08:10 PM
  2. JDOM Errors & How to set the classpath
    By kpandya in forum Java
    Replies: 0
    Last Post: 01-14-2006, 08:01 PM
  3. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 03:00 PM
  4. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  5. Assembly class
    By Shailesh C.Rathod in forum .NET
    Replies: 2
    Last Post: 03-13-2002, 08:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center