Change label text on another form

  • Thread starter Thread starter Roger Ries via DotNetMonster.com
  • Start date Start date
R

Roger Ries via DotNetMonster.com

I'm trying to change the text in a label from another form.

lblInfo.Text = "ABC"

Works for the form your currently in but how the heck do you change that
label information from another form.

Later
 
Use a property or pass the form with the label you want to change to the
form where you want to change it from

for example call the form used to change the label from the form with the
label:
'First give the second form a new constructor + a var to hold the form
that's passed:

Private Caller As Form1
Public Sub New(ByVal myCaller As Form)
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
Caller = DirectCast(myCaller, Form1)
End Sub

'Then in the form with label pass the form to the second forms' constructor
dim obj as new frmUsedToChange(me)
obj.show
'in the second form to change the label
Caller.Label1.Text = "I'm freaking changed ;-)"

hth Peter
 
I was able to use it and it works.

But what if I wasn't opening frmUsedToChange from Form1?

Say I had opened frmUsedToChange from something else but still needed to
change a label or text box on Form1.

Thanks
 
Roger Ries via DotNetMonster.com said:
But what if I wasn't opening frmUsedToChange from Form1?

Say I had opened frmUsedToChange from something else but still needed to
change a label or text box on Form1.

What you need is a reference to your instance of the form you want to
manipulate. There are different ways to make this reference available, for
example, by passing it in method parameters or assigning it to properties,
or implementing the Singleton design pattern if there can be only one
instance of a form:

Providing a reference to an application's main form
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=accessmainform&lang=en>
 
Back
Top