Access the answers to your technology questions today.
Subscribe Now
30-day free trial. Register in 60 seconds.
What Makes Experts Exchange Unique?
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.
Try it out and discover for yourself.
Subscribe Now
30-day free trial. Register in 60 seconds.
Join the Community
Give a Little. Get a Lot.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Join the Community
by: rllibbyPosted on 2009-02-06 at 09:39:55ID: 23571993
The routine in the OnKeyDown handler (below) will build a list of the forms controls, and order them by TabOrder. It then walks the list to locate the next edit control int he desired direction. All you need to do it:
.TabOrder - TWinControl(Item2).TabOrde r;
TObject; var Key: Word; Shift: TShiftState);
ol); ndex]); s); ctlEdit); ndex]) is TEdit) then wIndex]); ndex]) is TEdit) then wIndex]); ndex]) is TEdit) then wIndex]); unt) downto Succ(dwSelf) do ndex]) is TEdit) then wIndex]);
- Set the form's KeyPreview property to true.
- Add the following code and bind the Form's OnKeyDown event handler to the FormKeyDown procedure.
Russell
---
function SortControls(Item1, Item2: Pointer): Integer;
begin
// Sort the controls by tab order
result:=TWinControl(Item1)
end;
procedure TForm1.FormKeyDown(Sender:
var ctlEdit: TEdit;
listCtrls: TList;
dwSelf: Integer;
dwIndex: Integer;
begin
// Check key and active control
if ((Key = VK_UP) or (Key = VK_DOWN)) and (ActiveControl is TEdit) then
begin
// Get current edit
ctlEdit:=TEdit(ActiveContr
// Create list to hold all controls
listCtrls:=TList.Create;
// Resource protection
try
// Add all controls to list
for dwIndex:=0 to Pred(ControlCount) do listCtrls.Add(Controls[dwI
// Sort the list
listCtrls.Sort(SortControl
// Locate the index of the control we are currently on
dwSelf:=listCtrls.IndexOf(
// Clear the edit control pointer
ctlEdit:=nil;
// Now we either need to walk forwards or backwards, as well as implement wrap around
if (Key = VK_DOWN) then
begin
// Walk from index plus one to end of list
for dwIndex:=Succ(dwSelf) to Pred(listCtrls.Count) do
begin
// Check for edit type
if (TWinControl(listCtrls[dwI
begin
// Found the next edit
ctlEdit:=TEdit(listCtrls[d
// Done processing
break;
end;
end;
// Check edit
if (ctlEdit = nil) then
begin
// Walk from start of list to current index
for dwIndex:=0 to Pred(dwSelf) do
begin
// Check for edit type
if (TWinControl(listCtrls[dwI
begin
// Found the next edit
ctlEdit:=TEdit(listCtrls[d
// Done processing
break;
end;
end;
end;
end
else
begin
// Walk from index minus one to start of list
for dwIndex:=Pred(dwSelf) downto 0 do
begin
// Check for edit type
if (TWinControl(listCtrls[dwI
begin
// Found the next edit
ctlEdit:=TEdit(listCtrls[d
// Done processing
break;
end;
end;
// Check edit
if (ctlEdit = nil) then
begin
// Walk from end of list to current index plus one
for dwIndex:=Pred(listCtrls.CO
begin
// Check for edit type
if (TWinControl(listCtrls[dwI
begin
// Found the next edit
ctlEdit:=TEdit(listCtrls[d
// Done processing
break;
end;
end;
end;
end;
// If new edit is assigned then set focus
if Assigned(ctlEdit) then ctlEdit.SetFocus;
finally
// Free the list
listCtrls.Free;
end;
end;
end;