GUI
techniques II
Scrollable
objects:
- to ensure that an object automatically displays
scrollbars if users resize form, etc, place a scrollbox
on the form & set is align property to alClient
BEFORE dropping the object you wish to be scrolled (eg.
DBGrid, Memo, etc)
- to programmatically force the object to scroll to a
certain child object within a scrollbox, use the scrollbox.scrollInView(desiredControl)
method.
- auto-resizing images with scrollbars:
- place Tscrollbox, set align := alClient;
- place Timage, set align := alNone & autoSize
:= true; strectch := false;
- to zoom an image with a zoom factor called zf:
- with image1 do
- begin
- autosize := false; stretch := true;
height := round(picture.height * zf);
width := round(picture.width * zf);
- //if you also wish to resize form to fit
image:
- Clientwidth := width;ClientHeight :=
height;
- if clientheight > (screen.height-85)
then Form.height := screen.height
- else if clientheight > 115
then Form.height :=
clientheight+85
- else Form.height := 200;
- if clientwidth > (screen.width-28)
then Form.width := screen.width
- else if clientwidth > 272 then
Form.width := clientwidth+28
- else Form1.width := 300;
- end;
To
use "Page Down" and "Page Up" to scroll
records in a dataset:
- set Form.keyPreview := true;
- Form KeyDown event:
- begin
- case key of
- 33: {PageUp ie. VK_PRIOR} dataset.prior;
- 34: {PageDown ie. VK_NEXT} dataset.next;
- end;
- end;
To make the ENTER
key act like a tab key:
- set Form.keyPreview := true;
- on the Form.OnKeyUp method place code:
- if key = VK_RETURN then
focusControl(findNextControl(ActiveControl,true,true,false));
Visual controls and focus:
- Delphi assigns focus to the 1st visual control in the tab
order list (set this order by right clicking on control
at design time), to make your life easier, try to add
controls to form at design time in order of desired tab
order.
- when a user tabs to or clicks on a control, it has focus
& becomes the form's ActiveControl;
- a control cannot receive focus if it is either:
- enabled = false;
- tabstop = false;
- visible = false
- a control that cannot receive focus (eg. TLabel)
- you can programmatically set focus to an
object either by:
- object.setFocus; , or,
- Form.activeControl := object;
- To change color of a control as it gets focus:
- from Delphi help Screen.OnActiveControlChange
example
- create a procedure
TForm.ColorControl(Sender:TObject);
- var i:Integer;
- begin
- for I:= 0 to ControlCount -1
do
- begin
- if Controls[I] is TWinControl
then
- begin
- (Controls[I] as
TWinControl).Focused then
- Controls[I].Color
:= clRed
- else
- Controls[I].Color
:= clWindow;
- end;
- in the Form.create procedure place:
- Screen.OnActiveControlChange :=
ColorControl;