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. data transfers between two forms

data transfers between two forms

Scheduled Pinned Locked Moved C#
helptutorialquestion
14 Posts 7 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.
  • B Blue_Boy

    In Form1

    public partial class Form1: Form
    {
    internal static string textboxdata = string.Emtpy;
    public Form1()
    {
    InitializeComponent();
    }

    private void btnEkle_Click(object sender, EventArgs e)
    {
    textboxdata = txtGsm1.Text ;

    }
    }

    in Form2 call this way

    txtGsm1.Text = Form1.textboxdata ;


    I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

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

    Why suggest a static solution? :wtf: Do you really think that is a good idea? If so, then perhaps you ought to re-read your C# books / lecture notes and brush up on basic OOP.

    Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

    "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
    • OriginalGriffO OriginalGriff

      Look at your code. See that keyword on the first line of each method? The new keyword? Now, what do you think that does?

      1. Create a totally new variable, unconnected to any previous versions.

      or

      1. Access the correct old version of the variable that you had kicking around somewhere else in your program, but not to worry, the compiler will sort out which one your mean to use when it gets to it.

      Now, do you see what is wrong?

      Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

      E Offline
      E Offline
      Erdinc27
      wrote on last edited by
      #5

      yeah i got what u mean..but what should i do instead of that ?

      OriginalGriffO 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Why suggest a static solution? :wtf: Do you really think that is a good idea? If so, then perhaps you ought to re-read your C# books / lecture notes and brush up on basic OOP.

        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

        B Offline
        B Offline
        Blue_Boy
        wrote on last edited by
        #6

        :zzz:


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

        1 Reply Last reply
        0
        • E Erdinc27

          yeah i got what u mean..but what should i do instead of that ?

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

          Use the existing instance.

          private void btnRehber_Click(object sender, EventArgs e)
          {
          Form2 rhbForm = new Form2();
          rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text;
          rhbForm.ShowDialog();
          txtGsm1.Text = rhbForm.txtGsm1.Text;
          txtGsm2.Text = rhbForm.txtGsm2.Text;
          txtNumara.Text = rhbForm.txtNumara.Text;
          }

          Except that won't work as it is because the TextBoxes are (correctly) private to Form2. So instead add them as properties to Form2:

          public class Form2 : Form
          {
          ...
          public string Gsm1
          {
          get { return txtGsm1.Text; }
          }
          ...
          }

          Reapeat for Gsm2 and Numara. Then access Gsm1 in Form1:

          private void btnRehber_Click(object sender, EventArgs e)
          {
          Form2 rhbForm = new Form2();
          rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text;
          rhbForm.ShowDialog();
          txtGsm1.Text = rhbForm.Gsm1;
          txtGsm2.Text = rhbForm.Gsm2;
          txtNumara.Text = rhbForm.Numara;
          }

          It would also be a good idea to make "yetkili" a property as well, this time with a setter as well as a getter. The idea is that Form1 does not need to know how Form2 works - just that it can get Gsm data for it. You can then change the internals of Form2 as much as you like, without having to alter Form1

          Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

          "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

          E 1 Reply Last reply
          0
          • E Erdinc27

            hey guys.. i have a problem..actually i saw many document about it and i applied on my project but i couldnt solve it..i want to transfer datas btw two forms..for example i open Form2 with a button in Form1 and i want to get the text of the textbox in Form2 as a text of textbox in Form1 to do that i wrote that codes below but still i have nothing here i opened Form2 private void btnRehber_Click(object sender, EventArgs e) { Form2 rhbForm = new Form2(); rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text; rhbForm.ShowDialog(); } and here i filled the textboxes in Form2 and submit the values to the textboxes in Form1 private void btnEkle_Click(object sender, EventArgs e) { frmFirma = new Form1(); frmFirma.txtGsm1.Text = txtGsm1.Text; frmFirma.txtGsm2.Text = txtGsm2.Text; frmFirma.txtNumara.Text = txtNumara.Text; } do i make something wrong ? or is there another way ? thanks guys for help

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #8

            It always amazes me that people try to write the most tortuous logic to communicate between two disparate items in an application when there are mechanisms built into the framework that allow you to do this with ease. Consider the problem if you want to do this with a modeless form; you want to one form to be able to respond to something that happened in another form; can you think of a mechanism that allows you to communicate in this fashion? I am, of course, talking about using events here. Now, in your case, there is an even simpler way to achieve this. When you create an instance of Form2 (that's not a good name by the way, your naming convention should always indicate the intent of the item you are naming), you then display it using ShowDialog. This means that processing in Form1 halts until Form2 returns control to it - this, of course, means that you have access to the properties and methods inside Form2 until you Dispose of it. What you should do, in Form2, is create public properties that expose the values inside the textboxes and, once control has been returned to Form1, you should then take the values from it. Here's one way to code it:

            private void btnRehber_Click(object sender, EventArgs e)
            {
            using (Form2 rhbForm = new Form2()) // Please rename Form2.
            {
            // It's good practice to Dispose of forms. By wrapping
            // this in a using block, the form will be disposed of
            // automatically.
            rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text;
            if (rhbForm.ShowDialog() == DialogResult.OK)
            {
            // Only update the fields if the user clicked OK.
            txtGsm1.Text = rhbForm.Gsm1;
            txtGsm2.Text = rhbForm.Gsm2;
            frmFirma.txtNumara.Text = rhbForm.Numara;
            }
            }
            }

            Then, in Form2, I'd create Gsm1, Gsm2 and Numara as public (or internal) readonly properties which simply wrap access to the relevant textboxes.

            I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Onyx

            E F J 3 Replies Last reply
            0
            • OriginalGriffO OriginalGriff

              Use the existing instance.

              private void btnRehber_Click(object sender, EventArgs e)
              {
              Form2 rhbForm = new Form2();
              rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text;
              rhbForm.ShowDialog();
              txtGsm1.Text = rhbForm.txtGsm1.Text;
              txtGsm2.Text = rhbForm.txtGsm2.Text;
              txtNumara.Text = rhbForm.txtNumara.Text;
              }

              Except that won't work as it is because the TextBoxes are (correctly) private to Form2. So instead add them as properties to Form2:

              public class Form2 : Form
              {
              ...
              public string Gsm1
              {
              get { return txtGsm1.Text; }
              }
              ...
              }

              Reapeat for Gsm2 and Numara. Then access Gsm1 in Form1:

              private void btnRehber_Click(object sender, EventArgs e)
              {
              Form2 rhbForm = new Form2();
              rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text;
              rhbForm.ShowDialog();
              txtGsm1.Text = rhbForm.Gsm1;
              txtGsm2.Text = rhbForm.Gsm2;
              txtNumara.Text = rhbForm.Numara;
              }

              It would also be a good idea to make "yetkili" a property as well, this time with a setter as well as a getter. The idea is that Form1 does not need to know how Form2 works - just that it can get Gsm data for it. You can then change the internals of Form2 as much as you like, without having to alter Form1

              Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

              E Offline
              E Offline
              Erdinc27
              wrote on last edited by
              #9

              thanks man i did as i wished with your help and codes really thank u so much :))

              1 Reply Last reply
              0
              • P Pete OHanlon

                It always amazes me that people try to write the most tortuous logic to communicate between two disparate items in an application when there are mechanisms built into the framework that allow you to do this with ease. Consider the problem if you want to do this with a modeless form; you want to one form to be able to respond to something that happened in another form; can you think of a mechanism that allows you to communicate in this fashion? I am, of course, talking about using events here. Now, in your case, there is an even simpler way to achieve this. When you create an instance of Form2 (that's not a good name by the way, your naming convention should always indicate the intent of the item you are naming), you then display it using ShowDialog. This means that processing in Form1 halts until Form2 returns control to it - this, of course, means that you have access to the properties and methods inside Form2 until you Dispose of it. What you should do, in Form2, is create public properties that expose the values inside the textboxes and, once control has been returned to Form1, you should then take the values from it. Here's one way to code it:

                private void btnRehber_Click(object sender, EventArgs e)
                {
                using (Form2 rhbForm = new Form2()) // Please rename Form2.
                {
                // It's good practice to Dispose of forms. By wrapping
                // this in a using block, the form will be disposed of
                // automatically.
                rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text;
                if (rhbForm.ShowDialog() == DialogResult.OK)
                {
                // Only update the fields if the user clicked OK.
                txtGsm1.Text = rhbForm.Gsm1;
                txtGsm2.Text = rhbForm.Gsm2;
                frmFirma.txtNumara.Text = rhbForm.Numara;
                }
                }
                }

                Then, in Form2, I'd create Gsm1, Gsm2 and Numara as public (or internal) readonly properties which simply wrap access to the relevant textboxes.

                I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Onyx

                E Offline
                E Offline
                Erdinc27
                wrote on last edited by
                #10

                hey friend i did changed my form names as u suggested... thanks for your suggestion..

                P 1 Reply Last reply
                0
                • E Erdinc27

                  hey friend i did changed my form names as u suggested... thanks for your suggestion..

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #11

                  You're welcome.

                  I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                  Forgive your enemies - it messes with their heads

                  My blog | My articles | MoXAML PowerToys | Onyx

                  1 Reply Last reply
                  0
                  • P Pete OHanlon

                    It always amazes me that people try to write the most tortuous logic to communicate between two disparate items in an application when there are mechanisms built into the framework that allow you to do this with ease. Consider the problem if you want to do this with a modeless form; you want to one form to be able to respond to something that happened in another form; can you think of a mechanism that allows you to communicate in this fashion? I am, of course, talking about using events here. Now, in your case, there is an even simpler way to achieve this. When you create an instance of Form2 (that's not a good name by the way, your naming convention should always indicate the intent of the item you are naming), you then display it using ShowDialog. This means that processing in Form1 halts until Form2 returns control to it - this, of course, means that you have access to the properties and methods inside Form2 until you Dispose of it. What you should do, in Form2, is create public properties that expose the values inside the textboxes and, once control has been returned to Form1, you should then take the values from it. Here's one way to code it:

                    private void btnRehber_Click(object sender, EventArgs e)
                    {
                    using (Form2 rhbForm = new Form2()) // Please rename Form2.
                    {
                    // It's good practice to Dispose of forms. By wrapping
                    // this in a using block, the form will be disposed of
                    // automatically.
                    rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text;
                    if (rhbForm.ShowDialog() == DialogResult.OK)
                    {
                    // Only update the fields if the user clicked OK.
                    txtGsm1.Text = rhbForm.Gsm1;
                    txtGsm2.Text = rhbForm.Gsm2;
                    frmFirma.txtNumara.Text = rhbForm.Numara;
                    }
                    }
                    }

                    Then, in Form2, I'd create Gsm1, Gsm2 and Numara as public (or internal) readonly properties which simply wrap access to the relevant textboxes.

                    I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                    Forgive your enemies - it messes with their heads

                    My blog | My articles | MoXAML PowerToys | Onyx

                    F Offline
                    F Offline
                    fjdiewornncalwe
                    wrote on last edited by
                    #12

                    Got to agree with Pete here. Simple, elegant solutions will always work better than convoluted logic.

                    1 Reply Last reply
                    0
                    • P Pete OHanlon

                      It always amazes me that people try to write the most tortuous logic to communicate between two disparate items in an application when there are mechanisms built into the framework that allow you to do this with ease. Consider the problem if you want to do this with a modeless form; you want to one form to be able to respond to something that happened in another form; can you think of a mechanism that allows you to communicate in this fashion? I am, of course, talking about using events here. Now, in your case, there is an even simpler way to achieve this. When you create an instance of Form2 (that's not a good name by the way, your naming convention should always indicate the intent of the item you are naming), you then display it using ShowDialog. This means that processing in Form1 halts until Form2 returns control to it - this, of course, means that you have access to the properties and methods inside Form2 until you Dispose of it. What you should do, in Form2, is create public properties that expose the values inside the textboxes and, once control has been returned to Form1, you should then take the values from it. Here's one way to code it:

                      private void btnRehber_Click(object sender, EventArgs e)
                      {
                      using (Form2 rhbForm = new Form2()) // Please rename Form2.
                      {
                      // It's good practice to Dispose of forms. By wrapping
                      // this in a using block, the form will be disposed of
                      // automatically.
                      rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text;
                      if (rhbForm.ShowDialog() == DialogResult.OK)
                      {
                      // Only update the fields if the user clicked OK.
                      txtGsm1.Text = rhbForm.Gsm1;
                      txtGsm2.Text = rhbForm.Gsm2;
                      frmFirma.txtNumara.Text = rhbForm.Numara;
                      }
                      }
                      }

                      Then, in Form2, I'd create Gsm1, Gsm2 and Numara as public (or internal) readonly properties which simply wrap access to the relevant textboxes.

                      I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                      Forgive your enemies - it messes with their heads

                      My blog | My articles | MoXAML PowerToys | Onyx

                      J Offline
                      J Offline
                      Jason Henderson
                      wrote on last edited by
                      #13

                      I agree this is the proper way of communicating between 2 forms. I would also add that you could overload the constructor of the form to pass in a value. And, you only need to dispose a form if you use ShowDialog[^].

                      "Make everything as simple as possible, but not simpler." - Albert Einstein

                      Jason Henderson

                      1 Reply Last reply
                      0
                      • E Erdinc27

                        hey guys.. i have a problem..actually i saw many document about it and i applied on my project but i couldnt solve it..i want to transfer datas btw two forms..for example i open Form2 with a button in Form1 and i want to get the text of the textbox in Form2 as a text of textbox in Form1 to do that i wrote that codes below but still i have nothing here i opened Form2 private void btnRehber_Click(object sender, EventArgs e) { Form2 rhbForm = new Form2(); rhbForm.txtYetkili_isim.Text = txtYetkili_adi.Text; rhbForm.ShowDialog(); } and here i filled the textboxes in Form2 and submit the values to the textboxes in Form1 private void btnEkle_Click(object sender, EventArgs e) { frmFirma = new Form1(); frmFirma.txtGsm1.Text = txtGsm1.Text; frmFirma.txtGsm2.Text = txtGsm2.Text; frmFirma.txtNumara.Text = txtNumara.Text; } do i make something wrong ? or is there another way ? thanks guys for help

                        A Offline
                        A Offline
                        Ashutosh kumar Tripathi
                        wrote on last edited by
                        #14

                        hi... instead of using "txtGsm1.Text" better u declare a property in form2 . Then using that property u can do this. :)

                        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