VB.NET Update Labels and Progressbars on forms

Posted on 2004-11-19
Medium Priority
Last Modified: 2008-03-10
I am a veteran vb6 programmer finally upgrading...

In the old days if I was in a module and needed to update a status label or progress bar, i just called it via:

frmmain.progressbar1.value = newvalue
frmmain.label1.caption = "NEW LABEL"

How do I do it this easily in the new world?

Question by:jimtxas
  • 3
  • 2
  • 2
  • +1

Expert Comment

ID: 12631065
It is still that easy. All form controls are declared as Friend, so your module should be in the same assembly as your form so that the module has access. Also, you will need to have an object reference to the instance of the form you wish to change. This is some example code:

Module Module1
    Dim frm1 As New Form1
    Public Sub main()
        frm1.Button1.Text = "This text was changed with a module"
    End Sub
End Module


Expert Comment

ID: 12631115
To finish the above sample code,
* assuming Form1 has a progress bar called pb
* assuming StatusBar is called sb
* assuming StatusBar (sb) contains two panels called p1 and p2

Public Sub Whatever()
    frm1.pb.Incriment() 'Increments the progress bar by 1
    frm1.pb.Incriment(12) 'Incriment the progress bar by 12
    frm1.pb.Value = 50 'Set the progress to 50, wherever that happens to be between pb.minimum and pb.maximum
    p1.text = "New Text"
    p2.text = "More new text"
End Sub

Expert Comment

ID: 12631125
OK, misread that, 2 corrections:
Two lines:
    p1.text = "New Text"
    p2.text = "More new text"
Should Read:
    frm1.p1.text = "New Text"
    frm1.p2.text = "More new text"

For a label:
* assuming Form1 contains lable called lbl1
   frm1.lbl1.text = "New Text"
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 12631531
great!  i was missing the declaration of the object reference.  I figured it would be something simple....

I've increased to 500, can you now tell me how to do the old fashioned form.refresh, label.refresh, etc and get the form to respond to these refreshes... seems like I saw something different somewhere than just using the refresh call...  What's the DoEvents call?

Expert Comment

ID: 12631560
The correct call to make is invalidate(), this forces repainting.

Normally, you shouldn't need to do this in .NET...  What R U trying to do?  Be back tommorrow! G-night!

Accepted Solution

srcalc earned 2000 total points
ID: 12631881
I assume you mean Application.DoEvents... The official explaination is this:

When you run a Windows Form, it creates the new form, which then waits for events to handle. Each time the form handles an event, it processes all the code associated with that event. All other events wait in the queue. While your code handles the event, your application does not respond. For example, the window does not repaint if another window is dragged on top.

If you call DoEvents in your code, your application can handle the other events. For example, if you have a form that adds data to a ListBox and add DoEvents to your code, your form repaints when another window is dragged over it. If you remove DoEvents from your code, your form will not repaint until the click event handler of the button is finished executing.

The most common use of Application.DoEvents is when you are in a subroutine that takes some time to complete, and you want to make sure that your program does not stay unresponsive (can't move forms, click buttons etc) while the code completes.

As for your .refresh() question, I agree with Lacutah, what is your goal with that?

Expert Comment

ID: 12670317
What I do, is I declare some public variables in a module as like:

Public Main as Form

and assign them to the forms as they are loaded. Then you just call:

Main.TextBox.text = "Clicky me here"


Expert Comment

ID: 12670368
Also, the DoEvents has been changed. It is now practically the same as the System.Threading.Thread.Sleep(0) command. Seems like a lot, but if you import System.Threading, you just have to call Thread.Sleep(0):

Imports System.Threading

Public Sub DoEvents()


End Sub

This is better suited for new, .Net applications. It is also compatible for Multi-threading.

Yes, the correct way to repaint a control is Object.Invalidate()

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

850 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