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. How can we access to an object of a form from another form?

How can we access to an object of a form from another form?

Scheduled Pinned Locked Moved C#
questiontutorial
8 Posts 6 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

    If we have 2 form (Form1 & Form2) whitin a project and we added an object (for example textBox1) to Form1, how can we access to textBox1 from Form2. i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr doesn't show textBox1 durring writing codes. i suppose fr.textBox1 must be correct but it is not. please tell ne how can i do that.

    P L L H Q 5 Replies Last reply
    0
    • F Fred 34

      If we have 2 form (Form1 & Form2) whitin a project and we added an object (for example textBox1) to Form1, how can we access to textBox1 from Form2. i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr doesn't show textBox1 durring writing codes. i suppose fr.textBox1 must be correct but it is not. please tell ne how can i do that.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      The simplest but wrongest way would be to make the TextBox public. A better way is to make a public property that allows the other form to get and/or set the Text property of the TextBox. There are other ways that may be more appropriate to your situation, but we would need to know more about what you are trying to do. However you do it, you may have to protect against cross-thread access problems.

      F 1 Reply Last reply
      0
      • F Fred 34

        If we have 2 form (Form1 & Form2) whitin a project and we added an object (for example textBox1) to Form1, how can we access to textBox1 from Form2. i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr doesn't show textBox1 durring writing codes. i suppose fr.textBox1 must be correct but it is not. please tell ne how can i do that.

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

        You would have to change the access modifier for the textbox.

        Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Individuality is fine, as long as we do it together - F. Burns

        F 1 Reply Last reply
        0
        • L Lost User

          You would have to change the access modifier for the textbox.

          Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Individuality is fine, as long as we do it together - F. Burns

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

          by changing the modifier I only can get the properties of that object (the values can be read) how can we set the properties to a new value?

          1 Reply Last reply
          0
          • P PIEBALDconsult

            The simplest but wrongest way would be to make the TextBox public. A better way is to make a public property that allows the other form to get and/or set the Text property of the TextBox. There are other ways that may be more appropriate to your situation, but we would need to know more about what you are trying to do. However you do it, you may have to protect against cross-thread access problems.

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

            I want to get the properties value of that object and change it to a new value as I want.

            1 Reply Last reply
            0
            • F Fred 34

              If we have 2 form (Form1 & Form2) whitin a project and we added an object (for example textBox1) to Form1, how can we access to textBox1 from Form2. i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr doesn't show textBox1 durring writing codes. i suppose fr.textBox1 must be correct but it is not. please tell ne how can i do that.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Pass value between forms using events[^] explains the correct way to do such things. What you had, creating yet another Form1, is not going to work, as you are looking at one instance of Form1 and programmatically changing another unrelated instance. Making Controls public would work but is hardly acceptable, as now "the whole world" could modify, move, resize, etc. said Control. :)

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              1 Reply Last reply
              0
              • F Fred 34

                If we have 2 form (Form1 & Form2) whitin a project and we added an object (for example textBox1) to Form1, how can we access to textBox1 from Form2. i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr doesn't show textBox1 durring writing codes. i suppose fr.textBox1 must be correct but it is not. please tell ne how can i do that.

                H Offline
                H Offline
                hellono1
                wrote on last edited by
                #7

                use delegate to access. see this link it may be help u. http://www.c-sharpcorner.com/UploadFile/mosessaur/winformsdelegates09042006094826AM/winformsdelegates.aspx[^]

                1 Reply Last reply
                0
                • F Fred 34

                  If we have 2 form (Form1 & Form2) whitin a project and we added an object (for example textBox1) to Form1, how can we access to textBox1 from Form2. i have done this in form2 by these codes { Form1 fr = new Form1(); . . . } but fr doesn't show textBox1 durring writing codes. i suppose fr.textBox1 must be correct but it is not. please tell ne how can i do that.

                  Q Offline
                  Q Offline
                  Quintain
                  wrote on last edited by
                  #8

                  I hope this will help: Form1 frm=new Form1(); frm.Show(); TextBox textBox=frm.Controls.Find("textBox1",false)[0] as TextBox; textBox.Text = "new text";

                  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