Question:
VB .NET - How to begin program AFTER form load?
akv06
2010-06-30 21:09:52 UTC
Hi all,

I have a Form (Form1) and a Label (Label1).
Default: Label1.Text = "aaa"
My main objective is to change Label1.Text = "bbb" 5 sec after form load.

This is my code so far:

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

System.Threading.Thread.Sleep(5000)
Label1.Text = "bbb"

End Sub

However, what happen is: the form ONLY appear after 5 sec with Label1.Text = "bbb".
I don't want this.
What I want is: right after I run the project, the form will APPEAR with Label1.Text = "aaa", then after 5 sec, Label1.Text = "bbb".

Is there any function like: Private Sub Form1_AfterLoad(ByVal...) or something?
I don't find any in the function list.

*I don't want to use Timer! Unless that is the very last thing I have to use.

Please help~~~

.
Three answers:
Ratchetr
2010-06-30 21:27:38 UTC
You almost never want to sleep in Form code.



Here's the thing: When you create a form, or store values in a control, they don't get painted on the screen the instant you do that. All that happens is that the form or control gets marked as 'I need to repaint myself'. The repaint gets handled for you behind the scenes. Once there is nothing else to do, and something needs to be painted, then it gets painted.

But when you throw a Sleep in there, it mucks up the works. You don't hit the 'there's nothing else to do, So I'll paint now' code. Because you are busy doing something else...Sleeping.



Add a timer to your form. It's in the toolbox. Set the interval to 5000. Set it to enabled. Set an event handler for the Tick event.

In the Tick event handler, do Label1.Text = "bbb";



Sorry, but timer is the very last thing you have to do. There really isn't any other way.
hafner
2016-10-30 05:27:53 UTC
Vb.net Form Load
Helpful Harry
2010-07-02 17:32:18 UTC
The timer is best thing to do in this instance. The problem when the program sleeps is it freezes. But this CAN work.



What you want to do (anytime the program might freeze *example encrypting a file) is put your function in a thread. Then you can use delegates to change the text.



Dane


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...