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. add listBox selected items to a string array

add listBox selected items to a string array

Scheduled Pinned Locked Moved C#
data-structuresquestion
3 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.
  • P Offline
    P Offline
    pcaeiro
    wrote on last edited by
    #1

    I need to put some listbox selected items in to a string array. My code: string [] deleted_dictionarys; for (int i = 0; i < this.listBox_Delete1.SelectedItems.Count; i++) { this.deleted_dictionarys[i] = this.listBox_Delete1.SelectedItems[i].ToString(); } I don't have sintax errors but i'm getting a NullReferenceException. How can i resolve it. :doh:

    P X 2 Replies Last reply
    0
    • P pcaeiro

      I need to put some listbox selected items in to a string array. My code: string [] deleted_dictionarys; for (int i = 0; i < this.listBox_Delete1.SelectedItems.Count; i++) { this.deleted_dictionarys[i] = this.listBox_Delete1.SelectedItems[i].ToString(); } I don't have sintax errors but i'm getting a NullReferenceException. How can i resolve it. :doh:

      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #2

      pcaeiro wrote:

      string [] deleted_dictionarys;

      You declare your array but don't instantiate it. You need to do something like this:

      string[] deleted_dictionarys = new string[numberofitems];

      Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

      1 Reply Last reply
      0
      • P pcaeiro

        I need to put some listbox selected items in to a string array. My code: string [] deleted_dictionarys; for (int i = 0; i < this.listBox_Delete1.SelectedItems.Count; i++) { this.deleted_dictionarys[i] = this.listBox_Delete1.SelectedItems[i].ToString(); } I don't have sintax errors but i'm getting a NullReferenceException. How can i resolve it. :doh:

        X Offline
        X Offline
        Xmen Real
        wrote on last edited by
        #3

        here is easy and short way

        string[] deleted_dictionarys = new string[listBox_Delete1.SelectedItems.Count];
        listBox_Delete1.SelectedItems.CopyTo(deleted_dictionarys, 0);

        Becoming Programmer...

        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