Getting Multiple selection in ListBox

A

aroraamit81

I have a string like "English,Japanese,Spanish" and a list box which
contains say 10 lang.
Now I want my List box should come by selected values as the values in
string and rest of the values to be unselected.

But what I am getting is the last value selected (Spanish in this
case).

Any help.......


Regards,
Amit
 
S

S. Justin Gengo [MCP]

Amit,

Loop through each item and check if each one is selected.

For Each ListBoxItem As ListItem In MyListBox.Items

If ListBoxItem.Selected Then

'---This item is selected do something with it

End If

Next


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
A

aroraamit81

char[] splitter = {','};
string
[]lang=ds.Tables[0].Rows[0]["USER_LANG_ID"].ToString().Split(splitter);

foreach(string i in lang)
{
for(int j=0;j<lstLang.Rows;j++)
{
if(lstLang.Items[j].Text==i)
{
lstLang.SelectedIndex=j;
Response.Write(i);
}
}
}

This is the code I am using... but it does not work....

Pls see.....
 
S

S. Justin Gengo [MCP]

To start with try simplifying the code.

You don't even need to compare the entries with the dataset because the
listbox already has the entiries in it:

for (int j = 0; j < myListBox.Rows; j++)

{

if(myListBox.Items[j].Selected)

{

Response.Write(myListBox.Items[j].Text;

}

}



--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
A

aroraamit81

I guess its me who is unable to make u understand.....

Well the databse field tells u the language list (Comma Seprated) which
needs to be selected. So I have to get the values from the databse
itself.....
 
S

S. Justin Gengo [MCP]

Ok, you're trying to select each item in the list based on the dataset. Now
I've got you.

Very similar. First you will certainly have a problem if the text doesn't
match exactly. Strings matching is case sensitive remember. So make certain
that your data and the entries in the list box are exactly the same. Or use
a .ToLower to make certain.

Next your real problem is that you're using SelectedIndex. That keeps
changing the selected index to a single item. It automatically deselects any
other. You have to set each item to selected seperately like this:

foreach(string i in lang)
{
for(int j=0;j<lstLang.Rows;j++)
{
if(lstLang.Items[j].Text==i)
{
lstLang.Items[j].Selected = True;
}
}
}

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
A

aroraamit81

No NO No No No,
IT HAS WORKED...........................................

Thnx a lot Mate.........................................................
 
S

S. Justin Gengo [MCP]

Oh, good!

I was just about to email you a working example based on the previous code.


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top