Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How can I create an instance of Timer Control at runtime ?

Posted on 2002-04-19
10
Medium Priority
?
280 Views
Last Modified: 2013-11-26
Hi,

I'd like to have an ActiveX for serial port communication.
The MSComm control is not enough, i need to add a timer.

Because I must create many instance of my ActiveX, I must create some timer instance at runtime.
This code doesn't work...

Private myTimer as Object
...
Set myTimer = CreateObject( "VB.Timer" )

Why ?
Please Help Me

Cordially,

    SB.
0
Comment
Question by:steph_beauquis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6953394
2 options:
* either you put 1 timer control at design time to your form, and give as index value 0 (i assume the name is MyTimer).
  At runtime, you can then add a new timer using:
  Load myTimer(1)  
  Load myTimer(2)
  etc

* you can, if you have vb6, also use this (inside the form):
  set myTimer = Controls.Add ( "VB.Timer" ,"myTimer", me )

CHeers
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6953414
To create a timer dynamically you;'d do:

Private WithEvents mtimer As VB.Timer

Private Sub Command1_Click()
    Set mtimer = Me.Controls.Add("VB.Timer", "tm1")
    mtimer.Interval = 6000
    mtimer.Enabled = True
End Sub

Private Sub mtimer_Timer()
    MsgBox mtimer.Name
End Sub
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6953416
WithEvents is the keyword you will need for the coding instance creation.

CJ
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 17

Expert Comment

by:inthedark
ID: 6953480
CJ_S, The only snag I've found using WithEvents is how to make with work with an array.  Have you found a way to make WithEvents work with arrays?
0
 
LVL 5

Accepted Solution

by:
rkot2000 earned 225 total points
ID: 6953514
with activex(dll or exe) am using timer apis
Instead, use these functions in a single code module:

sample with class:
Option Explicit

     Private Declare Function SetTimer Lib "user32" (ByVal _
          hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse _
          As Long, ByVal lpTimerFunc As Long) As Long
     Private Declare Function KillTimer Lib "user32" (ByVal _
          hWnd As Long, ByVal nIDEvent As Long) As Long

     Private m_cb As Object

     Public Function timerSet(lTime As Long, cb As Object) _
          As Long
          Set m_cb = cb
          timerSet = SetTimer(0, 0, lTime, AddressOf _
               timerProcOnce)
     End Function

     Private Sub timerProcOnce(ByVal lHwnd As Long, ByVal _
          lMsg As Long, ByVal lTimerID As Long, ByVal lTime _
          As Long)

          On Error Resume Next
          Call KillTimer(0, lTimerID)
          m_cb.cbTimer
     End Sub

The class module then calls the function like this:
     ...
     timerSet 10, Me
     ...

After 10 milliseconds, the code triggers the cbTimer method in the class module:
Public Sub cbTimer()
     ' Do some stuff
End Sub


sample with regular module :

Insert this code into a bas module:

Dim lTimerId As Long
Private Declare Function SetTimer Lib “user32” (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib “user32” (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long

Private Sub TimerProc(ByVal lHwnd As Long, ByVal lMsg As Long, ByVal lTimerId As Long, ByVal lTime As Long)
    Dim lResult As Long
    lResult = StopTimer(lTimerId)
    Call InsertYourProcessNameHere 'code to be executed after interval
End Sub
 
Public Sub StartTimer(lInterval As Long) 'convert interval to milliseconds prior to passing
    lTimerId = SetTimer(0, 0, lInterval, AddressOf TimerProc)
End Sub
 
Public Function StopTimer(lTimerId As Long) As Long  'must pass the TimerId returned by SetTimer
    StopTimer = KillTimer(0, lTimerId)
End Function

To execute:

Call StartTimer(5000) '5 seconds


0
 
LVL 17

Expert Comment

by:inthedark
ID: 6953515
Here are some other possible ways:

1) Add a dummy form to your class and  load a new instance of the form when the class loads. Set a with events reference to the timer.

2) Use the Win32 API call to create a timer event:


=================Sample code

Declare Function SetTimer Lib "user32" _
                           (ByVal hwnd As Long, _
                            ByVal nIDEvent As Long, _
                            ByVal uElapse As Long, _
                            ByVal lpTimerFunc As Long) As Long

Declare Function KillTimer Lib "user32" _
                           (ByVal hwnd As Long, _
                            ByVal nIDEvent As Long) As Long


' Public Variables

Public TimerID As Long



Sub Open()

' Call this to start the timer ops.
   
TimerID=SetTimer( 0, 0, 8000, AddressOf TimerProc)


End Sub

Sub TimerProc()

' Place your timer code here

End Sub

Sub StopTimer()

' call this in the class terminate

if TimerID>0 then
     TimerID = KillTimer(0, TimerID)
end if


End Sub
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6953540
inthedark,
Only with custom coding, not with the withevents keyword. :-(

For labels and asuch it doesn't matter you can add them anyway, just like buttons. It's just that one there can only be one eventhandler for any object created with the withevents.

CJ
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6953621
You need to manage index i think. I didn't try it.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6953628
I was thinking same as inthedark but it could be a problem if, and only if, your activex is a dll and not an exe.
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6953629
Yes, if you do not want to use events. Otherwise it just is not possible. In VB.Net it sure is possible by using delegates but VB6 no...

CJ
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question