RAD Studio
ContentsIndex
PreviousUpNext
Working with controls
Name 
Description 
When the user drags something over a control, that control receives an OnDragOver event, at which time it must indicate whether it can accept the item if the user drops it there. The drag cursor changes to indicate whether the control can accept the dragged item. To accept items dragged over a control, attach an event handler to the control's OnDragOver event.
The drag-over event has a parameter called Accept that the event handler can set to True if it will accept the item. Accept changes the cursor type to an accept cursor or not.
The drag-over event has other... more 
Every string list has the ability to hold a list of objects in addition to its list of strings. You can also add graphical objects of varying sizes to a string list.
For example, in a file manager application, you may want to add bitmaps indicating the type of drive along with the letter of the drive. To do that, you need to add the bitmap images to the application, then copy those images into the proper places in the string list as described in the following sections.
Note that you can also organize graphical objects using an image list... more 
Several controls let you customize the way the control is rendered. These include list boxes, combo boxes, menus, headers, tab controls, list views, status bars, tree views, and toolbars. Instead of using the standard method of drawing a control or its items, the control's owner (generally, the form) draws them at runtime. The most common use for owner-draw controls is to provide graphics instead of, or in addition to, text for items. For information on using owner-draw to add images to menus, see Adding images to menu items..
All owner-draw controls contain lists of items. Usually, those lists are... more 
Once you have graphical images in an application, you can associate them with the strings in a string list. You can either add the objects at the same time as the strings, or associate objects with existing strings. The preferred method is to add objects and strings at the same time, if all the needed data is available.
The following example shows how you might want to add images to a string list. This is part of a file manager application where, along with a letter for each valid drive, it adds a bitmap indicating each drive's type. The OnCreate... more 
An image control is a nonvisual control that contains a graphical image, such as a bitmap. You use image controls to display graphical images on a form. You can also use them to hold hidden images that you'll use in your application. For example, 
Rich edit and memo components can contain horizontal or vertical scroll bars, or both, as needed. When word wrapping is enabled, the component needs only a vertical scroll bar. If the user turns off word wrapping, the component might also need a horizontal scroll bar, since text is not limited by the right side of the editor. 
Most text-handling applications provide users with a way to move selected text between documents, including documents in different applications. TClipboard object encapsulates a clipboard (such as the Windows Clipboard) and includes methods for cutting, copying, and pasting text (and other formats, including graphics). The Clipboard object is declared in the Clipbrd unit. 
You can customize the appearance of the mouse pointer during drag operations by setting the source component's DragCursor property (VCL only). 
A docking site automatically accepts child controls when they are released over the docking site. For most controls, the first child is docked to fill the client area, the second splits that into separate regions, and so on. Page controls dock children into new tab sheets (or merge in the tab sheets if the child is another page control).
Three events allow docking sites to further constrain how child controls are docked:  
A docking site automatically allows child controls to be undocked when they are dragged and have a DragMode property of dmAutomatic. Docking sites can respond when child controls are dragged off, and even prevent the undocking, in an OnUnDock event handler:  
Dockable child controls have two events that occur during drag-and-dock operations: OnStartDock, analogous to the OnStartDrag event of a drag-and-drop operation, allows the dockable child control to create a custom drag object. OnEndDock, like OnEndDrag, occurs when the dragging terminates. 
You can use a TDragObject descendant to customize an object's drag-and-drop behavior. The standard drag-over and drag-and-drop events indicate the source of the dragged item and the coordinates of the mouse cursor over the accepting control. To get additional state information, derive a custom drag object from TDragObject or TDragObjectEx (VCL only) and override its virtual methods. Create the custom drag object in the OnStartDrag event.
Normally, the source parameter of the drag-over and drag-and-drop events is the control that starts the drag operation. If different kinds of control can start an operation involving the same kind of data, the... more 
Applications that use the Clipbrd unit can cut, copy, and paste text, graphics, and objects through the clipboard. The edit components that encapsulate the standard text-handling controls all have methods built into them for interacting with the clipboard.
To cut, copy, or paste text with the clipboard, call the edit component's CutToClipboard, CopyToClipboard, and PasteFromClipboard methods, respectively.
For example, the following code attaches event handlers to the OnClick events of the EditCut, EditCopy, and EditPaste commands, respectively:  
You can delete the selected text in an edit component without cutting it to the clipboard. To do so, call the ClearSelection method. For example, if you have a Delete item on the Edit menu, your code could look like this:  
It is often useful to disable menu commands without removing them from the menu. For example, in a text editor, if there is no text currently selected, the Cut, Copy, and Delete commands are inapplicable. An appropriate time to enable or disable menu items is when the user selects the menu. To disable a menu item, set its Enabled property to False.
In the following example, an event handler is attached to the OnClick event for the Edit item on a child form's menu bar. It sets Enabled for the Cut, Copy, and Delete menu items on the Edit... more 
Drag-and-drop is often a convenient way for users to manipulate objects. You can let users drag an entire control, or let them drag items from one control—such as a list box or tree view—into another.  
When an application needs to draw or redraw an owner-draw control, the operating system generates draw-item events for each visible item in the control. Depending on the control, the item may also receive draw events for the item as a part of the item.
To draw each item in an owner-draw control, attach an event handler to the draw-item event for that control.
The names of events for owner drawing typically start with one of the following:
  • OnDraw, such as OnDrawItem or OnDrawCell
  • OnCustomDraw, such as OnCustomDrawItem
  • OnAdvancedCustomDraw, such as OnAdvancedCustomDrawItem
