The ComboBox and ListBox items in C # (winform) are completely resolved.

Source: Internet
Author: User

The ComboBox and ListBox items in C # (winform) are completely resolved.

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.

First, add the listitem class:
/// <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;
}
}
}

use
listitem item = new listitem ("I Am a value ", "my name");
This. lbchoicoom. items. add (item);
This. lbchoicoom. displaymember = "name";
This. lbchoicoom. valuemember = "ID";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.