If there are string values in the listbox, your code is correct except for missing braces:
string value = listBox.Items[index].ToString();
Example:
MyClass my = (MyClass) listBox.Items[index]; string value = my.SomePropertyOfMyClass;
To get the item from a ListBox's items by index please use this way
string item = listBox1.Items[0];
Gets or sets the currently selected item in the ListBox.,To retrieve a collection containing all selected items in a multiple-selection ListBox, use the SelectedItems property. If you want to obtain the index position of the currently selected item in the ListBox, use the SelectedIndex property. In addition, you can use the SelectedIndices property to obtain all the selected indexes in a multiple-selection ListBox.,For a standard ListBox, you can use this property to determine which item is selected in the ListBox. If the SelectionMode property of the ListBox is set to either SelectionMode.MultiSimple or SelectionMode.MultiExtended (which indicates a multiple-selection ListBox) and multiple items are selected in the list, this property can return any selected item.,An object that represents the current selection in the control.
public:
property System::Object ^ SelectedItem {
System::Object ^ get();
void set(System::Object ^ value);
};
public:
property System::Object ^ SelectedItem { System::Object ^ get(); void set(System::Object ^ value); };
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.Browsable(false)]
public object SelectedItem {
get;
set;
}
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.Browsable(false)]
public object SelectedItem { get; set; }
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.Browsable(false)]
public object ? SelectedItem {
get;
set;
}
[<System.ComponentModel.Bindable(true)>]
[<System.ComponentModel.Browsable(false)>]
member this.SelectedItem : obj with get, set
Public Property SelectedItem As Object
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) {
// Get the currently selected item in the ListBox.
string curItem = listBox1.SelectedItem.ToString();
// Find the string in ListBox2.
int index = listBox2.FindString(curItem);
// If the item was not found in ListBox 2 display a message box, otherwise select it in ListBox2.
if (index == -1)
MessageBox.Show("Item is not available in ListBox2");
else
listBox2.SetSelected(index, true);
}
ListBox1.Items.Add(new ListItem("asdf","40");,ListBox1.Items.Add(new ListItem("asdf","40"));,hi cgilbu, ListBox.Items.Add(New ListItem("text","value")),Thanks! But how can I get (echo out) the "value"? Right now i get the "text" by this code: PersonID.Append(li.Value).
ListBox1.Items.Add(new ListItem("asdf");
If you would like to post, please check out the MrExcel Message Board FAQ and register here. If you forgot your password, you can reset your password. , MrExcel Publishing MrExcel Homepage MrExcel Bookstore MrExcel Seminars Excel Consulting Services ,Do not share my Personal Information
For i = 0 To lstBoxSuppliers.ListCount - 1 ws.Cells(r, 1) = CStr(lstBoxSuppliers.Items.Item(i)) r = r + 1 Next i
With lstBoxSuppliers For i = 0 To.ListCount - 1 If.Selected(i) = True Then ws.Cells(r, 1) = CStr(.List(i)) r = r + 1 End If Next i End With
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL), communitylounge Who's Who Most Valuable Professionals The Lounge The CodeProject Blog Where I Am: Member Photos The Insider News The Weird & The Wonderful , help? What is 'CodeProject'? General FAQ Ask a Question Bugs and Suggestions Article Help Forum About Us ,Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
<ListBox x:Name="listWithImg" Height="200" Width="200" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="250 20 0 0" SelectionChanged="listWithImg_SelectionChanged">
<ListBoxItem Height="50">
<StackPanel Orientation="Horizontal">
<Image Height="25" Width="25" Source="Image/C.png" />
<TextBlock Text="C Programming" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10 0 0 0" />
</StackPanel>
</ListBoxItem>
<ListBoxItem Height="50">
<StackPanel Orientation="Horizontal">
<Image Height="25" Width="25" Source="Image/CPP.png" />
<TextBlock Text="C Plus Plus Programming" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10 0 0 0" />
</StackPanel>
</ListBoxItem>
<ListBoxItem Height="50">
<StackPanel Orientation="Horizontal">
<Image Height="25" Width="25" Source="Image/CSharp.png" />
<TextBlock Text="C Sharp Programming" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10 0 0 0" />
</StackPanel>
</ListBoxItem>
<ListBoxItem Height="50">
<StackPanel Orientation="Horizontal">
<Image Height="25" Width="25" Source="Image/Raspberry.png" />
<TextBlock Text="Raspberry" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10 0 0 0" />
</StackPanel>
</ListBoxItem>
</ListBox>
<TextBlock x:Name="txtblkSelectedItem" Text="Select To CHange" Height="30" Width="200" VerticalAlignment="Top" HorizontalAlignment="Center" Margin="100 240 0 0" Padding="5" />
private void listWithImg_SelectionChanged(object sender, SelectionChangedEventArgs e) {
txtblkSelectedItem.Text = name;
}
txtblkSelectedItem.Text = ((((sender as ListBox).SelectedValue as ListBoxItem).Content as StackPanel).Children[1] as TextBlock).Text;