textbox text not updated
I have a several subs where a textbox writes whats happening. The sub could look something this
private sub DO_Something
txtstatus.text="Now doing this"
call Another_sub
me.close
End sub
But the problem is that the textbox doesnt write the text im telling it to. I've tried putting the my.application.doevent() in the first line but this doesnt help either.
But if i in example put a messagebox right after the textbox.text="ijhpih" then it displays the messagebox and the the text is written in the textbox.
How can i get it to write the text in the textbox?
The glory of Visual Basic is that it can make incredibly difficult things ridiculously easy.
The bane of Visual Basic is that it can make incredibly easy things ridiculously difficult.
Sometimes the difficulty isn't in the procedure itself, its in finding the answer in the first place. It can be a huge frustration. Your problem falls in this category. This is the exact same problem I struggled with for over a week before I figured it out. You were on the right track. You simply had the proper command in the wrong place.
For those who are starting out with VB, here is a review of some important concepts. Feel free to skip ahead of you wish.
Most programing languages of the past were list oriented. Your project was like a list of chores which had to be done one after the other; sort of like directions on how to make a cake. First you get a bowl, then you add two cups flour, then two eggs, and so on down the list till the cake was complete. This approach lead to programs that worked, but for the most part were complex tangles of code which were hard to modify and harder still to use for other applications. Not so for visual Basic.
Visual Basic is object oriented. Now your project is like a collection of objects and your over all program describes how those objects are linked together. Each object is a separate thing, with its own separate program that describes what happens when that object is activated. You over all project is not a list of chores, it is a description of how those objects are connected to each other. Nothing happens until an object is activated, which is called an 'event'. Many events are triggered automatically so you may not even be aware that 'events' are happening. All you know is that when you click on button, code is executed.
The problem at hand is that we want to update a text box, but setting the value of a listbox (or other variable) does not in itself trigger an event. You need to trigger an event so that your changes are applied and your text box will be updated on the screen.
There are several ways to do this. You can set up a timer which will trigger events at regular intervals. You can force your user to press keys or click mice. You can define an entire new class of event handler and summon it. Or you can use the 'DoEvents' command.
When the 'DoEvents' command is executed, all changes waiting to be implemented are taken care of. (Please note, VB6.0 used the 'DoEvents' statement by itself, VB.Net requires that you use 'Application.DoEvents()'. It was this change, poorly documented, which cause me no end of frustration.)
Try the following:
Create a form with a single textbox and a single button on it. Don't bother with the names or settings, we will use the default settings.
Double click on the button and enter the following example code under
‘Private Sub Button1_Click(ByVal sender As System.Obect.... ‘ which appears:
=================
Dim x,y as long
for x = 1 to 10
TextBox1.Text = str$(x)
Application.DoEvents()
for y = 1 to 100000000:next y 'Crude delay loop.
Next x
End Sub
=============================
Hope this helps!
-Science_1
The glory of Visual Basic is that it can make incredibly difficult things ridiculously easy.
The bane of Visual Basic is that it can make incredibly easy things ridiculously difficult.
Sometimes the difficulty isn't in the procedure itself, its in finding the answer in the first place. It can be a huge frustration. Your problem falls in this category. This is the exact same problem I struggled with for over a week before I figured it out. You were on the right track. You simply had the proper command in the wrong place.
For those who are starting out with VB, here is a review of some important concepts. Feel free to skip ahead of you wish.
Most programing languages of the past were list oriented. Your project was like a list of chores which had to be done one after the other; sort of like directions on how to make a cake. First you get a bowl, then you add two cups flour, then two eggs, and so on down the list till the cake was complete. This approach lead to programs that worked, but for the most part were complex tangles of code which were hard to modify and harder still to use for other applications. Not so for visual Basic.
Visual Basic is object oriented. Now your project is like a collection of objects and your over all program describes how those objects are linked together. Each object is a separate thing, with its own separate program that describes what happens when that object is activated. You over all project is not a list of chores, it is a description of how those objects are connected to each other. Nothing happens until an object is activated, which is called an 'event'. Many events are triggered automatically so you may not even be aware that 'events' are happening. All you know is that when you click on button, code is executed.
The problem at hand is that we want to update a text box, but setting the value of a listbox (or other variable) does not in itself trigger an event. You need to trigger an event so that your changes are applied and your text box will be updated on the screen.
There are several ways to do this. You can set up a timer which will trigger events at regular intervals. You can force your user to press keys or click mice. You can define an entire new class of event handler and summon it. Or you can use the 'DoEvents' command.
When the 'DoEvents' command is executed, all changes waiting to be implemented are taken care of. (Please note, VB6.0 used the 'DoEvents' statement by itself, VB.Net requires that you use 'Application.DoEvents()'. It was this change, poorly documented, which cause me no end of frustration.)
Try the following:
Create a form with a single textbox and a single button on it. Don't bother with the names or settings, we will use the default settings.
Double click on the button and enter the following example code under
‘Private Sub Button1_Click(ByVal sender As System.Obect.... ‘ which appears:
=================
Dim x,y as long
for x = 1 to 10
TextBox1.Text = str$(x)
Application.DoEvents()
for y = 1 to 100000000:next y 'Crude delay loop.
Next x
End Sub
=============================
Hope this helps!
-Science_1
Well this is frustrating. The board chokes on my full post.
Lets try a stripped down version.
Try This:
Private Sub Button1_ButtonClick(ByVal sender...
Dim x,y as long
for x = 1 to 10
TextBox1.Text = str$(x)
Application.DoEvents()
for y = 1 to 100000000:next y 'Crude delay loop.
Next x
End Sub
Hope this helps!
-Science_1
I had the same problem with VB6. Try to insert txtstatus.refresh after the txtstatus.text="....". In VB6 that will bring the text to the textbox (and also other boxes like fillist).