Label Weirdness

  • Thread starter Thread starter Harry Simpson
  • Start date Start date
H

Harry Simpson

I am trying to display/hide a label either by visible or manipilating the
Left property.

Why would a label not appear at runtime on a panel. I've right clicked and
done the Bring to Front deal.

I even used a label that I am able to change the text in. When I add code
to change the text, it won't work for the original function either. What's
going on! I've been trying to solve this all afternoon..

TIA
Harry
 
Chris,

Maybe the problem is that I'm trying to set the property twice within one
proc in the tick event???

Here's the code:
This timer fires every five minutes. IT makes about a thirty second trip to
the server to grab fresh data....I want to alert the user in a more
pronouced way that they need to leave the device cradled until after the
sync.....

Private Sub tmrRefreshData_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles tmrRefreshData.Tick
Dim objLocalDB As New LocalDB
Dim ds As New DataSet
Dim blnUsersRetrieved As Boolean

lblWait.Text = "WAIT"
Try
CheckConnection()
Catch ex As Exception
'MsgBox("Unable to verify connection")

End Try


If g_blnConnected = True And g_blnLocalDBExists = True Then
Try
ds = objLocalDB.GetUsers
If ds.Tables(0).Rows.Count > 0 Then
blnUsersRetrieved = True
g_dsUsers = ds
End If

If g_blnLocalDBExists And blnUsersRetrieved Then
Try
RefreshLocalDatabase()
Catch ex As SqlServerCe.SqlCeException
MsgBox(ex.Message.ToString)
Finally

End Try
End If
Catch ex As Exception

End Try

End If



lblWait.Text = " "

Dim TimeNow As DateTime = DateTime.Now
Dim MyString As String = TimeNow.ToString("hh:mm")

lblLastSync.Text = "Last Sync : " & MyString.TrimStart("0")
End Sub
 
Typically when the timer fires (tick handler) you disable it and at the end
of the sub you re-enable it...

Regardless, try an Application.DoEvents after lblWait.Text = "WAIT"

Cheers
Daniel
 
Daniel,

You da man!!

After I posted this last night, i thought Harry you need a vacation. You
can't set the label text twice within one proc event!! So this morning I had
it call subs from the same points within the proc and it still wouldn't
work. Added the Application.DoEvents to the seperate procs and it's
working. Is this a CF bug?

Appreciate your insight....haven't used DoEvents since VB3!!

Thanks
Harry
 
It is not a CF bug. I haven't use DoEvents since VB classic (and still never
had to use it in my .NET apps).

The easiest way to think of it is that methods running on the UI thread
(e.g. button1_Click, timer1_Tick etc) must be very short (in terms of
execution time) and anything we change in there takes effect after the
method has run (at the point of End Sub). To that end if you need a process
to run for a long time you use a thread (or BackgroundWorker or some other
wrapper) from the UI method.

If you choose to violate that design and run some long processing from a UI
method (e.g. like you do in the timer tick), then any UI changes (e.g.
lblWait.Text=) will not have an effect until the method exits. For the
changes to have an effect you must notify the windows message pump to
process any pending messages and that is exactly what DoEvents does.

Anyway, glad it's working for you.

Cheers
Daniel
 
Thanks Daniel,

<<To that end if you need a process
to run for a long time you use a thread (or BackgroundWorker or some other
wrapper) from the UI method.

If you choose to violate that design and run some long processing from a UI
method (e.g. like you do in the timer tick), then any UI changes (e.g.
lblWait.Text=) will not have an effect until the method exits. >>

Could you point me to a place where I could get more information about this.
I don't want to violate best practices but plead ignorance and would like to
learn.

So you mean start a thread instead of enableing a timer?

TIA
Harry
 
Since you've got something that works (and I can think of many worst
violations than using DoEvents, which can be unavoidable) I wouldn't worry
about it.

If you want to learn about BackgroundWorker have a look at this example (the
whole point is not to block your UI with long running tasks):
http://www.danielmoth.com/Blog/2004/12/backgroundworker-sample.html

Once you understand it, only you will know if it applies to your scenario.

Cheers
Daniel
 
Hi guys,

I'm not sure if this is relevant to what you're talking about, but I've had
a similar issue previously where I wanted to change the label text, and I
solved it using the following:

lblTest.Text = "Text1"
lblTest.Refresh()

lblTest.Text = "Text2"
lblTest.Refresh()
 
Back
Top