In C # (winform), adding items to ComboBox and ListBox and setting pre-options are completely solved)

Source: Internet
Author: User

1.

By default, the ComboBox in winform is set to display the list in multiple texts, which is usually not in line with your daily applications,

Because daily applications usually bind key-value pairs.

So what should we do in the form of key-value pairs?

Because the value of each item in combox is an object, it is actually a key/value pair.
I use an instance of the following class as an item:

/// <Summary>
/// ComboBox item
/// </Summary>
Class listitem: system. Object
{
Private string _ value = string. empty;
Private string _ text = string. empty;

/// <Summary>
/// Value
/// </Summary>
Public String Value
{
Get {return this. _ value ;}
Set {This. _ value = value ;}
}
/// <Summary>
/// Displayed text
/// </Summary>
Public String text
{
Get {return this. _ text ;}
Set {This. _ text = value ;}
}

Public listitem (string value, string text)
{
This. _ value = value;
This. _ text = text;
}
Public override string tostring ()
{
Return this. _ text;
}

}

This class defines the value of ComboBox. First, we define a listitem list as the data source of ComboBox:

List items = new list ();
items. add (New listitem ("0", "item_0_text");
items. add (New listitem ("1", "item_shorttext");
items. add (New listitem ("2", "item_2_text");
items. add (New listitem ("3", "item_3_text");
items. add (New listitem ("4", "item_4_text");
items. add (New listitem ("5", "item_5_text");
then set it accordingly:

// Maps the data source attributes to the ComboBox attributes.
Drptest. displaymember = "text"; // display
Drptest. valuemember = "value"; // Value
Then you can bind it:

Drptest. datasource = items; // bind data
After binding data, you can set the default selection items and values:

Drptest. selectedvalue = "4"; // set the selection item

// Obtain the selected items
Listitem selecteditem = (listitem) drptest. selecteditem;
String value = selecteditem. value; // Value
String text = selecteditem. Text; // displayed text

For other operations, just draw a gourd.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/fcsh820/archive/2009/02/07/3867053.aspx

 

 

2.

Technorati tags: winform C #

In the web page, it is very convenient to use. However, when using C # Today, ComboBox can only display text with value, but does not support keys, when I add a large amount of data to the list, why?

No way, I can only do it myself. I studied ComboBox and added the object to add. It seems that there is a drama. if the process is not written, I only put the results. I can see it at a Glance:

Custom listitem

View plaincopy to clipboardprint?

    1. Public class listitem
    2. {
    3. Public String key {Get; set ;}
    4. Public String Value {Get; set ;}
    5. Public listitem (string pkey, string pvalue)
    6. {
    7. This. Key = pkey;
    8. This. value = pvalue;
    9. }
    10. Public override string tostring ()
    11. {
    12. Return this. Key;
    13. }
    14. }

When writing data:

View plaincopy to clipboardprint?

    1. Listitem li = new listitem ("name", "content ");
    2. This. combobox1.items. Add (LI );
    3. // N more
    4. This. combobox1.items. Add (LI );
    5. This. combobox1.displaymember = "key"; // The Key of the listitem.
    6. This. combobox1.valuemember = "value"; // corresponding to the value of listitem

When reading data:

    1. Listitem li = (listitem) combobox1.selecteditem;
    2. MessageBox. Show (Li. Key + Li. value );

Link: http://mdeve.com/blog/post/204

ComboBox

 

Technorati tags: winform C #

In the web page, it is very convenient to use. However, when using C # Today, ComboBox can only display text with value, but does not support keys, when I add a large amount of data to the list, why?

No way, I can only do it myself. I studied ComboBox and added the object to add. It seems that there is a drama. if the process is not written, I only put the results. I can see it at a Glance:

Custom listitem

View plaincopy to clipboardprint?

    1. Public class listitem
    2. {
    3. Public String key {Get; set ;}
    4. Public String Value {Get; set ;}
    5. Public listitem (string pkey, string pvalue)
    6. {
    7. This. Key = pkey;
    8. This. value = pvalue;
    9. }
    10. Public override string tostring ()
    11. {
    12. Return this. Key;
    13. }
    14. }

When writing data:

View plaincopy to clipboardprint?

    1. Listitem li = new listitem ("name", "content ");
    2. This. combobox1.items. Add (LI );
    3. // N more
    4. This. combobox1.items. Add (LI );
    5. This. combobox1.displaymember = "key"; // The Key of the listitem.
    6. This. combobox1.valuemember = "value"; // corresponding to the value of listitem

When reading data:

    1. Listitem li = (listitem) combobox1.selecteditem;
    2. MessageBox. Show (Li. Key + Li. value );

Link: http://mdeve.com/blog/post/204

ComboBox

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.