How to change label.text during process

Couldn't change label.text during process.

Page 1 of 1

7 Replies - 55036 Views - Last Post: 20 January 2010 - 07:23 AM Rate Topic: -----

#1 masterwaldo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-December 09

How to change label.text during process

Post icon  Posted 20 January 2010 - 01:17 AM

I have a "Start" button and a label. The label.text during form load contains "Ready...". Now what I want, after I click the "Start" button, the button will become grey and the label.text contains "Processing...".

I managed to make the button become grey, but I couldn't change the label.text from "Ready..." to "Processing..." and back to "Ready..." after completion of the task. Here is the code:

Public Class Form1
	Dim i As Integer
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Button1.Enabled = False
		Label1.Text = "Processing..."
		Do Until i = 1000
			TextBox1.Text = TextBox1.Text + i.ToString
			i += 1
		Loop
		Button1.Enabled = True
		'Label1.Text = "Ready..."
	End Sub

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		Label1.Text = "Ready..."
	End Sub
End Class


*In actual, the do until loop contains more than just 2 lines.

What happened when I click the "Start" button were, it became grey, it filled up the texbox.text with number from 0 to 1000 and after it finished processing then the label.text change to "Processing..." where it supposed to appear before the loop start.

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: How to change label.text during process

#2 TEH  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 152
  • Joined: 30-January 08

Re: How to change label.text during process

Posted 20 January 2010 - 03:09 AM

I get it to work when I replaced Label with another Textbox, but I dont have any clue why it not change Labels text.

You can make textbox looking like label with these settings:
Borderstyle :None
Backcolor: Control
Readonly:true

so the code would be:
Public Class Form1
	Dim i As Integer
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		TextBox2.Text = Nothing
		TextBox1.Text = "processing"
		Button1.Enabled = False
		i = 1
		Do Until i = 1000
			TextBox2.Text = TextBox2.Text + i.ToString
			i += 1
		Loop
		Button1.Enabled = True
		TextBox1.Text = "Ready..."


	End Sub

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		TextBox1.Text = "Ready..."
	End Sub
End Class



If you want 1000 to be the last number in your list then use:
Do Until i = 1001


Was This Post Helpful? 1
  • +
  • -

#3 masterwaldo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-December 09

Re: How to change label.text during process

Posted 20 January 2010 - 03:21 AM

Yup, it worked now.

But then when i applied to the actual application, it still the same. Give me a couple hours to study the actual code again and if I still can not resolve it, I'll post the code here.

Btw, why it is not working with label? Actually I want to apply it to ToolStripStatusLabel1.Text property.


Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: How to change label.text during process

Posted 20 January 2010 - 03:26 AM

The form runs on one thread - the UI thread. Thread's can only do one thing at a time. The two main things a form has to do are: (1) process windows messages (2) process your code. It can't do both at once. Processing windows messages involves listening for mouse clicks, keyboard presses, messages from windows that say to the form "draw yourself" etc.

Once the Button1_Click sub has started execution, the form will not process windows messages until the sub has ended. Therefore it won't respond to clicks, or keyboard presses or requests to paint itself until the sub has ended. So, when you change the Textbox's text property in the Sub, you don't see it on screen until the sub has ended.

The quick solution is to place Application.DoEvents calls inside your sub. This tells the form "process windows messages now". But it's quite a costly thing to do - you don't want to process all messages just to get a label to show some updated text.

The best solution is to do your work on another thread, and leave the UI thread to process messages. You then have the slight complication that arises from two threads trying to access the same object - in this case the controls on your form, but it's pretty simple to deal with.

Option Strict On
Option Explicit On

Imports System.Threading

Public Class Form1

	Dim i As Integer

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click		
		Dim t As New Thread(AddressOf WorkThread)
		t.Start()
	End Sub

	Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		Label1.Text = "Ready..."
	End Sub

	Private Delegate Sub SetButton1EnabledDelegate(ByVal enabled As Boolean)
	Private Sub SetButton1Enabled(ByVal enabled As Boolean)
		' If this is called from our other thread, then Button1.InvokeRequired
		' will be true.
		If Button1.InvokeRequired Then
			' And this code makes us call the sub again, but this time from the
			' UI therad, so we will then execute the code in the else block.
			Dim del As New SetButton1EnabledDelegate(AddressOf SetButton1Enabled)
			Me.Invoke(del, New Object() {enabled})
		Else
			Button1.Enabled = enabled
		End If
	End Sub

	Private Delegate Sub SetControlTextDelegate(ByVal aControl As Control, ByVal text As String)
	Private Sub SetControlText(ByVal aControl As Control, ByVal someText As String)
		If TextBox1.InvokeRequired Then
			Dim del As New SetControlTextDelegate(AddressOf SetControlText)
			Me.Invoke(del, New Object() {aControl, someText})
		Else
			aControl.Text = someText
		End If
	End Sub

	' Runs on a separate thread to the UI.
	Private Sub WorkThread()
		' We can't safely access any controls directly from this thread.
		' So we must use our own Subs that will access the controls from
		' the UI thread.
		SetButton1Enabled(False)
		SetControlText(Label1, "Processing...")
		Do Until i = 1000
			SetControlText(TextBox1, i.ToString)
			i += 1
		Loop
		SetButton1Enabled(True)
	End Sub

End Class


This post has been edited by crepitus: 20 January 2010 - 03:31 AM

Was This Post Helpful? 2
  • +
  • -

#5 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: How to change label.text during process

Posted 20 January 2010 - 03:29 AM

Hi,

Sometimes its a refresh problem, the text is set but the app dit not refresh. Using Application.DoEvents() could possibly help here.

Also instead of doing

TextBox1.Text = TextBox1.Text + i.ToString



rather do this
(otherwise the i getts added to the last string and you might end up with "Processing...1234" after 4 loops)

TextBox1.Text = "Processing..." + i.ToString



So Try this code and see if it does work.....

		Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Button1.Enabled = False
		Label1.Text = "Processing..."
		Do Until i = 1000
			TextBox1.Text = "Processing..." + i.ToString
			i += 1
			Application.DoEvents()
		Loop
		Button1.Enabled = True
		Label1.Text = "Ready..."
	End Sub




:^:
Was This Post Helpful? 1
  • +
  • -

#6 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: How to change label.text during process

Posted 20 January 2010 - 03:30 AM

Quote

If you want 1000 to be the last number in your list then use:
Do Until i = 1001



Or...

Do
...
Loop Until i = 1000

Was This Post Helpful? 0
  • +
  • -

#7 masterwaldo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-December 09

Re: How to change label.text during process

Posted 20 January 2010 - 06:00 AM

Thanks guys. Problem is solved.


Edit:
Why I received this message when I'm trying to give thanks?


"Sorry, You are unable to thank this member"

This post has been edited by masterwaldo: 20 January 2010 - 06:18 AM

Was This Post Helpful? 0
  • +
  • -

#8 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: How to change label.text during process

Posted 20 January 2010 - 07:23 AM

Hi,

I got your thank, I think it has to do with how many posts you have...

Don't worry we all had to go through it.. (not sure maybe 10 or 25 post or something like that, i can't remember)

;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1