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

Pass data between two child forms

Scheduled Pinned Locked Moved C#
questionhelp
5 Posts 3 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.
  • S Offline
    S Offline
    Saamir
    wrote on last edited by
    #1

    Hi guys! Please help, I am trying to pass data from form1 to form2 using a datagridview cell click event (passing data from datagridview1 in form1 to datagridview2 in form2) It is working fine if I open form1 and then form2, if I open form2 before form1 then it doesn't because it can't capture instance of form1 to access it. Here is what I am doing presently: in Parent (MDI), I have a public form instance for form1, I use that when I open form1 and then pass that instance to form2 when I open it. This is what I am doing: In Parent: public frmGrid1 frm1; .... //Form1 click event in parent frm1 = new frmGrid1(m_ADBCnn, 1, notWorkedToolStripMenuItem.Text); frm1.MdiParent = this; frm1.Show(); .... //Form2 click event in parent frm2 = new frmGrid2(frm1,m_ADBCnn,1,presentToolStripMenuItem.Text); frm2.Show(); frm2.MdiParent = this; I then assign frm1 in the constructor of form2 as follows: private frmGrid1 frm1; public frmGrid2(frmGrid1 f1) { try { InitializeComponent(); frm1 = f1; } catch (Exception ex) { MessageBox.Show("Failed on initialization: " + ex.Message); } } Again, this works if I open form1 before form2. How can I retrieve frm1 from frm2 if it is null? Please help

    Sameer

    M G 2 Replies Last reply
    0
    • S Saamir

      Hi guys! Please help, I am trying to pass data from form1 to form2 using a datagridview cell click event (passing data from datagridview1 in form1 to datagridview2 in form2) It is working fine if I open form1 and then form2, if I open form2 before form1 then it doesn't because it can't capture instance of form1 to access it. Here is what I am doing presently: in Parent (MDI), I have a public form instance for form1, I use that when I open form1 and then pass that instance to form2 when I open it. This is what I am doing: In Parent: public frmGrid1 frm1; .... //Form1 click event in parent frm1 = new frmGrid1(m_ADBCnn, 1, notWorkedToolStripMenuItem.Text); frm1.MdiParent = this; frm1.Show(); .... //Form2 click event in parent frm2 = new frmGrid2(frm1,m_ADBCnn,1,presentToolStripMenuItem.Text); frm2.Show(); frm2.MdiParent = this; I then assign frm1 in the constructor of form2 as follows: private frmGrid1 frm1; public frmGrid2(frmGrid1 f1) { try { InitializeComponent(); frm1 = f1; } catch (Exception ex) { MessageBox.Show("Failed on initialization: " + ex.Message); } } Again, this works if I open form1 before form2. How can I retrieve frm1 from frm2 if it is null? Please help

      Sameer

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      Best way to do this would be with events[^], each form will have an event that fires when the cell is clicked (or whatever) the parent form then listens for this event. When it is fired then the parent form can receive the data from one form (through event args or property) and then pass the data to the other form (through method or property)

      Life goes very fast. Tomorrow, today is already yesterday.

      S 2 Replies Last reply
      0
      • M musefan

        Best way to do this would be with events[^], each form will have an event that fires when the cell is clicked (or whatever) the parent form then listens for this event. When it is fired then the parent form can receive the data from one form (through event args or property) and then pass the data to the other form (through method or property)

        Life goes very fast. Tomorrow, today is already yesterday.

        S Offline
        S Offline
        Saamir
        wrote on last edited by
        #3

        Hi Musefan, Thank you for your prompt response. Pardon me but I have never used custom events, is there any way you can show me an example, I will appreciate it. Thanks

        Sameer

        1 Reply Last reply
        0
        • M musefan

          Best way to do this would be with events[^], each form will have an event that fires when the cell is clicked (or whatever) the parent form then listens for this event. When it is fired then the parent form can receive the data from one form (through event args or property) and then pass the data to the other form (through method or property)

          Life goes very fast. Tomorrow, today is already yesterday.

          S Offline
          S Offline
          Saamir
          wrote on last edited by
          #4

          Nevermind, I found a great example: Passing Values between Forms in .NET 1.x with C# and VB.NET examples[^] Thank you for the direction.

          Sameer

          1 Reply Last reply
          0
          • S Saamir

            Hi guys! Please help, I am trying to pass data from form1 to form2 using a datagridview cell click event (passing data from datagridview1 in form1 to datagridview2 in form2) It is working fine if I open form1 and then form2, if I open form2 before form1 then it doesn't because it can't capture instance of form1 to access it. Here is what I am doing presently: in Parent (MDI), I have a public form instance for form1, I use that when I open form1 and then pass that instance to form2 when I open it. This is what I am doing: In Parent: public frmGrid1 frm1; .... //Form1 click event in parent frm1 = new frmGrid1(m_ADBCnn, 1, notWorkedToolStripMenuItem.Text); frm1.MdiParent = this; frm1.Show(); .... //Form2 click event in parent frm2 = new frmGrid2(frm1,m_ADBCnn,1,presentToolStripMenuItem.Text); frm2.Show(); frm2.MdiParent = this; I then assign frm1 in the constructor of form2 as follows: private frmGrid1 frm1; public frmGrid2(frmGrid1 f1) { try { InitializeComponent(); frm1 = f1; } catch (Exception ex) { MessageBox.Show("Failed on initialization: " + ex.Message); } } Again, this works if I open form1 before form2. How can I retrieve frm1 from frm2 if it is null? Please help

            Sameer

            G Offline
            G Offline
            Giorgi Dalakishvili
            wrote on last edited by
            #5

            Here you are: Passing Values between Forms in .NET 1.x with C# and VB.NET examples[^]

            Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

            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