All,

This is driving me crazy. How come only the last thing I send to a textbox is displayed? In the following example I would expect 'abcdefghi' to be displayed then, 1 second later, '123456789' should be displayed. But the only thing I see is 123456789.

What is going on here?

Thanks,
Bill

Imports System.Threading

Public Class Form1

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

        TextBox1.Text = "abcdefghi"

        Thread.Sleep(1000)

        TextBox1.Text = "123456789"

    End Sub
End Class

Because assigning a value to a textbox replaces the old value. You probably want to append the string

TextBox1.Text = TextBox1.Text & "123456789"

Because assigning a value to a textbox replaces the old value.

Thanks for the reply, but I am trying to replace the old value and it doesn't work.

When the user clicks Button1 I want the textbox to display 'abcdefghi" then 1 second later display '123456789'. All it does is display '123456789' I don't understand why or how to fix it.

Thanks,
Bill

TextBox1.Text = "abcdefghi"
        Application.DoEvents() '// do what needs to be done and move on.
        Thread.Sleep(1000)
        TextBox1.Text = "123456789"

DoEvents()? Well, that makes my program work, but why? I'm new to VB.NET, so I am trying to understand what's going on here. Can you explain why the textbox doesn't update until it sees DoEvents()? Does C# work the same way? C++?

Thanks,
Bill

Not sure about any other languages, although I doubt that those languages do not have a similar solution to running all previous code before the Application.DoEvents code line and update your app., then run the remaining code.

Thank you. -Bill

DoEvents()? Well, that makes my program work, but why?

You are telling the program to sleep right after you change the text. Since it's all running on the same thread, the GUI doesn't have enough time before you tell it to sleep. You then change the text before it can do an update, thus you get the 2nd text.

Member Avatar for Unhnd_Exception

You can refresh it before you sleep. Just repaints the textbox will the value before the thread is paused.

TextBox1.Text = "abcdefghi"
TextBox1.Refresh
Thread.Sleep(1000)
TextBox1.Text = "123456789"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.