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