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 transfer data between two form

How to transfer data between two form

Scheduled Pinned Locked Moved C#
tutorialquestion
18 Posts 9 Posters 2 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.
  • OriginalGriffO OriginalGriff

    Oh, dear, oh dear. You were doing so well, right up to the "You can store that data in a static variable in program". What happens if there are two instances of the form, with data changing? What happens if the data changes again? Static is generally a bad idea, particularly when you are passing the information to another class. Either keep it local to your class instance, and let the handler pick it up from the sender parameter if it is just the latest info they need, or also create a custom EventArgs based class that you hand the changed data though with when you signal the event.

    Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

    B Offline
    B Offline
    Bull City Rambler
    wrote on last edited by
    #5

    I agree and my original example was setting form1.foo = form2.foo. However, I decided on a field to show how you can store data between instantiations if you want. In this case, any member of Program has to be static since it's a static class. Regardless, your point is generally a good one.

    1 Reply Last reply
    0
    • P Promance

      How to transfer data between two form ? I found few articles but it is only transmitted in one direction instructions

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

      imho a question like this would be better posted in the Q/A secion, with taqs included: is this Windows Forms C# ? Second, by "transfer data" you could mean several things: automatic update (binding) one-way, or two-way, or: in response to a user action on one or both forms (like the user clicks a button). You need to specify if the data being passed from Form1 to Form2 is a "horse of different color" than the data being passed from Form2 and Form1: or, if they are the same exact structure, then you need to tell us you are synchronizing data between two forms. Another plausible scenario is that both Forms are being streamed data from some real-time source, or that a control on one Form is bound to a data-provider of some type, and you need to take action everytime that Form is updated on the other Form. The more specific the questions, the more detailed the scenario you take the time to describe: behold: the more focused answers you will get :) best, Bill

      "Humans are amphibians ... half spirit and half animal ... as spirits they belong to the eternal world, but as animals they inhabit time. This means that while their spirit can be directed to an eternal object, their bodies, passions, and imaginations are in continual change, for to be in time, means to change. Their nearest approach to constancy, therefore, is undulation: the repeated return to a level from which they repeatedly fall back, a series of troughs and peaks.” C.S. Lewis

      P 1 Reply Last reply
      0
      • B BillWoodruff

        imho a question like this would be better posted in the Q/A secion, with taqs included: is this Windows Forms C# ? Second, by "transfer data" you could mean several things: automatic update (binding) one-way, or two-way, or: in response to a user action on one or both forms (like the user clicks a button). You need to specify if the data being passed from Form1 to Form2 is a "horse of different color" than the data being passed from Form2 and Form1: or, if they are the same exact structure, then you need to tell us you are synchronizing data between two forms. Another plausible scenario is that both Forms are being streamed data from some real-time source, or that a control on one Form is bound to a data-provider of some type, and you need to take action everytime that Form is updated on the other Form. The more specific the questions, the more detailed the scenario you take the time to describe: behold: the more focused answers you will get :) best, Bill

        "Humans are amphibians ... half spirit and half animal ... as spirits they belong to the eternal world, but as animals they inhabit time. This means that while their spirit can be directed to an eternal object, their bodies, passions, and imaginations are in continual change, for to be in time, means to change. Their nearest approach to constancy, therefore, is undulation: the repeated return to a level from which they repeatedly fall back, a series of troughs and peaks.” C.S. Lewis

        P Offline
        P Offline
        Promance
        wrote on last edited by
        #7

        i want to transfer data between two form automatic two way, and response by click a button too. Sory because my English is not goog, i'm come from Viet Nam, i'm learning C# my self. This my first time post a Question. :)

        B 1 Reply Last reply
        0
        • S Sandeep Mewara

          Look here: Video: Transfer data between two forms in C#[^] Video: How To Transfer Data Between two Forms In C#.mp4[^] How to pass data between two forms in C#[^]

          Sandeep Mewara [My last tip/trick]: Browser back button issue after logout

          P Offline
          P Offline
          Promance
          wrote on last edited by
          #8

          thanks, but i want to transfer data from form2 to form1 by click a button.

          S D 2 Replies Last reply
          0
          • P Promance

            thanks, but i want to transfer data from form2 to form1 by click a button.

            S Offline
            S Offline
            Sandeep Mewara
            wrote on last edited by
            #9

            :doh: Did you try?

            //Form1:
            private string valueFromSecondForm;

            public string ValueFromSecondForm
            get { return value; }
            set { valueFromSecondForm = value; }

            private void button1_Click(object sender, EventArgs e)
            {
            Form2 frm = new Form2(this);
            frm.Show();
            }

            //Form2:
            public partial class Form2 : Form
            {
            Form opener;

            public Form2(Form parentForm)
            {
                InitializeComponent();
                opener = parentForm;
            }
            
            private void button1\_Click(object sender, EventArgs e)
            {
                opener.ValueFromSecondForm = valueFromForm2Textbox.Text;
            }
            

            }

            Other way could be to transfer the data at the time of closing the form2, here: Managing Data Among Multiple Forms (Part 1) [^]

            Sandeep Mewara [My last tip/trick]: Browser back button issue after logout

            P 1 Reply Last reply
            0
            • P Promance

              thanks, but i want to transfer data from form2 to form1 by click a button.

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #10

              Here you go! Pass value between forms using events[^]

              Dave
              Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

              S P 2 Replies Last reply
              0
              • D DaveyM69

                Here you go! Pass value between forms using events[^]

                Dave
                Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                S Offline
                S Offline
                Sandeep Mewara
                wrote on last edited by
                #11

                Cool! Nice link. 5!

                Sandeep Mewara [My last tip/trick]: Browser back button issue after logout

                1 Reply Last reply
                0
                • P Promance

                  How to transfer data between two form ? I found few articles but it is only transmitted in one direction instructions

                  A Offline
                  A Offline
                  Apocalypse Now
                  wrote on last edited by
                  #12

                  there is three solution: 1、pipe. 2、shared memory. 3、Message.

                  D 1 Reply Last reply
                  0
                  • S Sandeep Mewara

                    :doh: Did you try?

                    //Form1:
                    private string valueFromSecondForm;

                    public string ValueFromSecondForm
                    get { return value; }
                    set { valueFromSecondForm = value; }

                    private void button1_Click(object sender, EventArgs e)
                    {
                    Form2 frm = new Form2(this);
                    frm.Show();
                    }

                    //Form2:
                    public partial class Form2 : Form
                    {
                    Form opener;

                    public Form2(Form parentForm)
                    {
                        InitializeComponent();
                        opener = parentForm;
                    }
                    
                    private void button1\_Click(object sender, EventArgs e)
                    {
                        opener.ValueFromSecondForm = valueFromForm2Textbox.Text;
                    }
                    

                    }

                    Other way could be to transfer the data at the time of closing the form2, here: Managing Data Among Multiple Forms (Part 1) [^]

                    Sandeep Mewara [My last tip/trick]: Browser back button issue after logout

                    P Offline
                    P Offline
                    Promance
                    wrote on last edited by
                    #13

                    thanks

                    1 Reply Last reply
                    0
                    • D DaveyM69

                      Here you go! Pass value between forms using events[^]

                      Dave
                      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                      P Offline
                      P Offline
                      Promance
                      wrote on last edited by
                      #14

                      thanks

                      1 Reply Last reply
                      0
                      • A Apocalypse Now

                        there is three solution: 1、pipe. 2、shared memory. 3、Message.

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

                        You use anti-tank missles to kill mosquitoes don't you?

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

                        P 1 Reply Last reply
                        0
                        • D Dave Kreskowiak

                          You use anti-tank missles to kill mosquitoes don't you?

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

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

                          I would.

                          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                          1 Reply Last reply
                          0
                          • P Promance

                            i want to transfer data between two form automatic two way, and response by click a button too. Sory because my English is not goog, i'm come from Viet Nam, i'm learning C# my self. This my first time post a Question. :)

                            B Offline
                            B Offline
                            BillWoodruff
                            wrote on last edited by
                            #17

                            Promance wrote:

                            want to transfer data between two form automatic two way, and response by click a button too.

                            Hi Promance, Then it sounds to me like you want ... at all times ... for the exact same content to appear on both forms ... yes ? That's "synchronization." And, if you have synchronization working, then why would you also need to have a button to press to make sure the content was synchronized ? Please describe more clearly the nature of the data you are synchronizing here: 1. is it only text characters: example: you want a textbox on Form1 to always display the same text as a textbox on Form2, and the reverse. 2. or, is the data "more than that" ? does the data include content which you interpret and then construct controls based on your interpretation ? And, finally, it would be helpful to clearly describe the source of the data: 1. by direct action of a user while your application is running: like typing in a text box 2. by making calls to some external database and getting new data ... or does some external source notify your application there is new data needed to be used ? Give us a brief description of what's on Form1 and Form2?: what kind of controls ? Custom controls from 3rd. party companies, Excel used via VSTO, a UserControl you created yourself, standard Visual Studio toolbox controls ? best, Bill good luck, Bill

                            "Humans are amphibians ... half spirit and half animal ... as spirits they belong to the eternal world, but as animals they inhabit time. This means that while their spirit can be directed to an eternal object, their bodies, passions, and imaginations are in continual change, for to be in time, means to change. Their nearest approach to constancy, therefore, is undulation: the repeated return to a level from which they repeatedly fall back, a series of troughs and peaks.” C.S. Lewis

                            P 1 Reply Last reply
                            0
                            • B BillWoodruff

                              Promance wrote:

                              want to transfer data between two form automatic two way, and response by click a button too.

                              Hi Promance, Then it sounds to me like you want ... at all times ... for the exact same content to appear on both forms ... yes ? That's "synchronization." And, if you have synchronization working, then why would you also need to have a button to press to make sure the content was synchronized ? Please describe more clearly the nature of the data you are synchronizing here: 1. is it only text characters: example: you want a textbox on Form1 to always display the same text as a textbox on Form2, and the reverse. 2. or, is the data "more than that" ? does the data include content which you interpret and then construct controls based on your interpretation ? And, finally, it would be helpful to clearly describe the source of the data: 1. by direct action of a user while your application is running: like typing in a text box 2. by making calls to some external database and getting new data ... or does some external source notify your application there is new data needed to be used ? Give us a brief description of what's on Form1 and Form2?: what kind of controls ? Custom controls from 3rd. party companies, Excel used via VSTO, a UserControl you created yourself, standard Visual Studio toolbox controls ? best, Bill good luck, Bill

                              "Humans are amphibians ... half spirit and half animal ... as spirits they belong to the eternal world, but as animals they inhabit time. This means that while their spirit can be directed to an eternal object, their bodies, passions, and imaginations are in continual change, for to be in time, means to change. Their nearest approach to constancy, therefore, is undulation: the repeated return to a level from which they repeatedly fall back, a series of troughs and peaks.” C.S. Lewis

                              P Offline
                              P Offline
                              Promance
                              wrote on last edited by
                              #18

                              It don't need complicated as you say. First, i want a textbox on Form1 to always display the same text as a textbox on Form2 when i press a letter key. Second, i have a project about File Explore in Windows, when i want to rename a folder or a file(i have used ListView to show file and folder), i must be add a form named Rename. And i'm stuck here, i can rename file, but can't update file's name in ListView or TreeView.

                              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