Disable VB6 Timer during updates - vb6

How do I execute my timer1.enabled=true after TreeView FOR statement has reached the last item. My timer starts counting while the treeview is working still.
This is the code I have so far.
Private Sub Command17_Click()
Dim objRootNode As Node
Dim objChildNode As Node
Dim iRootCounter As Integer
Dim iChildCounter As Integer
Dim countt As Integer
Dim ii As Integer
For iRootCounter = 1 To TreeView2.Nodes.Count
ii = TreeView2.Nodes(iRootCounter).Index
Set objRootNode = TreeView2.Nodes(iRootCounter)
If objRootNode.Image = 4 Then
Set objChildNode = objRootNode.Child ' Gets first child
For iChildCounter = 1 To objRootNode.Children
If objChildNode.Image = 3 Then
objRootNode.Image = 9
End If
Set objChildNode = objChildNode.Next ' Get next node
Next
End If
If TreeView2.Nodes(iRootCounter).Index = TreeView2.Nodes.Count - 0 Then
If startt = True Then
Timer1.Enabled = True
Exit For
End If
End If
Next
End Sub
When I run this code, the treeview items remain in processing mode, meaning it's still doing its job changing image index for each item after I run another code, then this button gets triggered.

Just add Timer1.Enabled = False at the beginning of the TreeView update. Then, set it back to True before exiting.
Better yet, stop the timer in the Timer1_Timer event handler:
Private Sub Timer1_Timer()
' Stop timer until all code is execute
Timer1.Enabled = False
Command17_Click
' Restart timer
Timer1.Enabled = True
End Sub

Related

Compare 2 listview parent and child items and change image index

Listview1,Listview2 compare both parent and child items.
the items are named some-name.zip a-z and child files are loaded in each parent.
Listview1 is Target
Listview2 is my loaded files i want to compare with Listview1
image = 4 'green icon
image = 3 ' Red icon
Compare 2 listview and if it matches then listview2 image = 4 else image = 3
if child matches then image = 4 else image = 3
startt = True
Dim FoundIt As Boolean, ii As Integer, ix As Integer
Dim NodX As Node, NodX2 As Node
For Each NodX In TreeView1.Nodes
ix = NodX.Index
For Each NodX2 In TreeView2.Nodes
ii = NodX2.Index
If NodX.FullPath = NodX2.FullPath Then
FoundIt = True
On Error Resume Next
Exit For
End If
DoEvents
'pause 0
If TreeView2.Nodes(ii).Index = TreeView2.Nodes.Count - 0 Then
Exit For
Exit Sub
End If
Next
If FoundIt Then
TreeView1.Nodes(ix).Image = 4
TreeView2.Nodes(ii).Image = 4
Else
TreeView2.Nodes(ix).Image = 3
End If
If TreeView2.Nodes(ii).Index = TreeView2.Nodes.Count - 0 Then
DoEvents
'Call Command16_Click
If downnn = True Then
Exit For
Exit Sub
End If
End If
FoundIt = False
Next
End Sub
Here's a Sub that takes in two TreeView controls and compares their contents. If a node in the Source TreeView doesn't have a corresponding node in the Target TreeView, its Image gets set to 3. Otherwise, Image is set to 4:
Private Sub CompareTreeViews(ByRef p_objSourceTreeView As TreeView, ByRef p_objTargetTreeView As TreeView)
Dim objSourceNode As Node
Dim objTargetNode As Node
Dim objMatchNode As Node
Dim objSourceChildNode As Node
Dim objTargetChildNode As Node
Dim iSourceCounter As Integer
Dim iTargetCounter As Integer
Dim fFound As Boolean
Dim fChildrenMatch As Boolean
On Error Resume Next
For Each objSourceNode In p_objSourceTreeView.Nodes
' Reset ChildrenMatch flag, used to track if all children match
fChildrenMatch = True
' Find matching node in Source TreeView
For Each objTargetNode In p_objTargetTreeView.Nodes
If objTargetNode.Text = objSourceNode.Text Then
' Match found
Set objMatchNode = objTargetNode
Exit For
End If
Next
If Not objMatchNode Is Nothing Then
' Check all children
If objSourceNode.Children > 0 Then
' Get first Child and Loop through all Children
Set objSourceChildNode = objSourceNode.Child
For iSourceCounter = 1 To objSourceNode.Children
' Check if it exists in Target Treeview
If objMatchNode.Children > 0 Then
' Set Found flag to False
fFound = False
' Get first Child and Loop through all Children
Set objTargetChildNode = objMatchNode.Child
For iTargetCounter = 1 To objMatchNode.Children
' Check for match
If objTargetChildNode.Text = objSourceChildNode.Text Then
fFound = True
Exit For
End If
' Get next node
Set objTargetChildNode = objTargetChildNode.Next
Next
' Mark Node
Select Case fFound
Case True
objSourceChildNode.Image = 4
Case False
objSourceChildNode.Image = 3
fChildrenMatch = False
End Select
' Get next node
Set objSourceChildNode = objSourceChildNode.Next
End If
DoEvents
Next ' Source Child
End If
End If
Select Case fChildrenMatch
Case True
objSourceNode.Image = 4
Case False
objSourceNode.Image = 3
End Select
DoEvents
Next ' Source Node
End Sub
Based on your question, you want to call the Sub this way:
Label1.Caption = "Comparing..."
CompareTreeViews TreeView2, TreeView1
Label1.Caption = "Done!"

