Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# VB.NET
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 30-Mar-12 0:08am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

ComboBox has has a property named Items that is a collection and you can access to each item with using Index.
 
For example:
MessageBox.Show(MyCombo.Items[2].ToString());
 

You can use this method too:
 
public object GetComboBoxItem(int index)
{
    if (index >= MyCombo.Items.Count)
        return null;
 
    return MyCombo.Items[index];
}
  Permalink  
v3
Comments
ProEnggSoft at 30-Mar-12 7:08am
   
+5
Shahin Khorshidnia at 30-Mar-12 7:46am
   
Thank you very much ProEnggSoft
shivkumar1984 at 12-Apr-12 9:03am
   
thanks
Shahin Khorshidnia at 12-Apr-12 10:13am
   
You're welcome
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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()
  Permalink  
Comments
Shahin Khorshidnia at 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 at 30-Mar-12 8:05am
   
Have you check cmbName.Items(Index).Tostring()
manoj kumar choubey at 30-Mar-12 8:06am
   
and Please suggest me what are you using naming convention .... ?
Shahin Khorshidnia at 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 at 30-Mar-12 10:59am
   
You are correct. Please see my comment below, where I have mentioned other possible cases.
Shahin Khorshidnia at 30-Mar-12 12:36pm
   
I saw. It's Perfect.
Thanks
ProEnggSoft at 30-Mar-12 13:21pm
   
Thank you.
manoj kumar choubey at 30-Mar-12 8:07am
   
and Also describe what is wrong with above answer and what is the correct answer ..... ?
Shahin Khorshidnia at 30-Mar-12 8:20am
   
And What's wrong about the answer:
As I said:
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!
Check my solution, It returns the value of ComboBox by passing the Index
manoj kumar choubey at 30-Mar-12 8:37am
   
Shahin Thanks for improving my skill , Actually I started programing with vb 6 and now i am doing programing with .net , and this is my habit, naming same type , I will keep it in mind, and also want to say I have viewed your articles this is pretty good , put more articles to share your knowledge, don't mind but I want to ask a question ?
ProEnggSoft at 30-Mar-12 10:54am
   
What Shahin said is correct.
I want to add the following to make it more clear.
 
As per Framework Design Guidelines
 
PascalCasing is used for static public and protected fields.
Internal and private fields are not covered by those guide lines. But if used camelCasing can be used.
Public and protected instance fields are not allowed by the member design guidelines.
i.e. We cannot generilize camelCasing for all fields.
 
Fields are also variables. So to be specific
camelCasing is used for parameters and local variables.
 
Regarding control, it depends on the access (visibility) level of the control.
In Visual Studio, in form designer (C#), when a control is dropped on a form, the control is given private access and the naming is camelCasing.
When the control is given public or protected access, then PascalCasing can be used.
Shahin Khorshidnia at 30-Mar-12 12:36pm
   
Thanks Manjo
Not at all. Please ask ;)
manoj kumar choubey at 30-Mar-12 12:36pm
   
Thanks ProEnggSoft .....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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

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

 
thanks
Smile | :)
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 10 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100