The draw-item event contains parameters identifying... more 
When you indicate that a control is owner-drawn, either by setting a property or supplying a custom draw event handler, the control is no longer drawn on the screen. Instead, the operating system generates events for each visible item in the control. Your application handles the events to draw the items. 
If a control indicates that it can accept a dragged item, it needs to handle the item should it be dropped. To handle dropped items, attach an event handler to the OnDragDrop event of the control accepting the drop. Like the drag-over event, the drag-and-drop event indicates the source of the dragged item and the coordinates of the mouse cursor over the accepting control. The latter parameter allows you to monitor the path an item takes while being dragged; you might, for example, want to use this information to change the color of components if an item is dropped.
In... more 
A drag operation ends when the item is either successfully dropped or released over a control that cannot accept it. At this point an end-drag event is sent to the control from which the drag was initiated. To enable a control to respond when items have been dragged from it, attach an event handler to the control's OnEndDrag event.
The most important parameter in an OnEndDrag event is called Target, which indicates which control, if any, accepts the drop. If Target is nil, it means no control accepts the dragged item. The OnEndDrag event also includes the coordinates... more 
You may want to adjust pop-up menu items before displaying the menu, just as you may want to enable or disable items on a regular menu. With a regular menu, you can handle the OnClick event for the item at the top of the menu.
With a pop-up menu, however, there is no top-level menu bar, so to prepare the pop-up menu commands, you handle the event in the menu component itself. The pop-up menu component provides an event just for this purpose, called OnPopup
Descendants of TWinControl can act as docking sites and descendants of TControl can act as child windows that are docked into docking sites. For example, to provide a docking site at the left edge of a form window, align a panel to the left edge of the form and make the panel a docking site. When dockable controls are dragged to the panel and released, they become child controls of the panel.
 
 
Pop-up, or local, menus are a common ease-of-use feature for any application. They enable users to minimize mouse movement by clicking the right mouse button in the application workspace to access a list of frequently used commands.
In a text editor application, for example, you can add a pop-up menu that repeats the Cut, Copy, and Paste editing commands. These pop-up menu items can use the same event handlers as the corresponding items on the Edit menu. You don't need to create accelerator or shortcut keys for pop-up menus because the corresponding regular menu items generally already have shortcuts.
A... more 
The SelectAll method selects the entire contents of an edit control, such as a rich edit or memo component. This is especially useful when the component's contents exceed the visible area of the component. In most other cases, users select text with either keystrokes or mouse dragging.
To select the entire contents of a rich edit or memo control, call the RichEdit1 control's SelectAll method.
For example:  
For text in an edit control, before you can send any text to the clipboard, that text must be selected. Highlighting of selected text is built into the edit components. When the user selects text, it appears highlighted.
The table below lists properties commonly used to handle selected text.
Properties of selected text  
To customize the drawing of a control, you must supply event handlers that render the control's image when it needs to be painted. Some controls receive these events automatically. For example, list views, tree views, and toolbars all receive events at various stages in the drawing process without your having to set any properties. These events have names such as OnCustomDraw or OnAdvancedCustomDraw.
Other controls, however, require you to set a property before they receive owner-draw events. List boxes, combo boxes, header controls, and status bars have a property called Style. Style determines whether the control uses the... more 
In a rich edit or memo component, text can be left- or right-aligned or centered. To change text alignment, set the edit component's Alignment property. Alignment takes effect only if the WordWrap property is True; if word wrapping is turned off, there is no margin to align to.
For example, the following code attaches an OnClick event handler to a CharacterLeft menu item, then attaches the same event handler to both a CharacterRight and CharacterCenter menu item.  
Before giving your application the chance to draw each item in a variable owner-draw control, the control receives a measure-item event, which is of type TMeasureItemEvent. TMeasureItemEvent tells the application where the item appears on the control.
Delphi determines the size of the item (generally, it is just large enough to display the item's text in the current font). Your application can handle the event and change the rectangle chosen. For example, if you plan to substitute a bitmap for the item's text, change the rectangle to the size of the bitmap. If you want a bitmap and text,... more 
Every control has a property called DragMode that determines how drag operations are initiated. If DragMode is dmAutomatic, dragging begins automatically when the user presses a mouse button with the cursor on the control. Because dmAutomatic can interfere with normal mouse activity, you may want to set DragMode to dmManual (the default) and start the dragging by handling mouse-down events.
To start dragging a control manually, call the control's BeginDrag method. BeginDrag takes a Boolean parameter called Immediate and, optionally, an integer parameter called Threshold. If you pass True for Immediate, dragging begins immediately. If you pass... more 
The following topics how to use various features of rich edit and memo controls. Some of these features work with edit controls as well.  
The ChangeScale method is dependent on the BorderStyle property of forms. When BorderStyle is Single, you cannot resize the form manually, and ChangeScale adjusts form size as would be expected. However, if a form's BorderStyle is Sizeable, ChangeScale unexpectedly fails, as if expecting the user to take care of sizing.
ChangeScale is sensitive to columns in a DBGrid that have been sized by the developer. ChangeScale typically fails on such columns. This can result in a DBGrid component and Form being adjusted by ChangeScale, but the Columns in the DBGrid not being resized.
When implementing either of these methods, it... more 
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!