ListBox: set SelectedItems

G

G-Fit

Hello group,

I have a ListBox control, populated by a DataSet with a DisplayMember and
a ValueMember.
I have a list of values (a string[] in my case), and I'd like the ListBox
control to show all items whose value it can find in this list, as selected.
It seems I can't find the way to do this, because I don't know how to set
the state of one item to 'selected'. Here is what I got so far:

foreach (object o in listBox1.Items)
{
DataRowView view = (DataRowView) o;
foreach (string s in myValues)
{
if (view.Row["value"].ToString() == s)
?????
}
}


Thanks
Karine Proot
 
A

Andy Becker

G-Fit said:
Hello group,

I have a ListBox control, populated by a DataSet with a DisplayMember and
a ValueMember.
I have a list of values (a string[] in my case), and I'd like the ListBox
control to show all items whose value it can find in this list, as selected.
It seems I can't find the way to do this, because I don't know how to set
the state of one item to 'selected'. Here is what I got so far:

foreach (object o in listBox1.Items)
{
DataRowView view = (DataRowView) o;
foreach (string s in myValues)
{
if (view.Row["value"].ToString() == s)
?????
}
}


Thanks
Karine Proot

I have found that I can set SelectedIndex multiple times with the
SelectionMode to some multi-select variation... So you can probably use
something like this:

listBox1.SelectedIndex = listBox1.Items.IndexOf(o);

Best Regards,

Andy
 
J

Jeffrey Tan[MSFT]

Hi Karine,

Based on my understanding, you want to select multi-items in the ListBox
programmatically.
====================================================
Just as Andy said, you may use ListBox.SelectedIndex or SelectedItem to
select item, then you may set
ListBox.SelectionMode=SelectionMode.MultiSimple to enable the multi-item
selection, like this:
private void button1_Click(object sender, System.EventArgs e)
{
this.listBox1.SelectionMode=SelectionMode.MultiSimple;
for(int i=0;i<this.listBox1.Items.Count;i++)
{
if(this.listBox1.Items.ToString().IndexOf("b")!=-1)
{
this.listBox1.SelectedIndex=i;
}
}
}

This code snippet selects all the items contains "b".

========================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

G-Fit

"Jeffrey Tan[MSFT]" said:
Hi Karine,

Based on my understanding, you want to select multi-items in the ListBox
programmatically. Correct.

====================================================
Just as Andy said, you may use ListBox.SelectedIndex or SelectedItem to
select item, then you may set
ListBox.SelectionMode=SelectionMode.MultiSimple to enable the multi-item
selection, like this:
Ah, I didn't think this would work. My bad for not trying - thought
SelectedIndex would de-select everything else.
private void button1_Click(object sender, System.EventArgs e)
{
this.listBox1.SelectionMode=SelectionMode.MultiSimple;
for(int i=0;i<this.listBox1.Items.Count;i++)
{
if(this.listBox1.Items.ToString().IndexOf("b")!=-1)
{
this.listBox1.SelectedIndex=i;
}
}
}

This code snippet selects all the items contains "b".

========================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Well, this is surely a good start point. I managed to select the first item
in my list of items-to-select, but the others won't... though I see in debug
mode that the code does go through each "this.listBox1.SelectedIndex=i;",
and though I enabled MultiSelection.

I'll make more attempts and will let you know if I am still stuck on monday!

Thanks for your help.
Karine Proot
 
G

G-Fit

"Jeffrey Tan[MSFT]" said:
Hi Karine,

Thanks for your feedback.

I think you may create a new Winform project, then paste and use my code
snippet. I think this will enable solution work for you. Also, please note
that my code only selects all the items that contains "b", so if there is
only one item contains "b" in your listbox, only that one item will be
selected.

I will also attach my sample project in this reply, for your information.
Thanks for the example, which helped me tracking where the problem was
standing (the only difference between your sample and mine): my listbox is
on a separate form, and the items should be selected as soon as that form
shows up. Through a lot of debugging, I saw that I correctly select all the
items I want, and then only the first is showed as selected after the call
to myForm.ShowDialog()

So far, the code to select those items is in the myForm constructor. I
wonder if I can put it elsewhere so it will happen later, and I can see all
the selected items when the form is shown.

I can provide a sample project if you wish.

Thanks for your help,
Karine Proot
 
J

Jeffrey Tan

Hi Karine,

Thanks for your feedback.

I have tried to place the ListBox in another seperate form and show this
Form through ShowDialog, also, I place the selection code in this seperate
form's constructor, all works well.

If the problem still existed, please paste some code snippet for me(or a
sample project) to reproduce this problem. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi G-Fit,

Is your problem resolved? Please feel free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top