The mechanism of the Web Control dropdownlist is different from that of the winform control ComboBox.
ComboBox does not have a corresponding listitem. You need to write one yourself:
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Namespace Winlistitem
{
/// <Summary>
/// Select an item class for adding items in ComboBox or ListBox
/// </Summary>
Public Class Listitem
{
Private String ID = String . Empty;
Private String Name = String . Empty;
Public Listitem ( String Sid, String Sname)
{
ID = Sid;
Name = Sname;
}
Public Override String Tostring ()
{
Return This . Name;
}
Public String ID
{
Get
{
Return This . ID;
}
Set
{
This . ID = Value;
}
}
Public String Name
{
Get
{
Return This . Name;
}
Set
{
This . Name = Value;
}
}
}
}
You can add items like dropdownlist:
Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
Using System. LINQ;
Using System. text;
Using System. Windows. forms;
NamespaceWinlistitem
{
Public Partial ClassMainfrm: Form
{
PublicMainfrm ()
{
Initializecomponent ();
}
Private Void Btnok_click ( Object Sender, eventargs E)
{
Listitem = Combobox1.selecteditem As Listitem;
MessageBox. Show (listitem. ID + " , " + Listitem. Name );
}
Private Void Mainfrm_load ( Object Sender, eventargs E)
{
// Add items. The dropdownlist of the web control has the corresponding listitem.
Listitem listitem0 = New Listitem ( " 0 " , " Option 0 " );
Listitem listitem1 = New Listitem ( " 1 " , " Option 1 " );
Listitem listitem2 = New Listitem ( " 2 " , " Option 2 " );
Combobox1.items. Add (listitem0 );
Combobox1.items. Add (listitem1 );
Combobox1.items. Add (listitem2 );
// Set the default option. dropdownlist selects the first option by default.
Combobox1.selectedindex = 0 ; // Set the first item as the default option.
Combobox1.selecteditem = Listitem1; // Set the specified item as the default item
}
}
}
Run
Source code: http://revit.5d6d.com/thread-960-1-1.html
Refer to ComboBox and ListBox in C # (winform) to completely solve the problem.
It was just used. net winform development, found that many controls are difficult to use, may be not familiar with the reason, this is not, a problem to add items to ComboBox is a headache for me, I want to add a name and a value to an item at the same time. I couldn't add it. I checked the information and tested it myself. I finally got everything done. Now I have written down the complete solution.
The data binding method with ComboBox is simple. You can create a data source, bind it to ComboBox, and specify displaymember and valuemember. But it feels very inflexible. What if I want to add another item on ComboBox? There is listitem in the Web, and why does it disappear in winform? I felt really uncomfortable. I found a method on the Internet and added a listitem class and added it to items. It felt pretty good, a bit like the usage in the Web, but the problem came up again, how does the first item become the class name? It is not the name I assigned to it, and there is no problem with other items. So I found out, "because of the item in ComboBox. add (a variable of any type). When displayed, the tostring () method of the variable is called. If the class does not overload tostring (), the displayed result is the namespace + class name, so we can add the overloaded tostring () method. Now, I can easily add items to ComboBox and ListBox.
listitem item = new listitem ("I Am a value", "I am a name");
This. lbchoicoom. items. add (item);
This. lbchoicoom. displaymember = "name";
This. lbchoicoom. valuemember = "ID";