ABaruh
asked on
Changing Label Text at Runtime
I am going nuts trying to figure this out.
All I want to do is when I click a button on my form (which executes a SQL query), change a label text on my form to read "Please wait while the query executes..."
I have:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Button1.Text = "Please wait while the query executes..."
(Do SQL Stuff)
End Sub
The label is not changing. I have tried to remove the default label text (which I would then set at Form Load) and this doesn't help.
Why do basic things need to be so hard sometimes???
All I want to do is when I click a button on my form (which executes a SQL query), change a label text on my form to read "Please wait while the query executes..."
I have:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Button1.Text = "Please wait while the query executes..."
(Do SQL Stuff)
End Sub
The label is not changing. I have tried to remove the default label text (which I would then set at Form Load) and this doesn't help.
Why do basic things need to be so hard sometimes???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
No....nothing happens
If I take out my SQL query it works fine. Why is it not doing anything while processing the SQL query? I am changing the label before the SQL
If I take out my SQL query it works fine. Why is it not doing anything while processing the SQL query? I am changing the label before the SQL
Well, you are changing the Buttons text, not a Labels text. Have your tried using the Label name in your code instead of the Button name?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Label1.Text = "Please wait while the query executes..."
(Do SQL Stuff)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Label1.Text = "Please wait while the query executes..."
(Do SQL Stuff)
End Sub
ASKER
Idle_Mind, good catch, I just typed that wrong in the example though
You can force a refresh before jumping into the SQL code like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "Please wait while the query executes..."
Label1.Refresh()
Application.DoEvents()
' (Do SQL Stuff)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "Please wait while the query executes..."
Label1.Refresh()
Application.DoEvents()
' (Do SQL Stuff)
End Sub
ASKER
Idle_Mind, I had already accepted Uncle's comment about the Refresh, thanks to everyone though.
Thanks Aburah.
Idle_Mind: Is the DoEvents necessary? I just made a little test program for this program without the DoEvents and it worked, but now I am all curious as to what situations need a DoEvents.
Idle_Mind: Is the DoEvents necessary? I just made a little test program for this program without the DoEvents and it worked, but now I am all curious as to what situations need a DoEvents.
It really depends on what else is going on in the application.
If you have other threads already running in the application then the Refesh() may get stuck in the message queue until those threads yield the processor or finish.
The DoEvents call forces the app to process any messages in it's queue. Adding it in ensures the the Refesh() message will be processed NOW. =)
If you don't have anything else going on in the app then it probably doesn't make a difference.
~IM
If you have other threads already running in the application then the Refesh() may get stuck in the message queue until those threads yield the processor or finish.
The DoEvents call forces the app to process any messages in it's queue. Adding it in ensures the the Refesh() message will be processed NOW. =)
If you don't have anything else going on in the app then it probably doesn't make a difference.
~IM
Ahhh...ok, that makes sense. Thanks.
Me.Button1.Text = "Please wait while the query executes..."
After I click the button the Button text changes. Do you get any error?