471,306 Members | 1,334 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

Update one form from another

Hi

In a c# Windows Forms application (not asp.net) if I've opened 2 or
more non-modal forms using code like:

FormTypeA aForm = new FormTypeA();
aForm .Show();

FormTypeB bForm = new FormTypeB();
bForm .Show();
How do I update a control (e.g. label) in aForm from code in bForm?
And will the update be seen immediately, or on activation?

John South
www.wherecanwego.com
Pangbourne UK

Feb 18 '06 #1
4 1953
bob
Hi John,
Suggest you use events to communicate between forms.

If formA instantiates FormB then
Declare a custom event (say MySpecialEvent) in FormB with the payload that
you want to deliver.

Declare your FormB variable at FormA Class Level and write a
FormB.MySpecialEvent Eventhandler.

The update will occur when events are handled. Depending on where you are in
your code you may have to issue a DoEvents to get a timely update.
If both FormA and FormB are instantiated in FormC then you can still use the
same technique by FormC responding to FormA and then raising an Interrrupt
which is responded to by FormB
Have a look at
http://www.codeproject.com/csharp/eventarguments.asp
HTH.
Bob
"JohnSouth" <Jo**********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi

In a c# Windows Forms application (not asp.net) if I've opened 2 or
more non-modal forms using code like:

FormTypeA aForm = new FormTypeA();
aForm .Show();

FormTypeB bForm = new FormTypeB();
bForm .Show();
How do I update a control (e.g. label) in aForm from code in bForm?
And will the update be seen immediately, or on activation?

John South
www.wherecanwego.com
Pangbourne UK

Feb 18 '06 #2
I'll second bob - events are the most elegant way here.

I shouldn't be posting this, but there is a 'hacky' way to do it
(particularly if you're not familiar with events in .NET). Remember,
Forms are objects - so just add a get accessor, (or a method if you
need to do a whole lotta stuff) to the FormTypeA class, and construct
your bForm passing aForm as a parameter. You can update it before it is
shown, or whenever you like then.

FormTypeA aForm = new FormTypeA();
aForm .Show();

FormTypeB bForm = new FormTypeB(aForm);
// Constructor: public FormTypeB(FormTypeA form)

string newvalue = "test";

bForm.MyAFormObjectProperty.ChangeLabel(newvalue); // Updates the value
bForm .Show();

However, this is poor programming practice and I strongly suggest
events! Once you understand them, they're unbelieveably cool and make
programming far more flexible :)

bob wrote:
Hi John,
Suggest you use events to communicate between forms.

If formA instantiates FormB then
Declare a custom event (say MySpecialEvent) in FormB with the payload that
you want to deliver.

Declare your FormB variable at FormA Class Level and write a
FormB.MySpecialEvent Eventhandler.

The update will occur when events are handled. Depending on where you are in
your code you may have to issue a DoEvents to get a timely update.
If both FormA and FormB are instantiated in FormC then you can still use the
same technique by FormC responding to FormA and then raising an Interrrupt
which is responded to by FormB
Have a look at
http://www.codeproject.com/csharp/eventarguments.asp
HTH.
Bob
"JohnSouth" <Jo**********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hi

In a c# Windows Forms application (not asp.net) if I've opened 2 or
more non-modal forms using code like:

FormTypeA aForm = new FormTypeA();
aForm .Show();

FormTypeB bForm = new FormTypeB();
bForm .Show();
How do I update a control (e.g. label) in aForm from code in bForm?
And will the update be seen immediately, or on activation?

John South
www.wherecanwego.com
Pangbourne UK


Feb 18 '06 #3
What's 'hacky' about that and why is it 'poor programming practice'?
<st*********@ntlworld.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I'll second bob - events are the most elegant way here.