Vertical scrollbar change event gets fired twice or more than once even though user click on it once in visual basic

I am working on Visual Basic V6.0 application.
I have a form having vertical scrollbar to change date in calendar and label.
I have attached screenshot of form. When user is clicking only once on highlighted vertical bar arrow - change event gets fired more than once.
I am unable to figure out why is that firing more than once even though we click only once.
I have tried running application in debug more - i don't see any problem.
Also, I have tried putting log msg - which also says - event fired twice.
also tried putting flag variable to exit sub if called second time.
This is not occurring every time but yes, most of the time.
Any suggestion is appreciated.
With warmest regards
Vishal Patel
Private Sub Vscroll2_Change()
Dim newmonth As Integer
Dim pass_date As String
Dim curr_dtime As String
Dim yeard As Variant
Dim chg_date As String
Dim set_new_month As Integer
newmonth = VScroll2.Value
curr_dtime = GetFormTagVar(Me, "CURR_DTIME")
Call SetFormTagVar(Me, "OLD_DTIME", curr_dtime)
yeard = Year(curr_dtime)
'set calendar refresh on if we have changed year or month
If (yeard <> Year(curr_dtime)) Or (Month(CVDate(curr_dtime)) <> newmonth) Then
SetCalRefresh (True)
End If
If Month(CVDate(curr_dtime)) <> newmonth Then
set_new_month = False
If newmonth = 13 Then
newmonth = 1
set_new_month = True
yeard = Year(curr_dtime)
yeard = yeard + 1
End If
If newmonth = 0 Then
newmonth = 12
set_new_month = True
yeard = Year(curr_dtime)
yeard = yeard - 1
End If
'* figure out the new date
If newmonth < 10 Then
pass_date = yeard & "0" & newmonth & "01" & "0000"
Else
pass_date = yeard & newmonth & "01" & "0000"
End If
pass_date = DatePaint(pass_date, True)
Call SetFormTagVar(Me, "CURR_DTIME", pass_date)
Call SetFormTagVar(Me, "NEW_DTIME", "1")
lbldate.Caption = DatePaint(DateParseFnc(pass_date), True)
chg_date = GetFormTagVar(Me, "CURR_DTIME")
If set_new_month Then
Call SetFormTagVar(Me, "NEW_MONTH", YES_FLAG)
VScroll2.Value = newmonth
End If
Call check_calendar(Me)
Call SetupNotesMenuItems
End If
'We're done
End Sub
If set_new_month Then
Call SetFormTagVar(Me, "NEW_MONTH", YES_FLAG)
VScroll2.Value = newmonth
End If
Here you set the value of the scroll bar from within the _Change event. This causes another _Change to fire. When you do this you get the situation you describe. The usual solution is to surround the change with a boolean that indicates that a change is already happening.

vb6 listbox list timer

