Custom vb.net control programmatic blocking keystroke action

Source: Internet
Author: User
Tags file system resource valid visual studio

First, Introduction

First, this article assumes that you are already familiar with the vb.net and Visual Studio.NET Windows Form Designer.

When you develop custom Windows Form controls, it is often convenient to provide our own Drop-down box type editor to manipulate the properties of the control. Custom type editors can not only provide a richer design-time experience, but can also be a determinant of whether a user likes your control.

If you decide to create your own Drop-down type editor, it should follow a pattern similar to the built-in Drop-down box type editor. Let's take the Anchor property for example. A typical design moment for this property the user interaction is described as follows:

· The user selects the Anchor property in the property grid and clicks the Drop-down button to the right of the character.

· A good graphical control is a Drop-down box that allows the user to use the mouse to click the edges or use the arrow keys to highlight an edge and use the SPACEBAR to select/deselect it.

· Users can receive changes by pressing the ENTER key or by clicking outside the Drop-down control. To cancel this change, the user can press the ESC key.

Next, let's discuss the specific implementation techniques.

Second, realize

First, let's build a Resourceimageeditor type editor that allows you to select an image file from the current file system (just like the built-in Imageeditor class) or select an image resource from an assembly's manifest file. Also, in terms of the user experience, the resourceimageeditor behavior should resemble the system-built-in type editor. The following is an overview of our requirements:

1. When the user selects a property from the property grid, the grid is displayed-the properties that can be edited are displayed in a Drop-down UI format.

2. When you click the Drop-down button, all the image resources in the current assembly are displayed.

3. When the user selects an image resource item, the corresponding image can be loaded from the assembly.

4. Allows you to select an image file, and the last item in the Drop-down list box will be marked "Browse ...". When the user presses the "Browse ..." item, the Classic Open File dialog box is displayed, and the user is able to select an image file from it.

5. By clicking the mouse or using the arrow keys to highlight an item and press ENTER to actually select it to allow the user to select an item from the Drop-down list box. This dropdown selection can be canceled by pressing the ESC key.

Resourceimageeditor is a type editor, so it derives directly or indirectly from the System.Drawing.Design.UITypeEditor class. I decided to derive from the built-in System.Drawing.Design.ImageEditor class because it already implements the image file selection feature. That is, the Imageeditor.editvalue implementation will display a File Open dialog box to allow the user to select an image file from the file system. You can then invoke this functionality from my derived class simply by calling Mybase.editvalue.

To achieve the first requirement above (Display the Drop-down arrow button in the property grid), I must overload the GetEditStyle method to return the appropriate constants from the Uitypeeditoreditstyle enumeration:

Public Overloads Overrides Function GetEditStyle( _
ByVal context As ITypeDescriptorContext) As UITypeEditorEditStyle
 Return UITypeEditorEditStyle.DropDown
End Function

To display the list of image resources, I must enumerate all the resources in a given assembly and display only the image resources in the list. To simplify, I decided to use a simple convention: When a resource name is in a valid image file name extension (. bmp,.jpg,.gif ...) At the end,

We think of it as an image resource and include it in the Drop-down list box. Also, I use a collection of image resource names to populate the Drop-down ListBox control, which is detailed later.

At the beginning, the assembly that is enumerated to query the image resource is the assembly that contains the Resourceimageeditor class. However, we can change it by setting the Resourceimageeditor.resourceassembly property to any valid System.Reflection.Assembly reference.

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.