Click here to Skip to main content
15,894,539 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually, I did this in web forms with dropdownlist.

Here is the code:
ListItem l1 = new ListItem("-Select-","0");
            ddlUName.Items.Add(l1);
            ddlUName.SelectedValue = "0";

Now I'm trying to do the same thing in Windows forms using combobox, but here the ListItem class is not there. So can anybody help me? How can I use the above code in windows forms?

Thank you.
Posted
Updated 12-Mar-13 8:56am
v2

The analogous list type is System.Windows.Forms.ListBox. A much more advanced control is System.Windows.Forms.ListView:
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.aspx[^].

First, class does not need list items. Instead, you can use object of any type. This is very important, because you can store any data in the item. Many beginners don't understand it and tend to store strings representing data instead the data itself, and then suffer extracting data. You really should store actual data in Items. The only question is: what string is shown in the UI? The answer is simple: whatever the virtual method ToString returns. So, you can control this text by overriding System.Object.ToString() of your item type, which can be any structure or class. Of course, you can store just strings, in some simple cases.

The second class does have the item type, ListViewItem, but also a concept of sub-item, ListViewItem.SubItems. If you need it, see the code sample on the page referenced above.

—SA
 
Share this answer
 
Comments
Rajeev Jayaram 12-Mar-13 16:35pm    
5'ed
Sergey Alexandrovich Kryukov 12-Mar-13 16:37pm    
Thank you, Rajeev.
—SA
Espen Harlinn 12-Mar-13 17:55pm    
5'ed!
Sergey Alexandrovich Kryukov 12-Mar-13 18:01pm    
Thank you, Espen.
—SA
abhijit hbk 13-Mar-13 0:08am    
AM AN BEGINNER_ I DON'T KNOW MUCH ABOUT_

ListItem l1 = new ListItem("-Select-","0");
ddlUName.Items.Add(l1);
ddlUName.SelectedValue = "0";

SO PLZZ CAN U GIVE CODE WHICH DOES SAME WORK AS THE CODE MENTIONED ABOVE, BUT HERE M USING COMBObOX INSTEAD DROPDOWNLIST

THANK UUUU
use ListViewItem [^] for WindowsForms. use SelectedIndices[^] property to iterate through selected items
 
Share this answer
 

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