Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Label flickers when contents change

Status
Not open for further replies.

katenka5

ISP
Nov 30, 2001
15
US
I have two labels where contents change during run-time. Caption changes on one depending on the actions and on the other one I have the timer. When both of them change their captions they flicker, how do I get rid of that? I tried to make them transparent or non-transparent, play with different option but none seemed to work.
 
You might try invoking the refresh method of the label immediately after assigning it a new caption.

lblTheLable.Caption = "this is the text"
lblTheLable.Refresh Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Actually, my guess is that this may make it worse. I amguessing that there is some code (in addition to the timer) like before (or after) update in both which causes the other to change. Even if the 'change' is to the same value as already exists, it is seen as a change (UPDATE), so each 'change' starts another cycle of updates. In effect, you have created the classic endless loop.

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
The Lock Window Update API call will eliminate annoying flickering. Your code will execute quicker too. It won’t have the overhead of updating refreshing the control.

Option Explicit
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long

Private Sub Command1_Click()
Dim iCount As Integer
Dim iRetVal As Long

LockWindowUpdate Me.hWnd ' lock form
Do Until iCount = 20000
Me.Label1 = Format$(iCount)
iCount = iCount + 1
Me.Label1.Refresh
Loop
LockWindowUpdate 0 ' unlock form
End Sub
 
I tried it and it did not work. It made the program load for about 15 minutes, but nothing has changed.
 
and 'it' is ????????????????????

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
"it" was what was suggested in the third reply.
At this point "it" is everything what was suggested before.
I tried refresh, I tried doevents and Lock Window update. The labels still flicker.
 
Can you be a little more specific as to what events are triggering each label to change, so that we might be able to reproduce it?
 
There is a timer, the label caption changes every second and shows the seconds elapsed. The second label shows the count of certain keyboard strokes (arrow keys).
 
I think we need to see some code. Putting a short program together that mimics what you outline your program as doing results in no flicker. So it is probably directly related to your particular implementation.
 
Here's part of the code:
Code:
Private Sub gametime_Timer()
Dim hour1 As String, min1 As String, sec1 As String
    sec = sec + 1
    If sec >= 60 Then
        sec = 0
        min = min + 1
    End If
    If min >= 60 Then
        min = 0
        hour = hour + 1
    End If
    If hour >= 24 Then
        hour = 0
    End If
    
    hour1 = CStr(hour)
    min1 = CStr(min)
    sec1 = CStr(sec)
    
    timetxt.Caption = hour1 & ":" & min1 & ":" & sec1
    DoEvents
End Sub
 
At least conceptually, the code shown should be replaced with ~~~~:

Format(Now - MyTime, "hh:mm:ss")


where "MyTime" is declared at a level which keeps it in scope throughout the app. It would also need to be initalized. I would probably do that in the routine (conditionally, and inefficiently).

but then this should have nothint to do with the flicker issue, just a simple matter of laziness being the mother ...


MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
There is not enough information here to see why you are getting a flicker, but min and hour are keywords. How about the code that counts the keyboard strokes.
 
It is usually because of the inadequate system resource for a windows drawing routine. Instead of a label, try this

1. Place a PictureBox in the place of a label.
2. Set the Picturebox's font property to wot ever u like.
3. Write this code in the timer.

Picture1.Print Format(Now,"mm:dd:yyyy hh:mm:ss")
Picture1.Refresh
4. But i'll suggest the use of Textout API or the DrawText API than just doing all these stuff.

Also i know that Label is a light weight control and a picturebox is a buggy and heavy control. But my post is to solve the guys' problem. All the Best
Praveen Menon
pcmin@rediffmail.com
 
As DrJavaJoe says, you haven't really given us enough code for us to help out.
 
I think that your timer is updating faster than your updates. With each cycle of the timer, it is refreshing the label unneccesarily. Only update the label when you need too.

Try the following:

Private Sub gametime_Timer()
Dim hour1 As String, min1 As String, sec1 As String
sec = sec + 1
If sec >= 60 Then
sec = 0
min = min + 1
End If
If min >= 60 Then
min = 0
hour = hour + 1
End If
If hour >= 24 Then
hour = 0
End If

hour1 = CStr(hour)
min1 = CStr(min)
sec1 = CStr(sec)

If timetxt.Caption <> (hour1 & &quot;:&quot; & min1 & &quot;:&quot; & sec1) then
timetxt.Caption = hour1 & &quot;:&quot; & min1 & &quot;:&quot; & sec1
DoEvents
End if
End Sub
 
Another good practice to get into, when dealing with timers, is to make sure the timer doesn't run into re-entrance problems (like stgovern is pointing out). Turn off the timer when entering the routine and enable it again when exiting.


Private Sub gametime_Timer()
gametime.Enabled = False

... update info

gametime.Enabled = True
End Sub
 
Goodness me!!!!!!!! loads of talk on almost nothing??????? katenka5, the stuff is flickerin because of redrawing problems, generic in windows. Please try an alternate code (many are posted here - including mine) and respond.

All the Best
Praveen Menon
pcmin@rediffmail.com
 
Praveen, if you believe that everything is likely to flicker because of endemic redrawing problems in Windows, surely your advice is actually poor, since you suggest using a control that is likely to have more redrawing issues than a simple label.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top