Passing a variable from one form to the next

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gggram2000
    New Member
    • Nov 2007
    • 97

    Passing a variable from one form to the next

    I'd like to know how I go about passing two variables from one form to the next.
    For example, I can pass a variable like this:
    This demonstrates how to pass a variable to a form when the form is opened.
    Code:
    private void OpenForm_Click(object sender, System.EventArgs e)
    {
    // Get the value to be passed
    string parentValue = this.UserResponse.Text;
    
    // Create the child form and pass the value in the constructor
    this.ChildForm = new ChildForm(parentValue);
    
    // Show the form which will display the user's value.
    this.ChildForm.Show();
    }
    Then in the new form:
    Code:
    public string ValueFromParent
    {
                    set
    	{
    	               		this.ParentValue.Text = value;
    	}
    }
    Since i want to pass two variables from two dateTime pickers I would like something like this:
    Code:
    private void OpenForm_Click(object sender, System.EventArgs e)
    {
    // Get the value to be passed
    string parentValue = this.dateTimePicker1.Text;
    string parentValue2 = this.dateTimePicker2.Text;
    
    
    // Create the child form and pass the value in the constructor
    this.ChildForm = new ChildForm(parentValue,parentValue2);
    
    // Show the form which will display the user's value.
    this.ChildForm.Show();
    }
    Where i declare two parent values but that does not work...any help please
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Create a constructor for your ChildForm that takes two DateTime objects as arguments?

    Comment

    • gggram2000
      New Member
      • Nov 2007
      • 97

      #3
      Originally posted by Plater
      Create a constructor for your ChildForm that takes two DateTime objects as arguments?
      I'm sorry but I don't know how to do that, is there anything I can reference caz i cant find any example

      Comment

      • TRScheel
        Recognized Expert Contributor
        • Apr 2007
        • 638

        #4
        Originally posted by gggram2000
        I'm sorry but I don't know how to do that, is there anything I can reference caz i cant find any example
        Your child form should have something similiar to:

        [CODE=vb]
        public ChildForm()
        {
        InitializeCompo nent();
        }[/CODE]

        or at least would have when you started. Change that to:

        [CODE=vb]
        public ChildForm(DateT ime value1, DateTime value2)
        {
        InitializeCompo nent();
        // do stuff here
        }[/CODE]


        EDIT:

        In your example you grabbed the text so you would actually want:
        [CODE=vb]
        public ChildForm(strin g value1, string value2)
        {
        InitializeCompo nent();
        // do stuff here
        }[/CODE]

        Comment

        • adamrlee
          New Member
          • Jun 2008
          • 2

          #5
          It is not good to have too many public vars.
          Try making more getter/setter methods.


          For example if you want to pass var 'x' from Form1 to Form2 simply call Form2.setMyX(x)

          in Form2 Sub setMyX(byVal new_x)
          x=new_x
          End Sub

          Comment

          • gggram2000
            New Member
            • Nov 2007
            • 97

            #6
            Originally posted by TRScheel
            Your child form should have something similiar to:

            [CODE=vb]
            public ChildForm()
            {
            InitializeCompo nent();
            }[/CODE]

            or at least would have when you started. Change that to:

            [CODE=vb]
            public ChildForm(DateT ime value1, DateTime value2)
            {
            InitializeCompo nent();
            // do stuff here
            }[/CODE]


            EDIT:

            In your example you grabbed the text so you would actually want:
            [CODE=vb]
            public ChildForm(strin g value1, string value2)
            {
            InitializeCompo nent();
            // do stuff here
            }[/CODE]
            Ok I'll try that and let u kno...thanks

            Comment

            • gggram2000
              New Member
              • Nov 2007
              • 97

              #7
              Hey just wanted say it worked great, so simple yet easy to miss...thanks a bunch!

              Comment

              Working...