I shouldn't be posting this, but there is a 'hacky' way to do it
(particularly if you're not familiar with events in .NET). Remember,
Forms are objects - so just add a get accessor, (or a method if you
need to do a whole lotta stuff) to the FormTypeA class, and construct
your bForm passing aForm as a parameter. You can update it before it is
shown, or whenever you like then.

FormTypeA aForm = new FormTypeA();
aForm .Show();

FormTypeB bForm = new FormTypeB(aForm);
// Constructor: public FormTypeB(FormTypeA form)

string newvalue = "test";

bForm.MyAFormObjectProperty.ChangeLabel(newvalue); // Updates the value
bForm .Show();

However, this is poor programming practice and I strongly suggest
events! Once you understand them, they're unbelieveably cool and make
programming far more flexible :)

bob wrote:
Hi John,
Suggest you use events to communicate between forms.

If formA instantiates FormB then
Declare a custom event (say MySpecialEvent) in FormB with the payload
that
you want to deliver.

Declare your FormB variable at FormA Class Level and write a
FormB.MySpecialEvent Eventhandler.

The update will occur when events are handled. Depending on where you are
in
your code you may have to issue a DoEvents to get a timely update.
If both FormA and FormB are instantiated in FormC then you can still use
the
same technique by FormC responding to FormA and then raising an
Interrrupt
which is responded to by FormB
Have a look at
http://www.codeproject.com/csharp/eventarguments.asp
HTH.
Bob
"JohnSouth" <Jo**********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
> Hi
>
> In a c# Windows Forms application (not asp.net) if I've opened 2 or
> more non-modal forms using code like:
>
> FormTypeA aForm = new FormTypeA();
> aForm .Show();
>
> FormTypeB bForm = new FormTypeB();
> bForm .Show();
>
>
> How do I update a control (e.g. label) in aForm from code in bForm?
> And will the update be seen immediately, or on activation?
>
> John South
> www.wherecanwego.com
> Pangbourne UK
>

Feb 18 '06 #4
Stephany,
Beats me! Certainly events are more flexible, but if it "ain't broke", then
don't fix it!
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Stephany Young" wrote:
What's 'hacky' about that and why is it 'poor programming practice'?
<st*********@ntlworld.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I'll second bob - events are the most elegant way here.

I shouldn't be posting this, but there is a 'hacky' way to do it
(particularly if you're not familiar with events in .NET). Remember,
Forms are objects - so just add a get accessor, (or a method if you
need to do a whole lotta stuff) to the FormTypeA class, and construct
your bForm passing aForm as a parameter. You can update it before it is
shown, or whenever you like then.

FormTypeA aForm = new FormTypeA();
aForm .Show();

FormTypeB bForm = new FormTypeB(aForm);
// Constructor: public FormTypeB(FormTypeA form)

string newvalue = "test";

bForm.MyAFormObjectProperty.ChangeLabel(newvalue); // Updates the value
bForm .Show();

However, this is poor programming practice and I strongly suggest
events! Once you understand them, they're unbelieveably cool and make
programming far more flexible :)

bob wrote:
Hi John,
Suggest you use events to communicate between forms.

If formA instantiates FormB then
Declare a custom event (say MySpecialEvent) in FormB with the payload
that
you want to deliver.

Declare your FormB variable at FormA Class Level and write a
FormB.MySpecialEvent Eventhandler.

The update will occur when events are handled. Depending on where you are
in
your code you may have to issue a DoEvents to get a timely update.
If both FormA and FormB are instantiated in FormC then you can still use
the
same technique by FormC responding to FormA and then raising an
Interrrupt
which is responded to by FormB
Have a look at
http://www.codeproject.com/csharp/eventarguments.asp
HTH.
Bob
"JohnSouth" <Jo**********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
> Hi
>
> In a c# Windows Forms application (not asp.net) if I've opened 2 or
> more non-modal forms using code like:
>
> FormTypeA aForm = new FormTypeA();
> aForm .Show();
>
> FormTypeB bForm = new FormTypeB();
> bForm .Show();
>
>
> How do I update a control (e.g. label) in aForm from code in bForm?
> And will the update be seen immediately, or on activation?
>
> John South
> www.wherecanwego.com
> Pangbourne UK
>


Feb 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Sue Adams | last post: by
1 post views Thread by Primerov | last post: by
15 posts views Thread by Darren | last post: by
5 posts views Thread by Stephen Plotnick | last post: by
reply views Thread by Access Programming only with macros, no code | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.