Recent

Author Topic: How to make Enter act like tab on all edits wtihin a form  (Read 10935 times)

mirce.vladimirov

  • Sr. Member
  • ****
  • Posts: 269
How to make Enter act like tab on all edits wtihin a form
« on: February 25, 2011, 04:22:18 pm »
I love when users use the enter as tab, so I usualy use edit's OnKeyPress :

Code: [Select]
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
begin
if key=#13 then begin
  key:=#0;
  selectnext(activecontrol, true, true);
  end;
end;         

But, now I created a form that contains more than 60 edits, grouped in 7-8 groupboxes and there are few non-grouped (just simply placed straight on the form). I dont want to create separate procedures for each of the edits. I tried with a procedure on form's key press, but it doesn't do anything at all :
Code: [Select]
procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
begin
if key=#13 then begin
  key:=#0;
  selectnext(activecontrol, true, true);
end;       

end;
My question is how to make one procedure (on which event) to detect that the user pressed the Enter/Return key and move the cursor to the next available control  ? Reading documentation I've met something like this "(sender as tedit).text", is there possibly a way to trick the form in order to make one procedure and use it form-wide ?
 

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: How to make Enter act like tab on all edits wtihin a form
« Reply #1 on: February 25, 2011, 04:38:30 pm »
What I do is more or less the same as you do and it works perfect on windows and linux:

Code: Pascal  [Select][+][-]
  1. procedure SeleccionarTodo(const ACtrl: TWinControl);
  2. begin
  3.    if ACtrl is TCustomEdit then begin
  4.       if TEdit(ACtrl).Text <> TEdit(ACtrl).SelText then begin
  5.          TEdit(ACtrl).ReadOnly := False;
  6.          TEdit(ACtrl).SelectAll;
  7.       end;
  8.       //Exit;
  9.    end;
  10.    if ACtrl is TComboBox then begin
  11.       if TComboBox(ACtrl).Style <> csDropDownList then begin
  12.          if TComboBox(ACtrl).Text <> TComboBox(ACtrl).SelText then begin
  13.             TComboBox(ACtrl).SelectAll;
  14.          end;
  15.       end;
  16.       Exit;
  17.    end;
  18. end;{<--- SeleccionarTodo }
  19.  
  20.  
  21. procedure IrAControl(const ACtrl: TWinControl);
  22. begin
  23.    ACtrl.Enabled := True;
  24.    ACtrl.TabStop := True;
  25.    if not ACtrl.CanFocus or not ACtrl.Visible then begin
  26.       Exit;
  27.    end;
  28.  
  29.    if ACtrl is TStringGrid then begin
  30.       TStringGrid(ACtrl).Options := TStringGrid(UnCtrl).Options + [goRowSelect];
  31.       //TStringGrid(ACtrl).Row := 0;
  32.    end;
  33.  
  34.    ACtrl.SetFocus;
  35.    SelectAll(UnCtrl);
  36. end;{<--- IrAControl }
  37.  
  38.  
  39. procedure GoToNextControl(const ACtrl: TWinControl; GoForward, ChkTabStop: Boolean);
  40. begin
  41.    ACtrl.SelectNext(UnCtrl, GoForward, ChkTabStop);
  42.    SelectAll(Screen.ActiveControl);
  43. end;{<--- IrAControlSig }
  44.  

so you can try to use these procedures instead of SelectNext().

linux-man

  • New Member
  • *
  • Posts: 16
Re: How to make Enter act like tab on all edits wtihin a form
« Reply #2 on: February 25, 2011, 04:45:10 pm »
Guess this is a little basic (there are probably more sophisticated ways), but something like:

Code: [Select]
procedure TForm1.EnterPressed(Sender: TObject; var Key: char);
begin
if key=#13 then begin
  key:=#0;
  selectnext(activecontrol, true, true);
end;        
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
begin
//Specific code...
EnterPressed(Sender, Key); //in each of the the EditBoxes
end;

Or else, if you don't need any special data management, you can assign the same KeyPress procedure to the KeyPress event of all Edits.
« Last Edit: February 25, 2011, 04:50:03 pm by JP »

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: How to make Enter act like tab on all edits wtihin a form
« Reply #3 on: February 25, 2011, 05:29:54 pm »
Guess this is a little basic (there are probably more sophisticated ways), but something like:

Code: [Select]
procedure TForm1.EnterPressed(Sender: TObject; var Key: char);
begin
if key=#13 then begin
  key:=#0;
  selectnext(activecontrol, true, true);
end;       
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
begin
//Specific code...
EnterPressed(Sender, Key); //in each of the the EditBoxes
end;

Or else, if you don't need any special data management, you can assign the same KeyPress procedure to the KeyPress event of all Edits.

It works fine and this is important.

And yes you can use the same procedure for all components as you said but I think it's important to keep that "if key=#13..." in that seppareted procedure is easier to use and the only risk you run is to forget how you did it.

Zoran

  • Hero Member
  • *****
  • Posts: 1992
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: How to make Enter act like tab on all edits wtihin a form
« Reply #4 on: February 25, 2011, 06:02:43 pm »
It works fine and this is important.

That is true, indeed!
However, there is a more elegant solution. Just set form's KeyPreview property to True and assign this to Form's (not edit's!) OnKeyDown event:

Code: [Select]
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  );
begin
  if Key = VK_RETURN then begin
    Key := 0;
    Self.SelectNext(ActiveControl, True, True);
  end;
end;

When form's KeyPreview property is true, the form receives the key events BEFORE the child controls.
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

linux-man

  • New Member
  • *
  • Posts: 16
Re: How to make Enter act like tab on all edits wtihin a form
« Reply #5 on: February 25, 2011, 06:19:05 pm »
I think that Sender in TForm1.FormKeyDown is the Form itself, so if you want to restrict this behaviour to Editboxes you must test the active object, something like

Code: [Select]
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  );
begin
  if (ActiveControl is TEdit) and (Key = VK_RETURN) then begin
    Key := 0;
    Self.SelectNext(ActiveControl, True, True);
  end;
end;

By the way, nice solution.
« Last Edit: February 25, 2011, 06:25:14 pm by JP »

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: How to make Enter act like tab on all edits wtihin a form
« Reply #6 on: February 25, 2011, 07:08:10 pm »
Oops!! I forgot that property :-[

A drawback of this is when some controls shouldn't execute what is on that particular OnKeyPress. This could be worked araound with an exception list or by hardcoding the exceptions.

Another pro is that every control you drop in the form will automatically perform the acctions programmed there.

mirce.vladimirov

  • Sr. Member
  • ****
  • Posts: 269
Re: How to make Enter act like tab on all edits wtihin a form
« Reply #7 on: February 26, 2011, 12:07:35 am »
Thanks everyone for helping,this issue is solved.
Thank you for spending time and effort to answer my question. I decided to use Zoran's and JP's solution, I find it shortest to implement and most functional since the Form takes over the input, instead of particular objects within it.

 

TinyPortal © 2005-2018