Solved

Changing Label Text at Runtime

Posted on 2004-10-12
10
431 Views
Last Modified: 2010-04-23
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???
0
Comment
Question by:ABaruh
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12289852
Works fine on my pc
 Me.Button1.Text = "Please wait while the query executes..."

After I click the button the Button text changes. Do you get any error?
0
 
LVL 2

Accepted Solution

by:
UncleMidriff earned 500 total points
ID: 12289867
I feel your pain.  I've lost count of the times I have pulled my hair out for hours on a simple problem.  

Try

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..."
      Me.Button1.Refresh

      (Do SQL Stuff)

End Sub

Also, I don't think you need to use Me.Button1.BlahWhatever.  Instead, try just Button1.BlahWhatever.

Hope that helps.
0
 
LVL 7

Author Comment

by:ABaruh
ID: 12289873
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
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12289886
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
0
 
LVL 7

Author Comment

by:ABaruh
ID: 12289903
Idle_Mind, good catch, I just typed that wrong in the example though
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12289924
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
0
 
LVL 7

Author Comment

by:ABaruh
ID: 12289966
Idle_Mind, I had already accepted Uncle's comment about the Refresh, thanks to everyone though.
0
 
LVL 2

Expert Comment

by:UncleMidriff
ID: 12290028
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.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12290201
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
0
 
LVL 2

Expert Comment

by:UncleMidriff
ID: 12290604
Ahhh...ok, that makes sense.  Thanks.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question