Get the Value for a Listbox Item by Index

How to get the ValueItem value of ListBox item by its index

This function will allow you to find the value of what is selected in a ListBox. You can use it to get either the "hidden" value of a selected item or the displayed value of an item in the ListBox.

Update: I adjusted the function so it works based on the index value, as per the original question

Public Enum SelectByMode As Integer
ByDisplayName = 1
ByReturnValue = 2
End Enum

''' <summary>
''' Use this function to get the values of an item in the list box. It works with list boxes that are bound to a DataSet, and those that have been populated via code, or manually populated via the GUI.
''' </summary>
''' <param name="lstBox">The list box you want to find the selected value in.</param>
''' <param name="iIndex">The number of the item in the ListBox that you want the value for.</param>
''' <param name="DesiredReturnValue">By default it returns the hidden return value, but you can ask for the display value if desired.</param>
''' <returns>This function returns a String value.</returns>
Public Function WhatValueIsSelectedInListBoxForIndex(ByVal lstBox As ListBox, ByVal iIndex As Integer, Optional ByVal DesiredReturnValue As SelectByMode = SelectByMode.ByReturnValue) As String
Dim sReturn As String = ""

Try
If DesiredReturnValue = SelectByMode.ByReturnValue Then
'Returns the value that is not visible, but stored in the "ValueMember" field of the bound ComboBox
If lstBox.ValueMember.Length > 0 And lstBox.DisplayMember.Length > 0 Then
'This is a bound listbox.
Dim drSelectedItem As DataRowView = lstBox.Items(iIndex)
sReturn = drSelectedItem.Item(0).ToString
Else
Try
'This listbox was populated in code with display values and return values.
sReturn = lstBox.Items(iIndex).ReturnValue
Catch Exp As Exception
'
End Try
End If
ElseIf DesiredReturnValue = SelectByMode.ByDisplayName Then
'Return the selected TEXT (visible in the control)
If lstBox.ValueMember.Length > 0 And lstBox.DisplayMember.Length > 0 Then
'This is a bound listbox.
Dim drSelectedItem As DataRowView = lstBox.Items(iIndex)
sReturn = drSelectedItem.Item(1).ToString
Else
sReturn = lstBox.Items(iIndex).ToString
End If
Else
sReturn = ""
End If
Catch Exp As Exception
'You do whatever you want to do here when an error occurs.
End Try

If sReturn Is Nothing Then sReturn = ""
Return sReturn
End Function

ListBox get value from index

If you add the items to the list box as such:

listBox1.Items.Add(textBox1.Text);

Then you can retrieve the item from given index as follows:

string x = listBox1.Items[index];

The indexer is returning the value, which in that case is a string. Probably you might need to cast it to string, because the indexer actually returns object - see here: ListBox.ObjectCollection.Item Property :

string x = (string)listBox1.Items[index];

How do you get the text from a listbox by index?

What do you have in your Listbox?

If there are string values in the listbox, your code is correct except for missing braces:

string value = listBox.Items[index].ToString();

If the things in the listbox are some sort of object, you may need to override ToString() to get the desired result, or cast the thing you get out of the listbox to the desired type and then access an appropriate property.

Example:

MyClass my = (MyClass)listBox.Items[index];
string value = my.SomePropertyOfMyClass;

Get Listbox item text by index value

Is this what you are trying to do?

string text = lboxSymbol.SelectedItem.ToString();

This will output the text of the selected listbox item when it is used.
For example:

private void Button_Click(object sender, RoutedEventArgs e)
{
string text = lboxSymbol.SelectedItem.ToString();
Console.WriteLine(text);
}

c#- listbox how to get listbox values by index

I must admit that i'm not familiar with WPF controls but MSDN shows it:

ListBoxItem lbi = listBox1.Items[0] as ListBoxItem; // cast object to ListBoxItem
txtbox1.Text = lbi.Content.ToString();

So you have to use the Content property of the ListBoxItem. You are using ToString which just returns the fully qualified name of the type if ToString was not overridden meaningfully.

If the ListBox is empty, you get an exception if you try to access the first item via index, the you have to check it first:

if(listBox1.HasItems)  // you can also use listBox1.Items.Count > 0
{
ListBoxItem lbi = listBox1.Items[0];
// ...
}

Get the selected index and selected value of a ListBox

This is because of clearing the items each time the page makes request

ListBox1.Items.Clear();

you can remove this line of code and adding if(!IsPostBack) so that data loads only for the first time when page loads

if(!IsPostBack)
{
Label1.Text = Session["uid"].ToString();
Label2.Text = Session["crs_id"].ToString();
SqlDataReader r;
SqlCommand cmd = new SqlCommand("select lecture_text, lecture_Title,lecture_No from Lecture where course_ID='" + Convert.ToInt32(Session["crs_id"]) + "'", con);
con.Open();
ListBox1.DataSource = cmd.ExecuteReader();
ListBox1.DataTextField = "lecture_Title";
ListBox1.DataValueField = "lecture_No";
ListBox1.DataBind();
con.Close();
ListBox1.Items.Insert(0, new ListItem("--Select Customer--", "0"));
}

how can i get the value of index in listbox?

You can store the selected item in a variable like this

object selected = listBox1.Items[index];

Is that what you're asking for?



Related Topics



Leave a reply



Submit