Click here to Skip to main content
15,907,001 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have problem with the following code. I want the timer still run when I press the other CommandButton. But it stops and only one runs. Help me, please.

VB
Option Explicit
Dim tora As Integer
Private Sub Command1_Click()
tora = 1
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
tora = 2
Timer1.Enabled = True
End Sub
Private Sub Command3_Click()
tora = 3
Timer1.Enabled = True
End Sub
Private Sub Command4_Click()
tora = 4
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Select Case tora
    Case 1
        Label1.Caption = Val(Label1.Caption) + 1
    Case 2
        Label2.Caption = Val(Label2.Caption) + 1
    Case 3
        Label3.Caption = Val(Label3.Caption) + 1
    Case 4
        Label4.Caption = Val(Label4.Caption) + 1
End Select
End Sub
Posted

I think that what you mean is "I want multiple labels to change when the timer ticks."

If so, then the way you are doing it is not going to work: when a button is pressed, it overwrites what the other buttons have done.
Instead, set up four bool variables, one for each button, and initially make them false.
In each button routine, invert the status of one of these variables (if it is true, make it false. If it is false, make it true)
In your timer event, check each variable separately, and if it is true, update the appropriate label. You can't use Select Case for this: you want four If statements.
 
Share this answer
 
Comments
derodevil 22-May-11 11:56am    
It's still amusing. I'm a novice programmer. Would you like to write the codes for me, please?
derodevil 22-May-11 12:20pm    
Thanks sir. I have solved it. Thanks for your answer.
OriginalGriff 22-May-11 15:19pm    
You are welcome - I thought you would, but I was away from my desk for a few hours.
derodevil 26-May-11 15:51pm    
Would you like to help me find what's wrong with my code, please? I can't find elsewhere. I'm sorry my English is bad. I'm Indonesian.

Option Explicit

Dim i As Integer
Dim LM As ListItem
Dim zIndex As Integer
Dim tmrIndex As Integer

Private Sub Command1_Click()
ListVitems
zIndex = zIndex + 1
tmrIndex = tmrIndex + 1
For tmrIndex = tmrIndex To tmrIndex
Load Timer1(tmrIndex)
Timer1(tmrIndex).Enabled = True
Next tmrIndex
End Sub

Private Sub Form_Load()
With ListView1
.View = lvwReport
.Sorted = True
.ColumnHeaders.Add , , "Membership", 1100
.ColumnHeaders.Add , , "Number", 2000
.ColumnHeaders.Add , , "Duration", 1000
.GridLines = True
End With
End Sub

Sub ListVitems()
Set LM = ListView1.ListItems.Add(, , "Member")
With LM
.SubItems(1) = "Member-" & CStr(zIndex)
End With
End Sub

Private Sub Timer1_Timer(Index As Integer)
Set LM = ListView1.ListItems(zIndex)
i = i + 1
With LM
.SubItems(2) = i
End With
End Sub
You are referring to the same Timer class. "Timer1". For every Command_Click you enable the same timer.
Only 1 timer is running, not 4.
 
Share this answer
 
Comments
derodevil 22-May-11 11:59am    
So, how to make all 4 label captions change (added by 1) with only one timer?
Kim Togo 22-May-11 14:11pm    
Did you find a solution ?
derodevil 23-May-11 11:37am    
Yes I did.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900