Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
hi friends,
I want to get value from combobox when I pass its index value. for ex:
suppose the following are the value in combobox

Jan
Feb
mar
apr
may

I want if I pass "2" as index value and want "mar" as result how can i do it please help me soon
Posted
Updated 31-Oct-18 0:17am

ComboBox has has a property named Items that is a collection and you can access to each item with using Index.

For example:
C#
MessageBox.Show(MyCombo.Items[2].ToString());



You can use this method too:

C#
public object GetComboBoxItem(int index)
{
    if (index >= MyCombo.Items.Count)
        return null;

    return MyCombo.Items[index];
}
 
Share this answer
 
v3
Comments
ProEnggSoft 30-Mar-12 7:08am    
+5
Shahin Khorshidnia 30-Mar-12 7:46am    
Thank you very much ProEnggSoft
ShivKrSingh 12-Apr-12 9:03am    
thanks
Shahin Khorshidnia 12-Apr-12 10:13am    
You're welcome
shady mohamed 5-Apr-21 11:06am    
Perfect Code
Hi all,

Its very simple u can get the text of combobox for specific index in one line


myCombobox.GetItemText(myCombobox.Items[index]);



thanks
:)
 
Share this answer
 
Comments
Member 10832926 26-Jun-15 2:14am    
Thank you :)
Libino Giffrin 17-Jul-15 12:35pm    
Help : I'm beginner in c#.
I've ComboBox.DisplayMember="Mar"; and ComboBox.ValueMember="50"; Bt the index value is same as "2";
Now my question is ' I want to get the value "50"? How to get? Plz help...
moutal 28-Sep-16 5:47am    
hello there ,
did u solve this issue ??
i have the same problem and i'm stuck on it .. i cant find any solution for it !
Libino Giffrin 5-Apr-17 7:00am    
S, I got answer
Member 13559666 14-Feb-18 5:03am    
Thank You Bro
You can simply select it using SelectedIndex. Check out the link for more info: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindex.aspx[^]

Good luck!
 
Share this answer
 
Dim index As Integer

1. CmbName.Items(Index).ToString().Trim()

2. index = CmbName.FindString("MonthName")
CmbName.SelectedIndex = index
CmbName.SelectedItem.ToString()
CmbName.SelectedText(Index).ToString().Trim()
CmbName.SelectedValue.ToString()
 
Share this answer
 
Comments
Shahin Khorshidnia 30-Mar-12 7:59am    
1. It doesn't work. It's actually 100% opposite of what OP wants! OP wants to pass an index and get a month name. Your solution 1.trims values, 2.passes the month name and gets the index!

=====
2. CmbName(?!) What kind of notation? PascalCasing (No!), camelCasing (No!), Hungarian (No!).
Manoj Kumar Choubey 30-Mar-12 8:05am    
Have you check cmbName.Items(Index).Tostring()
Manoj Kumar Choubey 30-Mar-12 8:06am    
and Please suggest me what are you using naming convention .... ?
Shahin Khorshidnia 30-Mar-12 8:18am    
Hello Manoj
Sorry about it. But:
CmbName (If I suppose it's cmbName) is in Hungarian and it's not standard in .Net. It's a back to VB6.
We can use camelCasing (For fields and variables) and PascalCasing (For properties and name of contols)
for example:

Dim indexOfItems As Integer 'Its camelCasing
===

'PascalCasing for this Public Property
Private _personID As Integer

Public Property PersonID() As Integer
Get
Return _personID
End Get
Set
_personID = value
End Set
End Property
.....
You can find standard notation in MSDN
ProEnggSoft 30-Mar-12 10:59am    
You are correct. Please see my comment below, where I have mentioned other possible cases.
If you want to get item text from a combobox by index number in vb.net
you should do
(correct ways) :> MyCombo.GetItemText(MyCombo.Items.Item(0))

you shouldn't
(X Incorrect way) :> MyCombo.Items(0).ToString

because if combobox filled from .DataSource then it won't work, it gives object string referring that item which is not wanted
 
Share this answer
 
Comments
Richard Deeming 2-Nov-18 12:31pm    
As already mentioned in solution #4, back in 2012.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900