Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Passing Variables between Forms

Passing Variables between Forms

Scheduled Pinned Locked Moved C#
hostingtutorialquestionworkspace
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    surgeproof
    wrote on last edited by
    #1

    i posted before asking how to accomplish this and was replied to, but what they said didn't seem to work (else i was being more thick than usual! :p). this is what i need to do: in an MDI environment, have a variable in FormParent called MyVar. i need to set this within that form (a task i do understand!) and then in my child generated from FormChildTemplate check the variable and set it as something else, then to be able to use it again in FormParent. anyone get the idea and can tell me? thanks in advance, surgeproof. ---------------------------------------------- looking for hosting? ithium is good.

    H 1 Reply Last reply
    0
    • S surgeproof

      i posted before asking how to accomplish this and was replied to, but what they said didn't seem to work (else i was being more thick than usual! :p). this is what i need to do: in an MDI environment, have a variable in FormParent called MyVar. i need to set this within that form (a task i do understand!) and then in my child generated from FormChildTemplate check the variable and set it as something else, then to be able to use it again in FormParent. anyone get the idea and can tell me? thanks in advance, surgeproof. ---------------------------------------------- looking for hosting? ithium is good.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Either pass a reference to the parent form in the constructor (or a property) of the parent form's Type, or cast the child's MdiParent to the actual form's Type and access the public or internal property or field. If you just try to access it from MdiParent, it won't work because that returns a Form, which doesn't declare your field or property. That's what you cast it, since your parent form is a derivative of Form.

      Microsoft MVP, Visual C# My Articles

      S 1 Reply Last reply
      0
      • H Heath Stewart

        Either pass a reference to the parent form in the constructor (or a property) of the parent form's Type, or cast the child's MdiParent to the actual form's Type and access the public or internal property or field. If you just try to access it from MdiParent, it won't work because that returns a Form, which doesn't declare your field or property. That's what you cast it, since your parent form is a derivative of Form.

        Microsoft MVP, Visual C# My Articles

        S Offline
        S Offline
        surgeproof
        wrote on last edited by
        #3

        thanks. :) you couldn't explain that a little bit more please could you? do you mean when initialising my variable putting something like (FormParent) on the end? or would i try FormParent.MyVar beacuse this doesn't seem to work. thanks. ------------------------------------------------------- looking for hosting? ithium is good.

        H 1 Reply Last reply
        0
        • S surgeproof

          thanks. :) you couldn't explain that a little bit more please could you? do you mean when initialising my variable putting something like (FormParent) on the end? or would i try FormParent.MyVar beacuse this doesn't seem to work. thanks. ------------------------------------------------------- looking for hosting? ithium is good.

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          It's a simple matter of access. First, your field (not variable, which is a temporary variable declared in a method) or property (properties are recommended to use from other classes) must be either public or internal (for classes within a single assembly), or protected if using a derived class (which you probably wouldn't). Then you must make sure the child form can access that field or property either by casting MdiParent or passing a reference to your parent form (FormParent I'm assuming) to your child form. An example of the former is:

          public class MyChildForm : Form
          {
          protected void ShowParentFormsProperty()
          {
          FormParent form = (FormParent)MdiParent;
          if (form != null) MessageBox.Show(form.MyProperty);
          }
          }

          The latter way:

          public class MyChildForm : Form
          {
          private FormParent parent;
          public MyChildForm(FormParent parent)
          {
          this.parent = parent; // use "this" when field and parameter are the same.
          }
          protected void ShowParentFormsProperty()
          {
          if (parent != null) MessageBox.Show(parent.MyProperty);
          }
          }

          In both examples, your FormParent may look like this:

          public class FormParent : Form
          {
          public FormParent()
          {
          // If you want to pass the parent in the constructor...
          MyChildForm form = new MyChildForm(this);
          AddOwnedForm(form);
          }
          }

          Microsoft MVP, Visual C# My Articles

          S 1 Reply Last reply
          0
          • H Heath Stewart

            It's a simple matter of access. First, your field (not variable, which is a temporary variable declared in a method) or property (properties are recommended to use from other classes) must be either public or internal (for classes within a single assembly), or protected if using a derived class (which you probably wouldn't). Then you must make sure the child form can access that field or property either by casting MdiParent or passing a reference to your parent form (FormParent I'm assuming) to your child form. An example of the former is:

            public class MyChildForm : Form
            {
            protected void ShowParentFormsProperty()
            {
            FormParent form = (FormParent)MdiParent;
            if (form != null) MessageBox.Show(form.MyProperty);
            }
            }

            The latter way:

            public class MyChildForm : Form
            {
            private FormParent parent;
            public MyChildForm(FormParent parent)
            {
            this.parent = parent; // use "this" when field and parameter are the same.
            }
            protected void ShowParentFormsProperty()
            {
            if (parent != null) MessageBox.Show(parent.MyProperty);
            }
            }

            In both examples, your FormParent may look like this:

            public class FormParent : Form
            {
            public FormParent()
            {
            // If you want to pass the parent in the constructor...
            MyChildForm form = new MyChildForm(this);
            AddOwnedForm(form);
            }
            }

            Microsoft MVP, Visual C# My Articles

            S Offline
            S Offline
            surgeproof
            wrote on last edited by
            #5

            thanks, i'll try that and say how i got on soon. :) surgeproof ------------------------------------------------------- looking for hosting?ithium is good.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups