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 to set text of textbox with value user entered in another form at form close

How to set text of textbox with value user entered in another form at form close

Scheduled Pinned Locked Moved C#
tutorial
10 Posts 4 Posters 10 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.
  • L Offline
    L Offline
    linqabc
    wrote on last edited by
    #1

    i have two forms form1 contain textbox1 and buttonsearch //click form2 is opend form2 contains textbox2 i want when user close form2 the value of textbox1 equals value of textbox2 in form2

    B OriginalGriffO 2 Replies Last reply
    0
    • L linqabc

      i have two forms form1 contain textbox1 and buttonsearch //click form2 is opend form2 contains textbox2 i want when user close form2 the value of textbox1 equals value of textbox2 in form2

      B Offline
      B Offline
      Brij
      wrote on last edited by
      #2

      So what is the problem you are facing.Can have some property in form1 which set the value in textbox1 and set this property from form2. Are you asking somthing else?

      Cheers!! Brij Check my latest Article :Exploring ASP.NET Validators

      OriginalGriffO L 2 Replies Last reply
      0
      • L linqabc

        i have two forms form1 contain textbox1 and buttonsearch //click form2 is opend form2 contains textbox2 i want when user close form2 the value of textbox1 equals value of textbox2 in form2

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        If you specifically want an operation to happen when a form closes, then add an event handler to the close event of that form. Form1 - assuming form 1 creates form2 and displayed it with form2.Show():

        form2.Closed += new EventHandler(form2_Closed);
        ...
        void form2.Closed(object sender, EventArgs e)
        {
        textbox1.Text = form2.MyTextProperty;
        }

        Declare

        public string MyTextProperty
        {
        get { return textboxInForm2.Text; }
        set { textboxInForm2.Text = value; }
        }

        In form2. If you show form2 via ShowDialog(), then just access the property - you don't need the event.

        You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        L 1 Reply Last reply
        0
        • B Brij

          So what is the problem you are facing.Can have some property in form1 which set the value in textbox1 and set this property from form2. Are you asking somthing else?

          Cheers!! Brij Check my latest Article :Exploring ASP.NET Validators

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Not really good practice - it means form2 has to know about form1 and cannot be used without it. Better to use an event handler in form1 and a property in form2, as many forms can then use form2. Better still is to use a custom event in form2 which returns the value via a customized EventArgs.

          You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          B 1 Reply Last reply
          0
          • B Brij

            So what is the problem you are facing.Can have some property in form1 which set the value in textbox1 and set this property from form2. Are you asking somthing else?

            Cheers!! Brij Check my latest Article :Exploring ASP.NET Validators

            L Offline
            L Offline
            linqabc
            wrote on last edited by
            #5

            textbox1 i can't see it in form to its modifier private

            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              If you specifically want an operation to happen when a form closes, then add an event handler to the close event of that form. Form1 - assuming form 1 creates form2 and displayed it with form2.Show():

              form2.Closed += new EventHandler(form2_Closed);
              ...
              void form2.Closed(object sender, EventArgs e)
              {
              textbox1.Text = form2.MyTextProperty;
              }

              Declare

              public string MyTextProperty
              {
              get { return textboxInForm2.Text; }
              set { textboxInForm2.Text = value; }
              }

              In form2. If you show form2 via ShowDialog(), then just access the property - you don't need the event.

              You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

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

              i solved it by another way form1 form2 f2 = new form2(); f2.showdialog(); f2.dispose(); if(f2.IsDispose) { textbox1.text = valuetext; } // value text is static variable in form2 thank u for ur replay

              OriginalGriffO 1 Reply Last reply
              0
              • L linqabc

                i solved it by another way form1 form2 f2 = new form2(); f2.showdialog(); f2.dispose(); if(f2.IsDispose) { textbox1.text = valuetext; } // value text is static variable in form2 thank u for ur replay

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                :omg: DO NOT DO THAT! See here[^] When you dispose a control, you have no control over when it's memory is available - the garbage collector is a liberty to remove it at any time. The code may work now, in testing, and then fail intermittently for no apparent reason later. To add to that: What do you think static variables are? Again, this will cause you problems. Read up on the difference between a STATIC and a PUBLIC variable. Also, it is considered bad practice to expose fields directly, and it makes it difficult to implement changes later. Use a property instead. To add to the list: Don't use f2.ShowDialog() alone - always surround it with an "if" as in:

                if (f2.ShowDialog() == DialogResult.OK)
                {
                ...
                }

                Otherwise, what happens if the user decides he doesn't want to enter a value?

                You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                L S 2 Replies Last reply
                0
                • OriginalGriffO OriginalGriff

                  :omg: DO NOT DO THAT! See here[^] When you dispose a control, you have no control over when it's memory is available - the garbage collector is a liberty to remove it at any time. The code may work now, in testing, and then fail intermittently for no apparent reason later. To add to that: What do you think static variables are? Again, this will cause you problems. Read up on the difference between a STATIC and a PUBLIC variable. Also, it is considered bad practice to expose fields directly, and it makes it difficult to implement changes later. Use a property instead. To add to the list: Don't use f2.ShowDialog() alone - always surround it with an "if" as in:

                  if (f2.ShowDialog() == DialogResult.OK)
                  {
                  ...
                  }

                  Otherwise, what happens if the user decides he doesn't want to enter a value?

                  You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                  L Offline
                  L Offline
                  linqabc
                  wrote on last edited by
                  #8

                  ok i will try it

                  1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Not really good practice - it means form2 has to know about form1 and cannot be used without it. Better to use an event handler in form1 and a property in form2, as many forms can then use form2. Better still is to use a custom event in form2 which returns the value via a customized EventArgs.

                    You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                    B Offline
                    B Offline
                    Brij
                    wrote on last edited by
                    #9

                    Obiuosly this is thebetter way and having least coupling.

                    Cheers!! Brij Check my latest Article :Exploring ASP.NET Validators

                    1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      :omg: DO NOT DO THAT! See here[^] When you dispose a control, you have no control over when it's memory is available - the garbage collector is a liberty to remove it at any time. The code may work now, in testing, and then fail intermittently for no apparent reason later. To add to that: What do you think static variables are? Again, this will cause you problems. Read up on the difference between a STATIC and a PUBLIC variable. Also, it is considered bad practice to expose fields directly, and it makes it difficult to implement changes later. Use a property instead. To add to the list: Don't use f2.ShowDialog() alone - always surround it with an "if" as in:

                      if (f2.ShowDialog() == DialogResult.OK)
                      {
                      ...
                      }

                      Otherwise, what happens if the user decides he doesn't want to enter a value?

                      You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                      S Offline
                      S Offline
                      supercat9
                      wrote on last edited by
                      #10

                      OriginalGriff wrote:

                      When you dispose a control, you have no control over when it's memory is available - the garbage collector is a liberty to remove it at any time. The code may work now, in testing, and then fail intermittently for no apparent reason later.

                      The garbage collector cannot remove a disposed control if any references to it exist. If there are any user fields or properties, within a control, those fields will continue to be valid, even after the control is disposed, with the exception of properties that explicitly check for isDisposed and will throw exception exception if it is set. As for the best way to return the content of a form that is invoked modally, I would suggest that rather than using events, one (1) define a class which contains the form's contents and an enumerated type listing methods of exit; (2) define a method in the form which will ShowDialog itself and then return an object of the class in '1' filled with the values from the form; (3) define a static method in the form which will create a new instance, call the method in '2', and dispose of the instance. That would avoid any excess dependencies between the child form and the main form, and avoid having to add any extra code into the calling form.

                      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