I have a ListBox in my win forms app that will have generally fewer than 5 selections. The user can select 1 or more of these items; which i will use the selected items as part of an update query. I need 1 new record for each selection made. However, i am having extreme difficulty finding all of the selections the user has made.
I believe it to be related to the way i have used LINQ to fill the listbox with items. Here is how i fill it:
List<Accessor.SwitchPages> sites = Accessor.getSites(dataGridView1.Rows[i].Cells[3].Value.ToString()); listProjects.DataSource = sites; listProjects.SelectedIndex = -1; listProjects.ValueMember = "_projectid"; listProjects.DisplayMember = "_projectid";
Later after the user has made a selection and clicked a button will i loop through to find the selections and make the update. But, I am having difficulty getting the actual string value of the item selected.
for (int x = 0; x <= listProjects.SelectedItems.Count; x++)
{
if(listProjects.GetSelected(x) == true)
{
string d = listProjects.SelectedValue.ToString();
string s = listProjects.SelectedItems[x].ToString();
//listProjects.DisplayMember[x].ToString();
//listProjects.Items[x].ToString();
}
}
Inside the IF above, i am trying different ways to test the return of the string value of the selections made.
string d will return the first item(out of many) selected, every loop iteration; OR the ONLY item selected no matter it's position in the array.
string s will return the value: "ForcePassword.Accessor+SwitchPages"
Available example options in the listbox are:
STAR_CSI
STAR_LIC
STAR_CCC
Here is a shot of my locals window. Basically, I want to get to the _projectid value.

Questions:
1) How do i get string s to return the actual text value of the item selected?
2) Is there a better/preferred way to populate the listbox in order to call the selected items later?


Sign In
Create Account


Back to top









