Delphi's creation of component templates (Component template)

Source: Internet
Author: User

[Tip]
Component templates (Component template) are one or a set of components that are saved for later use.
[/tip]
Component templates make it possible to create, save, and reuse component groups. In fact, a component template does not have to be a set of components, it can be a single component entirely. A small example can be helpful in understanding the purpose of a component template, but let's briefly explain the Windows edit control.

As with all Windows controls, a standard Windows single-line edit control has several predefined actions. One of these actions is related to how the ENTER key is handled. If the user presses the ENTER key while the cursor is on the edit box control, Windows starts looking for the default button on the window, and if the default button is found, Windows taps the button.

What does that mean? For example, a form has multiple edit controls and a default button (such as an OK button or other button with the default property set to True), and when you press the ENTER key and an edit control is the current control, the window is closed, and Windows beeps if the form has no save button. Although this is a standard Windows action, many users find it annoying because they want to press the ENTER key to move the input focus to the next control instead of closing the window.

The solution to this problem is actually very simple. As long as you provide an event handler for the onkeypress event and add code, the code is as follows:

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);begin  if Key = Char(vk_return) then  begin    Key := #0;    PostMessage(Handle, WM_NEXTDLGCTL, 0, 0);  end;end;

This code first checks whether the pressed key is the ENTER key (Vk_return virtual key code), if it is, it will set the key value of #0,windows will not beep. The code line 6th sends a Windows WM_NEXTDLGCTL message to the form, which sets the focus to the next control, which is all about it.

After you have written the code for the new edit component, you can save it as a component template. When you save the component, all the code is saved together. The created code template is saved on the Templates page of the palette. The following creates a component template so that the reader can understand it better. Here we follow the steps to build the component template:

(1) Place an edit component on the blank form, change its Name property to Enterastab, and clear its Text property;

(2) switch to the events page of the object Inspector. Creates an event handler for the onkeypress event. Enter the following code:

procedure TForm1.EnterAsTabKeyPress(Sender: TObject; var Key: Char);begin  if Key = Char(vk_return) then  begin    Key := #0;    PostMessage(Handle, WM_NEXTDLGCTL, 0, 0);  end;end;

(3) Select the Edit component and select "Component" from the main menu | Create Component Template menu item, which displays the Component Template Information dialog box;

(4) in the "Component Name" field, enter Tenterastab, the dialog box as shown above;

(5) Click OK to save the component template.

Now there is a Templates page on the palette.

Switch to the Templates page, select the component you want to place on the form, and the code for the onkeypress event handler is added to the form along with the component.

[Tip]

一个窗体上如果有多个这样的组件,对于窗体上每个EnterAsTab组件,OnKeyPress事件处理程序的代码是重复的。为了避免代码重复,可只放置一个EnterAsTab组件于窗体上,其他组件可以是标准Edit组件,它们的OnKeyPress事件均有EnterAsTab组件的OnKeyPress事件处理程序处理。

The biggest advantage of component templates is that the code for each component's event handlers is stored with the component. Component templates let you set the value components together in their own arrangement: a common dialog box with predefined filters and headings, a symbol accelerator button, a list box, or a combo box that automatically loads items from a file and any other collection.

Although the component template concept works for a single component, it is more meaningful to use it to handle multiple components. If you have a set of components that you want to place on a form, you can create a component template that consists of this set of components, which you can then simply click on a component template that is composed of them.

[Note]

组件模板与对象库中保存的窗体有一些相似之处。组件模板用于窗体上的组件组。使用对象库保存要重复使用的窗体。

Delphi Create Component Templates (Component template)

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.