Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

Accessing controls on another form

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
vbtrying - 31 Aug 2007 02:28 GMT
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,
Peter Duniho - 31 Aug 2007 02:47 GMT
> [...]
> My code looks like this:
[quoted text clipped - 3 lines]
>
> 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
vbtrying - 31 Aug 2007 03:00 GMT
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 !

> > [...]
> > My code looks like this:
[quoted text clipped - 14 lines]
>
> Pete
achilles2conquer@gmail.com - 31 Aug 2007 15:39 GMT
On Aug 30, 10:00 pm, vbtrying <vbtry...@discussions.microsoft.com>
wrote:
> 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.
[quoted text clipped - 3 lines]
>
> 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});

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.