Link to home
Start Free TrialLog in
Avatar of jimtxas
jimtxas

asked on

VB.NET Update Labels and Progressbars on forms

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
or
frmmain.label1.caption = "NEW LABEL"

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

Tks,
J
Avatar of srcalc
srcalc

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"
        frm1.ShowDialog()
    End Sub
End Module

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
   
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"
Avatar of jimtxas

ASKER

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?
The correct call to make is invalidate(), this forces repainting.
So:
frm1.invalidate()

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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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"

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()

     Thread.Sleep(0)

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()