Accessing controls on another form

G

Guest

Background: I have a windows form called 'frmMain'. On this form, I have a
LABEL control called 'lblWorkStatus', and it's property is set to PUBLIC.

On another form, I have a process that checks on the status of a project
that our company is working on. Programmatically, if the status changes, I
want to relay that information to the 'lblWorkStatus' control on the main
form.

My code looks like this:

frmMain fmain = new frmMain();
fmain.lblWorkStatus.Text = "Status: Project Postponed";

The control never gets updated. Am I doing something wrong?


Thanks,
 
P

Peter Duniho

vbtrying said:
[...]
My code looks like this:

frmMain fmain = new frmMain();
fmain.lblWorkStatus.Text = "Status: Project Postponed";

The control never gets updated. Am I doing something wrong?

Hard to say for sure. But based on the code you posted, it seems you
are creating a new instance of frmMain, setting that instance's
control's Text property, and then doing nothing else.

You need to at a minimum show the instance of frmMain you created. If
you meant to set the Text property on the control in an already-existing
instance of frmMain, you need to get that instance instead, rather than
creating a new one.

Pete
 
G

Guest

It is an already existing instance. How to I determine the instance? I see
that by using the NEW keyword, it is making a new instance.

If I try using the "frmMain.lblWorkStatus.Text" syntax without creating a
new instance, I dont see the lblWorkStatus control using the . sequence

I'm lost of this one ... thanks for the help !



Peter Duniho said:
vbtrying said:
[...]
My code looks like this:

frmMain fmain = new frmMain();
fmain.lblWorkStatus.Text = "Status: Project Postponed";

The control never gets updated. Am I doing something wrong?

Hard to say for sure. But based on the code you posted, it seems you
are creating a new instance of frmMain, setting that instance's
control's Text property, and then doing nothing else.

You need to at a minimum show the instance of frmMain you created. If
you meant to set the Text property on the control in an already-existing
instance of frmMain, you need to get that instance instead, rather than
creating a new one.

Pete
 
A

achilles2conquer

It is an already existing instance. How to I determine the instance? I see
that by using the NEW keyword, it is making a new instance.

If I try using the "frmMain.lblWorkStatus.Text" syntax without creating a
new instance, I dont see the lblWorkStatus control using the . sequence

I'm lost of this one ... thanks for the help !

In child form, define this
public frmMain myParentForm;

While creating the child form, set the parent form.

//Creating child
FrmChild frmChild = new FrmChild();
//Set Parent
frmChild.MyParentForm = this;

Now you can access the mainfrm using
frmChild.myParentFrom.lblWorkStatus.Text

But you wont be able to code it this way, as parent is created in a
different thread. You need to have a method on parent form for
updating the status, and invoke that method from your child form.

Create a method in parent:
public void UpdateStatus(string message)
{
this.lblWorkStatus.Text = message;
}

define a delegate in child form:

private delegate void delegateUpdate(string message);

and for updating the status:

delegateUpdate RelayMessage=new
delegateUpdate(this.myParentFrom.UpdateStatus)
invoke(RelayMessage,new object[] {message});
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top