i want to iterate listbox list item with Timer1 .
for example, if listbox list item have 'A','B','C'
then i want to make run 'A' then run timer1
and after finish 'B' then run timer1 and so on
maybe this is easy for someone but it not easy for me because Timer1 is continue looping
and it make me some confused.
sorry my bad english and anyone could enlight me i really much appreate!
Private Sub Command1_Click()
For xx = 0 To List3.listcount - 1
Timer1.Enabled = True
Next xx
End Sub
Public Sub Timer1_Timer()
some code....
.
.
End Sub
The Timer1_Timer() event fires every interval of the timer - so your code needs to go in there.
Something like this (untested code):
Private Sub Command1_Click()
Timer1.Enabled = True ' Start the timer
End Sub
Public Sub Timer1_Timer()
Static currentIndex = 0
' Do what you want with List1.List(currentIndex)
currentIndex = currentIndex + 1
If (currentIndex = List1.ListCount) Then
Timer1.Enabled = False ' Stop the timer
End if
End Sub
The Interval property of Timer1 controls how often Timer1_Timer() is called.

vb6 check if multiple timers

In my application I got few timers, timer1 too timer5. timer1 activates timer2 and so on setting the previous timer to false.
so I want to make another timer that follow these, like "timer6"
if timer1.enabled = true then 'then it should check if the timer2 now is enabled
if timer2.enabled = true then 'and so on to it reaches timer5..
Need any example to achieve this because I am at a stop point and basically just need this part to work.
my idea was just to do this in the timer6
if timer1.enabled = true then
if timer2.enabled = true then
if etc etc
else
timer6.enabled = true
timer6.enabled = false
end if
end if
end if
end sub
any ideas how to accomplish this?
So.. I am looking for a way to check all the timers are enabled in one condition and disable the last one.
I am not sure you can check al the timers at once but what you can do is to keep array of all the timers corresponding Boolean field.
array = collection[timerState{timer1,true},timerState{timer2,false}]
Then on each timer enabled/disabled event you keep this state array updated.
and lastly wherever you want you will have the state for all the timers.
have a look at the following test project
click the form to start the first timer, click the button to check which timer is active
'1 form with
' 1 timer : name=Timer1 Index=0
' 1 command button : name=Command1
Option Explicit
Private Sub Form_Load()
Dim intIndex As Integer
Timer1(0).Enabled = False
Timer1(0).Interval = 1000
For intIndex = 1 To 5
Load Timer1(intIndex)
Timer1(intIndex).Interval = intIndex * 1000
Next intIndex
End Sub
Private Sub Form_Click()
Timer1(0).Enabled = True
End Sub
Private Sub Timer1_Timer(Index As Integer)
Print CStr(Now)
Timer1(Index).Enabled = False
If Index < Timer1.Count - 1 Then
Timer1(Index + 1).Enabled = True
Else
Print "done"
End If
End Sub
Private Sub Command1_Click()
Dim intIndex As Integer
For intIndex = 0 To Timer1.Count - 1
If Timer1(intIndex).Enabled Then
Caption = "active timer : " & CStr(intIndex)
Exit For
End If
Next intIndex
End Sub

how to keep changing the background image of mdi form?

I am doing a project in vb6..i want to know how to keeping changing the background image of the MDI for as soon as it is loaded.
i tried to make an array of images and then set the timer
here is my code
Private Sub Timer1_Timer()
For i = 0 To 2
Picture1.Picture = LoadPicture(arr(i))
i = i + 1
If i = 3 Then
i = 0
End If
Next i
End Sub
Private Sub MDIForm_Load()
arr(0) = "images\Shop.jpg"
arr(1) = "images\Display1.jpg"
arr(2) = "images\Display2.jpg"
end sub
please help
thank you
Couple of issues with your code as written. First, you don't need to use a picturebox, the MDI form should have a Picture property you can set directly.
Second, as written you're cycling through all the images in your array each time the timer event fires. What you really want is one change per timer event and store/increment the array index at the end of each timer event, like so:
Private Sub Timer1_Timer()
MDIForm.Picture = LoadPicture(arr(arrIndex))
If arrIndex + 1 <= UBound(arr) Then
arrIndex = arrIndex + 1
Else
arrIndex = 0
End If
End Sub
Dim arrIndex as Integer
Private Sub MDIForm_Load()
arr(0) = "images\Shop.jpg"
arr(1) = "images\Display1.jpg"
arr(2) = "images\Display2.jpg"
arrIndex = 0
end sub
The Dim of arrIndex should be at the top of your MDIForm. This will change the background picture every time the timer event fires.

Resources