Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,373 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,646 people online right now. Registration is fast and FREE... Join Now!




Textbox.Text not seen in loop

 
Reply to this topicStart new topic

Textbox.Text not seen in loop

k6elc
21 Jan, 2008 - 07:53 PM
Post #1

New D.I.C Head
*

Joined: 21 Jan, 2008
Posts: 2

When I run the following code, the TextBox1 is passed over in the loop until the final iteration when it displays the final value of i. The beep occurs three times, but the TextBox1 is not updated for i=1 and i=2. I would like an explanation of why this is.

Thanks in advance!
CODE

Public Class Form1

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

        For i = 1 To 3
            TextBox1.Text = i
            Beep()
            For j = 1 To 1000000000
            Next j
        Next i

    End Sub

End Class

User is offlineProfile CardPM
+Quote Post

Nayana
RE: Textbox.Text Not Seen In Loop
22 Jan, 2008 - 06:38 PM
Post #2

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
OK, what's happening is that code above is running in the same thread as the user interface. What that means is that the update events for the user interface will not be executed until your above code has finished.

This means that only the last state will be shown.

In VB you can allow the UI to be updated by using the DoEvents command.

However, having a big loop like
CODE
For j = 1 To 1000000000
Next j

Is pretty useless. If you want to wait for a certain amount of time, you should use a timer.

This post has been edited by Nayana: 22 Jan, 2008 - 06:51 PM
User is offlineProfile CardPM
+Quote Post

k6elc
RE: Textbox.Text Not Seen In Loop
23 Jan, 2008 - 03:33 PM
Post #3

New D.I.C Head
*

Joined: 21 Jan, 2008
Posts: 2

Thanks Nayana.... That works fine and another possibility, as I now realize, is multithreading. I was fluent in basic 30 years ago, but this GUI is taxing my feeble old mind. The long loop was just for the example and yes the timer is better solution.


QUOTE(Nayana @ 22 Jan, 2008 - 07:38 PM) *

OK, what's happening is that code above is running in the same thread as the user interface. What that means is that the update events for the user interface will not be executed until your above code has finished.

This means that only the last state will be shown.

In VB you can allow the UI to be updated by using the DoEvents command.

However, having a big loop like
CODE
For j = 1 To 1000000000
Next j

Is pretty useless. If you want to wait for a certain amount of time, you should use a timer.



User is offlineProfile CardPM
+Quote Post

Nayana
RE: Textbox.Text Not Seen In Loop
23 Jan, 2008 - 05:21 PM
Post #4

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
No worries smile.gif

I wasn't putting down your example, for your query, it was a perfect example. I just meant that in production code, it is pretty useless.

Multi-threading can be quite complex at first (actually, always), so if you give it a go, and have any queries, don't hesitate to ask here again.

Good luck.
User is offlineProfile CardPM
+Quote Post

LookNAO
RE: Textbox.Text Not Seen In Loop
24 Jan, 2008 - 06:59 AM
Post #5

D.I.C Head
**

Joined: 28 Dec, 2007
Posts: 66



Thanked: 1 times
My Contributions
CODE


TextBox1.Text = i
TextBox1.refresh
doevents



User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Textbox.Text Not Seen In Loop
24 Jan, 2008 - 07:08 AM
Post #6

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,474



Thanked: 160 times
Dream Kudos: 9025
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
I wrote a tutorial on Cross Process Communication, in this tutorial you will learn that you cannot access that textbox from a separate thread, UI controls can only be accessed directly from the thread that created them.

You can, however, as the tutorial shows, use a Delegate to marshal the call onto the thread controlling the UI controls.

PS: This is VB.Net so I am moving this to the proper forum, but VB,Net also has a DoEvents Method as well smile.gif . The tutorial above is written in C#, but for a seasoned programmer you should be able t get the gist of how the logic works smile.gif
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 09:06AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month