473,325 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 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 2019
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sue Adams | last post by:
I actually have two issues/questions: I have an autonumber field in an access db table that I grab and later use to update a record in another table withing the same db. The code I use to get...
1
by: Primerov | last post by:
Why i cannot update from another form? I have an update function called UpdateData that works otherwise,but not from another form. Why is it so? I am trying to update tables from the form F1.There...
4
by: N. Graves | last post by:
Hello... thank you for your time. I have a form that has a List box of equipotent records and a sub form that will show the data of the equipment select from the list box. Is it possible to...
3
by: Vinay | last post by:
Hello I am trying to update a Progress bar on a form. I am able to update it via using a simple clock timer, but as soon as I perform a long operation G1 (generation of a report) in a separate...
15
by: Darren | last post by:
Help, i want to run an update query from a form.. and was wonderin.. Can the update query run if i want to update a value manually inputted from a form (e.g. !!) to a table...
1
by: Mark Reed | last post by:
Hi All, I'm having a problem with the following code. I've read quite a lot of old posts regarding the issue but none seem to affer a solution. The scenario is. I have a bound form which...
5
by: Stephen Plotnick | last post by:
I'm very new to VB.NET 2003 Here is what I have accomplished: MainSelectForm - Selects an item In a public class I pass a DataViewRow to ItemInformation1 Form ItemInformation2 Form
4
by: Ryan | last post by:
I've got a little bit of code that runs when you enter data in a datasheet view (which is a subform of the form you are in) if rst!DateReceived >= 30 Then Forms!DisposalRecords.Label90.Caption =...
0
by: Access Programming only with macros, no code | last post by:
ERROR MESSAGE: Could not update; currently locked by another session on this machine. BACKGROUND I have the following objects: Table1 - HO (which has about 51,000+ records) Table2 -...
16
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.