Update one form from another

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JohnSouth

    #1

    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
    Discover local events, family days out, gigs, and festivals across the UK. WhereCanWeGo is your go-to guide for finding what’s on near you.

    Pangbourne UK

  • bob

    #2
    Re: Update one form from another

    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.MySpecial Event 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

    HTH.
    Bob
    "JohnSouth" <JohnSouth104@g mail.com> wrote in message
    news:1140284923 .935241.194280@ g14g2000cwa.goo glegroups.com.. .[color=blue]
    > 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
    >[/color]


    Comment

    • stevehayter@ntlworld.com

      #3
      Re: Update one form from another

      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(FormT ypeA form)

      string newvalue = "test";

      bForm.MyAFormOb jectProperty.Ch angeLabel(newva lue); // 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:
      [color=blue]
      > 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.MySpecial Event 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" <JohnSouth104@g mail.com> wrote in message
      > news:1140284923 .935241.194280@ g14g2000cwa.goo glegroups.com.. .[color=green]
      > > 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
      > >[/color][/color]

      Comment

      • Stephany Young

        #4
        Re: Update one form from another

        What's 'hacky' about that and why is it 'poor programming practice'?


        <stevehayter@nt lworld.com> wrote in message
        news:1140292615 .095592.20160@g 14g2000cwa.goog legroups.com...[color=blue]
        > 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(FormT ypeA form)
        >
        > string newvalue = "test";
        >
        > bForm.MyAFormOb jectProperty.Ch angeLabel(newva lue); // 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:
        >[color=green]
        >> 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.MySpecial Event 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" <JohnSouth104@g mail.com> wrote in message
        >> news:1140284923 .935241.194280@ g14g2000cwa.goo glegroups.com.. .[color=darkred]
        >> > 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
        >> >[/color][/color]
        >[/color]


        Comment

        • Peter Bromberg [C# MVP]

          #5
          Re: Update one form from another

          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:

          UnBlog:





          "Stephany Young" wrote:
          [color=blue]
          > What's 'hacky' about that and why is it 'poor programming practice'?
          >
          >
          > <stevehayter@nt lworld.com> wrote in message
          > news:1140292615 .095592.20160@g 14g2000cwa.goog legroups.com...[color=green]
          > > 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(FormT ypeA form)
          > >
          > > string newvalue = "test";
          > >
          > > bForm.MyAFormOb jectProperty.Ch angeLabel(newva lue); // 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:
          > >[color=darkred]
          > >> 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.MySpecial Event 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" <JohnSouth104@g mail.com> wrote in message
          > >> news:1140284923 .935241.194280@ g14g2000cwa.goo glegroups.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
          > >> >[/color]
          > >[/color]
          >
          >
          >[/color]

          Comment

          Working...