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. ParentForm and a null reference

ParentForm and a null reference

Scheduled Pinned Locked Moved C#
questioncsharphelptutorial
4 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.
  • A Offline
    A Offline
    Andrew McLellan
    wrote on last edited by
    #1

    I'm trying to load some data from a parent form, actually the main Form, of my app into a dialog box using the following: Config config; config = ((Form1)this.ParentForm).config; the ParentForm though is always 'undefined' and a NullReferenceException occurs with the followiing information: 'Object reference not set to an instance of an object' I'm confused, as I followed the code example in the C# help "Retrieving Information from the Parent Form of a Dialog Box". What am I doing wrong, how do I manage this?

    B 1 Reply Last reply
    0
    • A Andrew McLellan

      I'm trying to load some data from a parent form, actually the main Form, of my app into a dialog box using the following: Config config; config = ((Form1)this.ParentForm).config; the ParentForm though is always 'undefined' and a NullReferenceException occurs with the followiing information: 'Object reference not set to an instance of an object' I'm confused, as I followed the code example in the C# help "Retrieving Information from the Parent Form of a Dialog Box". What am I doing wrong, how do I manage this?

      B Offline
      B Offline
      Broken God
      wrote on last edited by
      #2

      When are you trying to do this? In the constructor? ParentForm has the default value (null) when in the constructor (just like any other variable).

      A 1 Reply Last reply
      0
      • B Broken God

        When are you trying to do this? In the constructor? ParentForm has the default value (null) when in the constructor (just like any other variable).

        A Offline
        A Offline
        AWoodgate
        wrote on last edited by
        #3

        I'm not doing any of this in the constructor. A simple case is: I have 2 forms Form1 has a public string, myString, initialised with "ABCDEF". It also has a button that when clicked loads Form2: private void button1_Click(object sender, System.EventArgs e) { Form2 frm = new Form2(); frm.Show(); } Form2 has a label on it, in the label's 'Click' event I try to set it's Text with the following: void label1_Click(object sender, System.EventArgs e) { label1.Text = ((Form1)this.ParentForm).myString; } This causes the NullReferenceException and when I highlight 'this.ParentForm', it shows as Why?

        B 1 Reply Last reply
        0
        • A AWoodgate

          I'm not doing any of this in the constructor. A simple case is: I have 2 forms Form1 has a public string, myString, initialised with "ABCDEF". It also has a button that when clicked loads Form2: private void button1_Click(object sender, System.EventArgs e) { Form2 frm = new Form2(); frm.Show(); } Form2 has a label on it, in the label's 'Click' event I try to set it's Text with the following: void label1_Click(object sender, System.EventArgs e) { label1.Text = ((Form1)this.ParentForm).myString; } This causes the NullReferenceException and when I highlight 'this.ParentForm', it shows as Why?

          B Offline
          B Offline
          Broken God
          wrote on last edited by
          #4

          Ah, I see. The problem is that when you use Form.Show(), it creates a global form, not a child form. You either need to set the Parent property manually (which will also put the second form "into" the first form visually) or create a modal form using Form.ShowDialog().

          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