tailieunhanh - MASTERING DELPHI 6 phần 5

Xây dựng các thành phần là một hoạt động quan trọng cho các lập trình Delphi. Ý tưởng cơ bản là bất cứ lúc nào bạn cần phải có hành vi tương tự ở hai nơi khác nhau trong một ứng dụng, hoặc trong hai ứng dụng khác nhau, bạn có thể đặt mã được chia sẻ bên trong một thành phần lớp hoặc, thậm chí còn tốt hơn,. | 426 Chapter 11 Creating Components Building Your First Component Building components is an important activity for Delphi programmers. The basic idea is that any time you need the same behavior in two different places in an application or in two different applications you can place the shared code inside a class or even better a component. In this section I ll just introduce a couple of simple components to give you an idea of the steps required to build one and to show you different things you can do to customize an existing component with a limited amount of code. The Fonts Combo Box Many applications have a toolbar with a combo box you can use to select a font. If you often use a customized combo box like this why not turn it into a component It would probably take less than a minute. To begin close any active projects in the Delphi environment and start the Component Wizard either by choosing Component New Component or by selecting File New to open the Object Repository and then choosing the Component in the New page. As you can see in Figure the Component Wizard requires the following information FIGURE Defining the new TMdFont-Combo component with the Component Wizard The name of the ancestor type the component class you want to inherit from. In this case we can use TComboBox. The name of the class of the new component you are building we can use TMdFontCombo. The page of the Component Palette where you want to display the new component which can be a new or an existing page. We can create a new page called Md. Copyright 2001 SYBEX Inc. Alameda CA Building Your First Component 427 The filename of the Pascal unit where Delphi will place the source code of the new component we can type MdFontBox. The current search path which should be set up automatically . Click the OK button and the Component Wizard will generate the following simple Pascal source file with the structure of your component. The Install button can be used to install the

TỪ KHÓA LIÊN QUAN