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. accessing the value of control between seperate Forms

accessing the value of control between seperate Forms

Scheduled Pinned Locked Moved C#
tutorialquestion
12 Posts 4 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.
  • F Offline
    F Offline
    Fred 34
    wrote on last edited by
    #1

    sorry, this is the second time that i ask this questio but i didn't get an explicit answer. anyway If we have 2 form (Form1 & Form2) whitin a project and we added a public object (for example textBox1) to Form1, how can we access to value of textBox1.text from Form2.i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr.textBox1.text doesn't show the actual value. it just show the prdefined value of it. please explicitly tell me how can i solve this.

    L D 4 Replies Last reply
    0
    • F Fred 34

      sorry, this is the second time that i ask this questio but i didn't get an explicit answer. anyway If we have 2 form (Form1 & Form2) whitin a project and we added a public object (for example textBox1) to Form1, how can we access to value of textBox1.text from Form2.i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr.textBox1.text doesn't show the actual value. it just show the prdefined value of it. please explicitly tell me how can i solve this.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Hello , In this example I will pass Textbox1.text in Form1 to Form2 TextBox1 Form1 Code :

      using System;
      using System.Windows.Forms;

      namespace WindowsFormsApplication2
      {
      public partial class Form1 : Form
      {
      public Form2 frm2;
      public Form1()
      {
      InitializeComponent();
      }

          private void button1\_Click(object sender, EventArgs e)
          {
              frm2 = new Form2();
              frm2.MyVar = textBox1.Text;
      
              frm2.Show();
              
          }
      
          private void textBox1\_TextChanged(object sender, EventArgs e)
          {
              if (frm2 != null)
              {
                  frm2.MyVar = textBox1.Text;
              }
          }
      
      }
      

      }

      Form2 Code

      using System;
      using System.Windows.Forms;

      namespace WindowsFormsApplication2
      {
      public partial class Form2 : Form
      {
      public Form2()
      {
      InitializeComponent();
      }

          public string MyVar
          {
              set
              {
                  textBox1.Text = value;
              }
          }
      }
      

      }

      F 1 Reply Last reply
      0
      • L Lost User

        Hello , In this example I will pass Textbox1.text in Form1 to Form2 TextBox1 Form1 Code :

        using System;
        using System.Windows.Forms;

        namespace WindowsFormsApplication2
        {
        public partial class Form1 : Form
        {
        public Form2 frm2;
        public Form1()
        {
        InitializeComponent();
        }

            private void button1\_Click(object sender, EventArgs e)
            {
                frm2 = new Form2();
                frm2.MyVar = textBox1.Text;
        
                frm2.Show();
                
            }
        
            private void textBox1\_TextChanged(object sender, EventArgs e)
            {
                if (frm2 != null)
                {
                    frm2.MyVar = textBox1.Text;
                }
            }
        
        }
        

        }

        Form2 Code

        using System;
        using System.Windows.Forms;

        namespace WindowsFormsApplication2
        {
        public partial class Form2 : Form
        {
        public Form2()
        {
        InitializeComponent();
        }

            public string MyVar
            {
                set
                {
                    textBox1.Text = value;
                }
            }
        }
        

        }

        F Offline
        F Offline
        Fred 34
        wrote on last edited by
        #3

        Hello and thanks to replied me. this is quite correct but suppose u want to send a plenty amount of textBox and and dataGridview and.... from a Form to another, can u tell me how can we have direct access to controls of that form. i am expert in Delphi and there is no such a problems there. thanks agian.

        1 Reply Last reply
        0
        • F Fred 34

          sorry, this is the second time that i ask this questio but i didn't get an explicit answer. anyway If we have 2 form (Form1 & Form2) whitin a project and we added a public object (for example textBox1) to Form1, how can we access to value of textBox1.text from Form2.i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr.textBox1.text doesn't show the actual value. it just show the prdefined value of it. please explicitly tell me how can i solve this.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          The cleanest way to do this is by the use of delegates[^]. You create a public delegate in Form1 which Form2 can subscribe to, then when the textbox content changes, Form2 will get notified of the change and can copy the content into its own variable.

          Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

          F 1 Reply Last reply
          0
          • L Lost User

            The cleanest way to do this is by the use of delegates[^]. You create a public delegate in Form1 which Form2 can subscribe to, then when the textbox content changes, Form2 will get notified of the change and can copy the content into its own variable.

            Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

            F Offline
            F Offline
            Fred 34
            wrote on last edited by
            #5

            thanks dear, but may u show me how? give me an example by writing a short codes.

            L L 2 Replies Last reply
            0
            • F Fred 34

              thanks dear, but may u show me how? give me an example by writing a short codes.

              L Offline
              L Offline
              lukeer
              wrote on last edited by
              #6

              Try this:

              public class Form1 : Form
              {
              public delegate void StringTransportingEvent(string value);
              public event StringTransportingEvent VariableChanged;

              override void OnLoad()
              {
                  Form2 secondForm = new Form2(this);
                  secondForm.Show();
              }
              
              textBox1\_TextChanged(object sender, EventArgs e)
              {
                  EventHandler eh = VariableChanged;
                  if( eh != null)
                  {
                      eh(textBox1.Text);
                  }
              }
              

              }

              public class Fomr2 : Form
              {
              public Form2(Form1 eventRaiser)
              {
              eventRaiser.VariableChanged += Form1_VariableChanged;
              }

              private void Form1\_VariableChanged(string value)
              {
                  this.TextBox1.Text = value;
              }
              

              }

              It creates a custom delegate that can transport a string. Form1 raises a custom event "VariableChanged" that Form2 subscribes to.

              Ciao, luker

              F 1 Reply Last reply
              0
              • F Fred 34

                thanks dear, but may u show me how? give me an example by writing a short codes.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                If you go to the link I posted you will see a very comprehensive tutorial on the subject. While it does not answer your question directly, it is a good learning tool to help in developing your skills.

                Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                1 Reply Last reply
                0
                • F Fred 34

                  sorry, this is the second time that i ask this questio but i didn't get an explicit answer. anyway If we have 2 form (Form1 & Form2) whitin a project and we added a public object (for example textBox1) to Form1, how can we access to value of textBox1.text from Form2.i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr.textBox1.text doesn't show the actual value. it just show the prdefined value of it. please explicitly tell me how can i solve this.

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  faraz34 wrote:

                  but i didn't get an explicit answer

                  Yes you did. What you didn't get was an exact code sample you could copy'n'adapt to your code. Luc's link was very explicit and explained the concepts quite clearly. By doing this you are also making your forms completely dependant on one another. You now cannot change one form without affecting the other. This means you have a very bad data model. You shouldn't be accessing controls on one form from another. You should be putting the data from that form in a container object so that it can be passed back to or retrieved by the calling code.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak

                  F 1 Reply Last reply
                  0
                  • L lukeer

                    Try this:

                    public class Form1 : Form
                    {
                    public delegate void StringTransportingEvent(string value);
                    public event StringTransportingEvent VariableChanged;

                    override void OnLoad()
                    {
                        Form2 secondForm = new Form2(this);
                        secondForm.Show();
                    }
                    
                    textBox1\_TextChanged(object sender, EventArgs e)
                    {
                        EventHandler eh = VariableChanged;
                        if( eh != null)
                        {
                            eh(textBox1.Text);
                        }
                    }
                    

                    }

                    public class Fomr2 : Form
                    {
                    public Form2(Form1 eventRaiser)
                    {
                    eventRaiser.VariableChanged += Form1_VariableChanged;
                    }

                    private void Form1\_VariableChanged(string value)
                    {
                        this.TextBox1.Text = value;
                    }
                    

                    }

                    It creates a custom delegate that can transport a string. Form1 raises a custom event "VariableChanged" that Form2 subscribes to.

                    Ciao, luker

                    F Offline
                    F Offline
                    Fred 34
                    wrote on last edited by
                    #9

                    thanks LUK, I think got it. this was a question that had occupied my mind for many days. and u solved it for me. thank a lot again. persons like u rich this forum.

                    1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      faraz34 wrote:

                      but i didn't get an explicit answer

                      Yes you did. What you didn't get was an exact code sample you could copy'n'adapt to your code. Luc's link was very explicit and explained the concepts quite clearly. By doing this you are also making your forms completely dependant on one another. You now cannot change one form without affecting the other. This means you have a very bad data model. You shouldn't be accessing controls on one form from another. You should be putting the data from that form in a container object so that it can be passed back to or retrieved by the calling code.

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak

                      F Offline
                      F Offline
                      Fred 34
                      wrote on last edited by
                      #10

                      Yes dear, u are right.

                      D 1 Reply Last reply
                      0
                      • F Fred 34

                        sorry, this is the second time that i ask this questio but i didn't get an explicit answer. anyway If we have 2 form (Form1 & Form2) whitin a project and we added a public object (for example textBox1) to Form1, how can we access to value of textBox1.text from Form2.i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr.textBox1.text doesn't show the actual value. it just show the prdefined value of it. please explicitly tell me how can i solve this.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        Your original post contains some really good answers that can help you achieve what you are trying to accomplish. But your problem is that you expect too much spoon feeding and if this is the case, you're not going to stop littering the forum by asking the same question again and again until someone writes the entire code for you for free. People here try to teach you how to fish, but you're expecting the fish itself everytime. Pathetic.

                        1 Reply Last reply
                        0
                        • F Fred 34

                          Yes dear, u are right.

                          D Offline
                          D Offline
                          Dave Kreskowiak
                          wrote on last edited by
                          #12

                          Unless it's your wife, you don't call anyone in the Western world "dear".

                          A guide to posting questions on CodeProject[^]
                          Dave Kreskowiak

                          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