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. LISTBOX How to select more than one entry at the startup

LISTBOX How to select more than one entry at the startup

Scheduled Pinned Locked Moved C#
questiongraphicstutorial
3 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.
  • M Offline
    M Offline
    Mmithat
    wrote on last edited by
    #1

    Hi All, I have list box in one of of my form: private System.Windows.Forms.ListBox listCampaign; (InitializeComponent()) function contains those initializations for that list box: this.listCampaign.Location = new System.Drawing.Point(80, 144); this.listCampaign.Name = "listCampaign"; this.listCampaign.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended; this.listCampaign.Size = new System.Drawing.Size(168, 69); this.listCampaign.TabIndex = 1; this.listCampaign.Visible = false; And then I add the items to List Box. listSkillSet.Items.Add("AB"); listSkillSet.Items.Add("AC"); listSkillSet.Items.Add("AD"); listSkillSet.Items.Add("AE"); QUESTION: Now I have 4 items in my list, I want to show 2 of em already selected.(at the beggining before user chooses - like telling him, we did choose for you already) I know how to choose one item (highlighted i mean) as in : listCampaign.SelectedIndex = 0; But How you select more then one ? (if I want to Choose (highlight) "AB" and "AC" at the same time Thanks, ~Mithat

    C 1 Reply Last reply
    0
    • M Mmithat

      Hi All, I have list box in one of of my form: private System.Windows.Forms.ListBox listCampaign; (InitializeComponent()) function contains those initializations for that list box: this.listCampaign.Location = new System.Drawing.Point(80, 144); this.listCampaign.Name = "listCampaign"; this.listCampaign.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended; this.listCampaign.Size = new System.Drawing.Size(168, 69); this.listCampaign.TabIndex = 1; this.listCampaign.Visible = false; And then I add the items to List Box. listSkillSet.Items.Add("AB"); listSkillSet.Items.Add("AC"); listSkillSet.Items.Add("AD"); listSkillSet.Items.Add("AE"); QUESTION: Now I have 4 items in my list, I want to show 2 of em already selected.(at the beggining before user chooses - like telling him, we did choose for you already) I know how to choose one item (highlighted i mean) as in : listCampaign.SelectedIndex = 0; But How you select more then one ? (if I want to Choose (highlight) "AB" and "AC" at the same time Thanks, ~Mithat

      C Offline
      C Offline
      Chris Jobson
      wrote on last edited by
      #2

      Assuming that you have set the SelectionMode to MultiSimple or MultiExtended, code similar to the following will work: //Unselect all items (not needed if you've only just created the ListBox) listBox1.SelectedIndex = -1; //Select second, fourth, ..., items (each time a new item is assigned to //.SelectedItem that item is added to the collection of selected items) for (int i = 1; i < listBox1.Items.Count; i += 2) listBox1.SelectedItem = listBox1.Items[i]; Chris Jobson

      M 1 Reply Last reply
      0
      • C Chris Jobson

        Assuming that you have set the SelectionMode to MultiSimple or MultiExtended, code similar to the following will work: //Unselect all items (not needed if you've only just created the ListBox) listBox1.SelectedIndex = -1; //Select second, fourth, ..., items (each time a new item is assigned to //.SelectedItem that item is added to the collection of selected items) for (int i = 1; i < listBox1.Items.Count; i += 2) listBox1.SelectedItem = listBox1.Items[i]; Chris Jobson

        M Offline
        M Offline
        Mmithat
        wrote on last edited by
        #3

        It worked just fine, Thank you very much =) ~Mithat

        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