Creating personalized ListBox Controls with vb.net

Source: Internet
Author: User
Tags visual studio

A ListBox (list box) is also a commonly used and encountered component of the day, in Visual Basic. NET is actually instantiated from the ListBox class in the. NET Framework SDK. The listbox created by the ListBox class in the. NET Framework SDK is the most typical, To make your own listbox unique, you must write the appropriate code, but the ListBox class in the. Net FrameWork SDK provides a rich, operable "tool" for implementing a personalized ListBox, which is the required event to draw a ListBox, Related properties as well as rich rendering methods. In this article, you'll see how each element in the listbox is plotted, including drawing a variety of fonts, colors, and adding a border to each item, plus a picture for each item. Let's take a look at the "tools" provided by the. Net FrameWork SDK to implement the manual draw listbox in vb.net.

The. Net Frame Work SDK provides tools for implementing a personalized listbox in vb.net:

1. Properties:

By default, the ListBox is not allowed to be redrawn because the "DrawMode" property value in the listbox is "Normal". The function of this property is to set the drawing mode of the ListBox, and "Normal" is a member of the enumeration DrawMode. setting to "Normal" means that each member of the ListBox is System-controlled and has equal size. The "DrawMode" property works in this article similar to the "OwnerDraw" attribute in the previous article. Enumerate the other members of the DrawMode see table 01:

Property

Description

Normal All elements of a component are drawn by the operating system, and the element size is equal.
OwnerDrawFixed All elements of a component are drawn manually, and the elements are equal in size.
OwnerDrawVariable All elements of a component are drawn manually, and the element size may not be equal.

Table 01: Enumerating the Members in DrawMode and their descriptions

This article sets the "DrawMode" property value for the listbox to be "OwnerDrawVariable". Because the program described in this article not only draws the elements in the ListBox, it also sets the size of the elements in the listbox.

2. Events:

After setting the "DrawMode" property value of "OwnerDrawVariable", some events are triggered to draw the listbox, such as the DrawItem event and the MeasureItem event. The DrawItem event mainly handles the drawing of the various elements in the ListBox, and the MeasureItem event mainly handles the size of the elements of the set ListBox. The DrawItem event and the MeasureItem event in the listbox are the same as the DrawItem events and MeasureItem events described in the previous section, Receive DrawItemEventArgs parameters and MeasureItemEventArgs parameter data separately, and use this data to process. For a detailed description of these two events, see the previous section.

3. How to draw graphics:

The method used to draw the listbox is essentially the same as in the previous section, except that the addition of the graphical rendering method in this article is drawimage,drawimage to the specified position to draw the original size of the specified shape. In this paper, the DrawImage method is adopted to add the corresponding graphs in the listbox.

Two Design, commissioning and operating environment:

(1). Microsoft Windows 2000 Server Edition.

(2). Visual Studio. NET 2003 Enterprise build,. NET FrameWork SDK 1.1 version number 4322.

Three Draw your own ListBox step-by-step:

First, follow these steps to create a new visual Basic. NET project and add a ListBox component to the project form:

1. Start visual Studio. Net.

2. Select Menu "File" | "new" | "Project", pop-up the "New Project" dialog box.

3. Set the project type to Visual basic project.

4. Set "template" to "Windows Application".

5. In the Name text box, enter "Create your own listbox."

Enter the E:vs in the text box in the location. NET project, and then click the OK button, so that in the E:vs. NET project directory, a folder called "Create Your Own ListBox" is created, and a project file named "Create your own ListBox" is built inside.

6. Take visual Studio. NET to the Form1.vb window, and drag a ListBox component into the form from the Windows Forms Components tab in the Toolbox to the Form1 form. and set this ListBox component to add "AAA", "BBB" and "CCC" three items.

So one of the most typical listbox is done. As shown in Figure 01:

Figure 01: One of the project design interfaces for "Building your own ListBox"

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.