Passing values from form2 to form1 using objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shoram
    New Member
    • Feb 2007
    • 26

    Passing values from form2 to form1 using objects

    it's no longer a problem passing variables between two form its just this

    form1 is still open

    i do a few actions that change a var in form2

    and i want the new number to be send to form1 immediatly after those changes are done

    how can i do so without opening a new form1 and sending the var into the only form1 that is open
  • vijaydiwakar
    Contributor
    • Feb 2007
    • 579

    #2
    Originally posted by shoram
    it's no longer a problem passing variables between two form its just this

    form1 is still open

    i do a few actions that change a var in form2

    and i want the new number to be send to form1 immediatly after those changes are done

    how can i do so without opening a new form1 and sending the var into the only form1 that is open
    pls explain thy problem in detail

    Comment

    • shoram
      New Member
      • Feb 2007
      • 26

      #3
      Originally posted by vijaydiwakar
      pls explain thy problem in detail
      the problem is that i dont know how to write somthing in form1 while im in form2
      without opening a new form1

      Comment

      • vijaydiwakar
        Contributor
        • Feb 2007
        • 579

        #4
        Originally posted by shoram
        the problem is that i dont know how to write somthing in form1 while im in form2
        without opening a new form1
        to access the form u must have to create the object.
        try this code
        for that add two forms and one module and try this code
        Code:
        'in form1
        Private Sub Form_Load()
           ReDim myObj(0)
           Set myObj(0) = Me
           Form2.Show
        End Sub
        'in form2
        Private Sub Form_Click()
        myObj(0).Caption = "hai"
        End Sub
        ' in module
        Public myObj() As Form
        This will defenetly solve thy problem

        Comment

        • shoram
          New Member
          • Feb 2007
          • 26

          #5
          im sorry but can you do it in c# i dont know anything in vb and thanks for helping

          Comment

          • vijaydiwakar
            Contributor
            • Feb 2007
            • 579

            #6
            Originally posted by shoram
            im sorry but can you do it in c# i dont know anything in vb and thanks for helping
            I'm sorry i don't no anything in C#

            Comment

            • kenobewan
              Recognized Expert Specialist
              • Dec 2006
              • 4871

              #7
              Suggest that you find a converter.

              Comment

              • shoram
                New Member
                • Feb 2007
                • 26

                #8
                where can i find a converter? i didnt know they exist

                Comment

                • SammyB
                  Recognized Expert Contributor
                  • Mar 2007
                  • 807

                  #9
                  It's the same principle in C#, but you will need to pass the Form1 object to Form2 and change the Modifiers property of any control that you want to access from Privater to Internal. HTH --Sam

                  You can also create an event in Form2 that Form1 could fire whenever something changes. That is much more complecated, but easily done. If that is what you want, you should tell us whether you are using 2003 or 2005.

                  Here is the code for Form1:
                  Code:
                  		private void button1_Click(object sender, EventArgs e)
                  		{
                  			Form2 FrmChild = new Form2(this);
                  			FrmChild.Show();
                  		}
                  Here is the code for Form2. Notice that the constructor is overridden to get the Form1 object:
                  Code:
                  		private Form1 FrmParent;
                  		public Form2(Form1 parent)
                  		{
                  			InitializeComponent();
                  			FrmParent = parent;
                  		}
                  		private void button1_Click(object sender, EventArgs e)
                  		{
                  			this.textBox1.Text = FrmParent.textBox1.Text;
                  		}

                  Comment

                  • shoram
                    New Member
                    • Feb 2007
                    • 26

                    #10
                    sorry for the late replay but i have 2003 and the method you gave me doesnt work
                    if its not so hard can you please tell me how to do that in 2003
                    thanks in advance

                    Comment

                    • SammyB
                      Recognized Expert Contributor
                      • Mar 2007
                      • 807

                      #11
                      Originally posted by shoram
                      sorry for the late replay but i have 2003 and the method you gave me doesnt work
                      if its not so hard can you please tell me how to do that in 2003
                      thanks in advance
                      Well, I did do it in 2005, but it does not use any new features. What problem are you having?

                      Comment

                      • shoram
                        New Member
                        • Feb 2007
                        • 26

                        #12
                        i over came the error but how do i insert the value into the textbox?

                        Comment

                        • shoram
                          New Member
                          • Feb 2007
                          • 26

                          #13
                          Originally posted by shoram
                          i over came the error but how do i insert the value into the textbox?
                          ok forget that replay, my problem is diffrent
                          i need to pass a value from form2 to form1 not the other way

                          Comment

                          • SammyB
                            Recognized Expert Contributor
                            • Mar 2007
                            • 807

                            #14
                            Originally posted by shoram
                            ok forget that replay, my problem is diffrent
                            i need to pass a value from form2 to form1 not the other way
                            That's what I did in post #9 above, only I put the value into a Textbox on Form1. If you need to pass back a value, then you will need to create a Form1 property for Form2 to use:
                            Code:
                            public int Know;
                            private void button1_Click(object sender, EventArgs e)
                            {
                            	Form2 FrmChild = new Form2(this);
                            	FrmChild.ShowDialog();
                            	MessageBox.Show(Know.ToString());
                            }
                            Then, Form2 just puts the value into that property:
                            Code:
                            private Form1 FrmParent;
                            public Form2(Form1 parent)
                            {
                            	InitializeComponent();
                            	FrmParent = parent;
                            }
                            private void button1_Click(object sender, EventArgs e)
                            {
                            	FrmParent.Know = 42;
                            	this.Close();
                            }

                            Comment

                            • shoram
                              New Member
                              • Feb 2007
                              • 26

                              #15
                              by that answer i can surly belive that you saw the hitch hikcer galxci guide but not about that. when i did the thing you told me to it opened a diffrent form telling me the answer and i wanted that the form1 to get the value (without closing)

                              Comment

                              Working...