![]() |
Click here to advertise with us
|
|
|||||||
| VB.NET General Discussion VB.NET general discussion area |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Hi all
Can I please get some help on some basic coding. The user Pushes a button on the form. A 'For loop' begins at i=0 Textbox2 is set to display some information within the loop Then it goes onto the next i. Again Textbox2 displays new information. How do you go about doing this? Many Thanks Hscorpio |
|
|||
|
Not hard at all if you know the answer. I guess thats why I asked.
But you'll have to dumb it down a bit more for me. for i = 0 to x me.textbox2.text=textbox2.text & " " & aa(i) next i gives me the same result as I had before I added the me. windows form bit. It goes through the loop and the user only sees what comes out at the end. |
|
|||
|
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Me.BackgroundWorker1.WorkerReportsProgress = True Me.BackgroundWorker1.RunWorkerAsync() End Sub Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) _ Handles BackgroundWorker1.DoWork For i As Integer = 0 To 10000 BackgroundWorker1.ReportProgress(i / 100, i) Threading.Thread.Sleep(1) Next End Sub Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) _ Handles BackgroundWorker1.ProgressChanged Me.lblUserState.Text = "Number: " & e.UserState.ToString() Me.lblPercent.Text = e.ProgressPercentage.ToString() & "%" End Sub |
|
||||
|
But, it doesnt work for the OP
He wants to count to.. say.. a million.. And he wants to see the numbers rolling up in the text box from 1 to a million (or the text extending each time). He does not want to click the Go button, and have the UI hang for 30 seconds while VB concatenates a million strings together and stores them in a text box and only reveals the result at the end, hence why MattP provided the BackgroundWorker example hscorpio: note that MattP's example replaces the text each time. If you want to append the text each time, use textbox.AppendText("text") in place of textbox.Text = "text" Note also that you DONT have to use e.ProgressPercentage (you can set it to 0 always, or use it to pass status codes) nor does it have to be a progress percentage at all; it can jump up and down and be totally unrelated to your operation's progress. You can pass any information you like in the UserState parameter; MattP passes a number, i, but it can be a string, number, error, anything. At the other side, in the ProgressChanged event handler, convert the userstate back to what you want with DirectCast(e.UserState, <type>). e.g. Suppose you had a custom class ReportState, you would Code:
Dim myReportState as New ReportState ... BackgroundWorker1.ReportProgress(i / 100, myReportState) ... 'and in the progress changed event handler: ... Dim tmpRepState as ReportState = DirectCast(e.UserState, ReportState) 'now do whatever you want with the reportstate object Last edited by cjard; 12-03-2008 at 7:16 AM. |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|