Label not showing on form

  • Thread starter Thread starter Martin Eyles
  • Start date Start date
M

Martin Eyles

I am making a simple program that displays a warning message for 10 seconds
when a user attempts to run an unauthorised program. (I may also log to a
file, but I want the simple warning message working first).

I have made a windows form Form1 which contains a label Label1. In my code I
make an instance of Form1 called theForm, and use its Show method and
CenterToScreen method. I then wait 10 seconds, before the main method ends.
In the Form1 Load handler, I perform the show method of the Label.

In practice, the form is indeed shown and centered on screen. However, the
label is not drawn on screen, leaving a transparent area where whichever
program is running behind shows through.

The code for the methods I have written is:


Public Shared Sub main()
Dim theForm As New Form1
theForm.Show()
theForm.CenterToScreen()
Threading.Thread.Sleep(10 * 1000)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Label1.Show()
End Sub


Does anyone have any idea how to fix this?

Thanks,
Martin
 
I found a solution that appears to work

I just add Application.DoEvents() before the time delay. Still, seems weird
that I should have to, but never mind.

If anyone has any improvements on the idea then that's great, but don't
worry too much about it now.

Thanks,
Martin
 
Your call to Sleep() is blocking the UI thread and thus blocking any
message from the Windows message loop from being processed. So the form
won't be repainted until the Sleep exits. Calling DoEvents allows
Windows messages to be processed. You could use the
System.Windows.Forms.Timer class instead. Set the Interval to 1000 *
10, then handle to Tick event to hide the label, remove event handler
for Tick and destroy the timer instance.
 
Back
Top