Simple forms question

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

    #1

    Simple forms question

    Hi,

    I have a form (FormA) which was loaded using Application.Run when my app
    loads. This form has a button which loads Form B using "FormB myForm=new
    FormB()". My question is, how do I access and modify the value of a control
    on FormA from FormB?

    I thought this would be easy but I was wrong.

    Cheers,

    John


  • Marcellus

    #2
    Simple forms question

    Expose a public property of type Form, from B. Assign A
    to the public property. Then B has a reference to A, you
    can iterate the controls collection to identify your
    target control and manipulate it as you wish.
    I'm sure there's a more elegant solution, but this will
    work in the interim.



    [color=blue]
    >-----Original Message-----
    >Hi,
    >
    >I have a form (FormA) which was loaded using[/color]
    Application.Run when my app[color=blue]
    >loads. This form has a button which loads Form B[/color]
    using "FormB myForm=new[color=blue]
    >FormB()". My question is, how do I access and modify the[/color]
    value of a control[color=blue]
    >on FormA from FormB?
    >
    >I thought this would be easy but I was wrong.
    >
    >Cheers,
    >
    >John
    >
    >
    >.
    >[/color]

    Comment

    • JSM

      #3
      Re: Simple forms question

      Hi Mikael,

      The example you gave sends information from A to B. What I can't figure out
      is how can I send information from B to A. Since A was created using
      Application.Run I don't have an instance name of A which I could use.

      Cheers,

      John

      "Mikael" <_NOSPAM_@chell o.se> wrote in message
      news:O0iOoSbXDH A.208@tk2msftng p13.phx.gbl...[color=blue]
      > Heres a simple example using a property.
      >
      > Regards, Mikael
      >
      > using System;
      > using System.Windows. Forms;
      >
      > public class A : Form
      > {
      > private Button btn = new Button();
      > B b = new B();
      >
      > public A()
      > {
      > btn.Text = "Load form B";
      > btn.Click += new EventHandler(bt n_Click);
      > this.Controls.A dd(btn);
      > }
      >
      > private void btn_Click(objec t sender, EventArgs e)
      > {
      > b.Show();
      > }
      >
      > protected override void OnMouseDown(Mou seEventArgs e)
      > {
      > b.LabelText = "Hello from A!";
      > }
      > static void Main()
      > {
      > Application.Run (new A());
      > }
      > }
      >
      > public class B : Form
      > {
      > private Label lbl = new Label();
      > public B()
      > {
      > lbl.Dock = DockStyle.Fill;
      > this.Controls.A dd(lbl);
      > }
      >
      > public string LabelText
      > {
      > set { lbl.Text = value; }
      > }
      > }
      >
      > "JSM" <123@123.com> skrev i meddelandet
      > news:uALxrCbXDH A.1872@TK2MSFTN GP12.phx.gbl...[color=green]
      > > Hi,
      > >
      > > I have a form (FormA) which was loaded using Application.Run when my app
      > > loads. This form has a button which loads Form B using "FormB myForm=new
      > > FormB()". My question is, how do I access and modify the value of a[/color]
      > control[color=green]
      > > on FormA from FormB?
      > >
      > > I thought this would be easy but I was wrong.
      > >
      > > Cheers,
      > >
      > > John
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • AlexS

        #4
        Re: Simple forms question

        {
        A a=new A();
        Application.Run (a);
        }

        Does this help?



        "JSM" <123@123.com> wrote in message
        news:%233mPTZbX DHA.2392@TK2MSF TNGP10.phx.gbl. ..[color=blue]
        > Hi Mikael,
        >
        > The example you gave sends information from A to B. What I can't figure[/color]
        out[color=blue]
        > is how can I send information from B to A. Since A was created using
        > Application.Run I don't have an instance name of A which I could use.
        >
        > Cheers,
        >
        > John
        >
        > "Mikael" <_NOSPAM_@chell o.se> wrote in message
        > news:O0iOoSbXDH A.208@tk2msftng p13.phx.gbl...[color=green]
        > > Heres a simple example using a property.
        > >
        > > Regards, Mikael
        > >
        > > using System;
        > > using System.Windows. Forms;
        > >
        > > public class A : Form
        > > {
        > > private Button btn = new Button();
        > > B b = new B();
        > >
        > > public A()
        > > {
        > > btn.Text = "Load form B";
        > > btn.Click += new EventHandler(bt n_Click);
        > > this.Controls.A dd(btn);
        > > }
        > >
        > > private void btn_Click(objec t sender, EventArgs e)
        > > {
        > > b.Show();
        > > }
        > >
        > > protected override void OnMouseDown(Mou seEventArgs e)
        > > {
        > > b.LabelText = "Hello from A!";
        > > }
        > > static void Main()
        > > {
        > > Application.Run (new A());
        > > }
        > > }
        > >
        > > public class B : Form
        > > {
        > > private Label lbl = new Label();
        > > public B()
        > > {
        > > lbl.Dock = DockStyle.Fill;
        > > this.Controls.A dd(lbl);
        > > }
        > >
        > > public string LabelText
        > > {
        > > set { lbl.Text = value; }
        > > }
        > > }
        > >
        > > "JSM" <123@123.com> skrev i meddelandet
        > > news:uALxrCbXDH A.1872@TK2MSFTN GP12.phx.gbl...[color=darkred]
        > > > Hi,
        > > >
        > > > I have a form (FormA) which was loaded using Application.Run when my[/color][/color][/color]
        app[color=blue][color=green][color=darkred]
        > > > loads. This form has a button which loads Form B using "FormB[/color][/color][/color]
        myForm=new[color=blue][color=green][color=darkred]
        > > > FormB()". My question is, how do I access and modify the value of a[/color]
        > > control[color=darkred]
        > > > on FormA from FormB?
        > > >
        > > > I thought this would be easy but I was wrong.
        > > >
        > > > Cheers,
        > > >
        > > > John
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Arran Pearce

          #5
          Re: Simple forms question

          if in FormB's constructor you have a param which receieves a referance of a
          form control.

          then when you call FormB from FormA use the this keyword to pass the
          referance.

          FormB will then be able to access public elements in FormA.


          "JSM" <123@123.com> wrote in message
          news:uALxrCbXDH A.1872@TK2MSFTN GP12.phx.gbl...[color=blue]
          > Hi,
          >
          > I have a form (FormA) which was loaded using Application.Run when my app
          > loads. This form has a button which loads Form B using "FormB myForm=new
          > FormB()". My question is, how do I access and modify the value of a[/color]
          control[color=blue]
          > on FormA from FormB?
          >
          > I thought this would be easy but I was wrong.
          >
          > Cheers,
          >
          > John
          >
          >[/color]


          Comment

          Working...