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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Getting the number of listbox items in another form

Getting the number of listbox items in another form

Scheduled Pinned Locked Moved C#
helpquestion
2 Posts 2 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.
  • L Offline
    L Offline
    Lucy
    wrote on last edited by
    #1

    I have two forms. Form1 with a listbox (with 3 items) and a button. When this button is clicked form2 should open. On Form2 there is a tabcontrol with 4 tabpages. When Form2 loads I want it to show with just 3 tab pages. So on Form1 i have the following variable defined: public int countItems { get { return listBox1.Items.Count; } } On form2 I have made form1 public ( public Form1 frm1; ) and in the form2 load event i have the following: if (frm1.countItems == 3) { tabControl1.TabPages.Remove(tabPage4); } This complies with no errors but when the application is run and the button is clicked to bring up form2, the following error appeas for the line if (frm1.countItems == 3): Additional information: Object reference not set to an instance of an object. Can anyone tell me where I am going wrong? Lucy

    J 1 Reply Last reply
    0
    • L Lucy

      I have two forms. Form1 with a listbox (with 3 items) and a button. When this button is clicked form2 should open. On Form2 there is a tabcontrol with 4 tabpages. When Form2 loads I want it to show with just 3 tab pages. So on Form1 i have the following variable defined: public int countItems { get { return listBox1.Items.Count; } } On form2 I have made form1 public ( public Form1 frm1; ) and in the form2 load event i have the following: if (frm1.countItems == 3) { tabControl1.TabPages.Remove(tabPage4); } This complies with no errors but when the application is run and the button is clicked to bring up form2, the following error appeas for the line if (frm1.countItems == 3): Additional information: Object reference not set to an instance of an object. Can anyone tell me where I am going wrong? Lucy

      J Offline
      J Offline
      JoeSharp
      wrote on last edited by
      #2

      hi set the count-value in the form2 constructor e.g. Form2 form2 = new Form2(listBox1.Items.Count); form2.Show(); in the constructor of form2 remove your tabpage. regards

      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