[Original] The winform drop-down box is automatically selected

Source: Internet
Author: User

Applicable environment: winform development and vs2008 development tools

Problem description:

    1. There are a series of products, each product has a unique product ID, product name.
    2. You need to bind the product to the drop-down box (ComboBox). Because of the winform drop-down box control, unlike the drop-down box control in web development, you can directly add two elements to each item (Value and text ). However, it allows you to directly add object items, so it is more flexible.
    3. The product information needs to be displayed in the drop-down box, not just the simple display name, but some special processing is required, so it cannot be used.

Cboxproduct. displaymember = "productname ";

Cboxproduct. valuemember = "productid ";

Cboxproduct. datasource = datatable; // Product Data Source

4. You must be able to automatically select a specific product based on the product information.

Solution:

    1. First, we can define a product information class that contains various product information. The required information can be used as a constructor parameter so that the information must be set for the instantiated product class. At the same time, we can also extract a public class for binding the drop-down box information, because in most cases, we generally use the drop-down box to bind only two types of information. However, the data format of the information is not fixed, so you can consider generics.
    2. Create a data class in the public drop-down box using generics, which contains two attributes: Value and text.
    3. The Value Attribute must be unique as the index value of the drop-down box. Therefore, when determining whether the data items in the drop-down box are the same, it can be determined by determining whether the value is the same (it must be noted that here we have not forcibly determined whether the value is unique, you can determine when binding data ), to determine whether two objects are equal, consider rewriting the equal method.

Let's look at a practical example!

InstanceCode:

    1. Create a product type class (producttype class). Because the product type can be an unlimited category (the sub-product type levels under a product type are not fixed, there may be many sub-types ), when the product type is displayed in the drop-down list, the Parent and Child product types must be differentiated. The code for the product type is as follows:

Class producttype <t, TK>

{

Public producttype (T value, TK text)

{

_ Value = value;

_ Text = text;

}

 

Private T _ value;

/// <Summary>

/// Value

/// </Summary>

Public T value

{

Get {return _ value ;}

Set {_ value = value ;}

}

 

Private TK _ text;

/// <Summary>

/// Display value

/// </Summary>

Public TK text

{

Get {return _ text ;}

Set {_ text = value ;}

}

 

Public override string tostring ()

{

Return _ text. tostring ();

}

 

Public override bool equals (Object OBJ)

{

Return (producttype <t, TK>) OBJ). value. Equals (_ value );

}

}

    1. Create a test form and add a drop-down box control (ComboBox) named cboxproducttype. Bind the product type data to the drop-down list. Binds the product type cyclically and recursively traverses its subproduct types. The implementation code is as follows:

Private void bindingproducttype (INT parentproducttypeid, string splitstr)

{

VaR datasource = bllproducttype. getlist ("parentid =" + parentproducttypeid). Tables [0];

Foreach (datarow in datasource. Rows)

{

VaR producttypeid = convert. toint32 (datarow ["producttypeid"]);

Cboxproducttype. Items. Add (New producttype () <int, string> (producttypeid,

Splitstr + datarow ["producttypename"]);

Bindingproducttype (producttypeid, splitstr + @ "-- | ");

}

}

    1. After a product type ID is provided, the product type is automatically selected in the drop-down box. You can use the cboxproducttype. selecteditem attribute to set the product type. For details, refer to the Code:

Producttype <int, string> producttype = new producttype (producttypeid ,"");

Cboxproducttype. selecteditem = producttype;

Because the product type ID is a unique value, we only compare the product type with the value. Therefore, when instantiating the producttype class, the text attribute value is not required, because the text property value has nothing to do with determining whether the product type is equal. (In this case, fill in and select existing data when editing data .)

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.