Brief description: I sometimes encounter such problems during development, such as: I have such a notebook brand table
CATEGORY ID |
Category name |
1001 |
Acer |
1002 |
Lenovo |
1003 |
ASUS |
1004 |
HP |
Now I need to display these products to users to make some or more choices. After a user makes a choice, all I need is to select the product ID corresponding to the product, the customer does not know the selected product ID. In terms of web development, it seems that many list controls have provided good support, such as checkboxlist, radiobuttonlist, dropdownlist, and other Web controls all provide two attributes (Value and text ), value can be used to store product IDs, text can be used to store product names, and displayed to users. However, in winform development, such list controls are not as convenient as those in the Web, such as ComboBox and checkedlistbox list controls. They only provide the ability to bind an object. What should I do if I need to implement a function similar to Web? The description is a bit depressing, and the language expression capability needs to be greatly enhanced. The following is an example:
- First, define a notebook brand structure to store the notebook brand ID and brand name.
/// <Summary>
/// Notebook type
/// </Summary>
/// <Typeparam> Value Type </typeparam>
/// <Typeparam> text value type </typeparam>
Public struct computertype <t, k>
{
Private t m_value;
/// <Summary>
/// Value
/// </Summary>
Public T value
{
Get {return m_value ;}
Set {m_value = value ;}
}
Private K m_text;
/// <Summary>
/// Text Value
/// </Summary>
Public K text
{
Get {return m_text ;}
Set {m_text = value ;}
}
Public override string tostring ()
{
Return m_text.tostring ();
}
}
Note: You need to override this tostring ()
2. bind it to the brand table,CodeAs follows:
Computertype <int, string> computertype
Foreach (string strtype in typearray)
{
Computertype = new packettype <int, string> ();
Computertype. value = "brand name ";
Computertype. Text = "brand ID"
Combobox1.items. Add (computertype );
}
PS: it becomes very flexible to use the bound object here.