שתף באמצעות


Change text in label.text with a click of a button.

Question

Wednesday, January 20, 2010 10:01 AM

Hello,

I have this problem but I already simplify the code as below:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      
        Label1.Text = "hello"
        System.Threading.Thread.Sleep(5000)
        'MessageBox.Show("hahahaha")
        Label1.Text = "world"

    End Sub

What I'm trying to achieve here is, after I click the button, the label1.text should change to hello, and after that to world. But I couldn't achieve that. Instead when I click the button, it just paused for 5 second and displayed world.

System.Threading.Thread.Sleep(5000)

The code is just a dummy for a loop that I have.

Hope to get response this time, since I my past question is never been answered.

Thank you.

All replies (6)

Wednesday, January 20, 2010 10:15 AM ✅Answered | 2 votes

The button click event stops all form refreshes until the event has finished. What your seeing here is normal windows forms behaviour.

To get it going to need to tell the form to update before windows gets around to doing this itself.

if you put  me.refresh() after the first label setting and then run it, it should work.. Like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      
        Label1.Text = "hello"
        Me.Refresh()
        System.Threading.Thread.Sleep(5000)
        'MessageBox.Show("hahahaha")
        Label1.Text = "world"

    End Sub

This Should get what you want going as this will tell windows to refresh the form before it's natural cycle of windows events and therefore you should see the changes.

Hope it helps.

Tom


Wednesday, January 20, 2010 10:17 AM ✅Answered | 1 vote

 Try out with this:

Private
 Sub
 Button1_Click(ByVal
 sender As
 System.Object, ByVal
 e As
 System.EventArgs) Handles
 Button1.Click

      

            Label1.Text = "Hello"



        Application.DoEvents()



        Threading.Thread.CurrentThread.Sleep(5000)



        Label1.Text = "World"

    End
 Sub

Si la respuesta te ha sido util Marcala como Respuesta o Votala.
Mi Blog: Jtorrecilla


Wednesday, January 20, 2010 10:20 AM ✅Answered | 1 vote

you need to refresh the label

    Label1.Text = "hello"
    Me.Refresh()
    System.Threading.Thread.Sleep(5000)
    'MessageBox.Show("hahahaha")
    Label1.Text = "world"

please, mark this as answer if it is THE answer

Diego Cattaruzza
Microsoft MVP - Visual Basic: Development
blog: http://community.visual-basic.it/Diego
web site: http://www.visual-basic.it


Wednesday, January 20, 2010 10:13 AM | 1 vote

Put a timer control on the form.  by default it will be called Timer1.

Use the following code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Interval = 5000
        Label1.Text = "hello"
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Text = "world"
        Timer1.Stop()
    End Sub

Wednesday, January 20, 2010 12:37 PM

Thanks guys for the answer.

I chose Me.Refresh() method as it's not complicated and I understand it. The timer method at this point of time is still beyond my knowledge.


Wednesday, January 20, 2010 8:13 PM

The Thread.Sleep option will make your applcation completely unresposive during those 5 seconds, even to the extent of not allowing the user to resize or minimize the window.  In fact, it makes it look as if the application has crashed.  That might be the effect you wanted to achieve, but if it's not then you should avoid Thread.Sleep.