Recent

Author Topic: [solved] need help for TEDIT component  (Read 683 times)

compispezi@web.de

  • New member
  • *
  • Posts: 5
[solved] need help for TEDIT component
« on: August 06, 2022, 08:25:25 pm »
Hello, I am learning about Lazarus. I want to catch key up and key down and enter in TEDIT fields to jump to other TEDIT fields.
In the example I have written this for TEDIT2. But, as soon as I go into TEDIT2 and press a key, the programme stops and can only be terminated with CTRL+F2. Can you please give me support?

Here is the example:
unit unit1;

{$mode ObjFPC}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm2 }

  TForm2 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Edit2KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private

  public

  end;

var
  Form2: TForm2;

implementation

{$R *.lfm}

{ TForm2 }

procedure TForm2.Edit2KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  begin
    case key of
      38    : Edit1.SetFocus; //hoch
      40,13 : Edit3.SetFocus  //runter, Enter
      end;
    end;

procedure TForm2.Button1Click(Sender: TObject);
begin
  Form2.Close;
end;

end.   
« Last Edit: August 08, 2022, 11:14:00 am by compispezi@web.de »

Gald

  • Full Member
  • ***
  • Posts: 107
Re: need help for TEDIT component
« Reply #1 on: August 06, 2022, 09:47:26 pm »
I've tried some things and found out strange behavior.

You have to declare LCLType to the uses and then Uses session this code:

Code: Pascal  [Select][+][-]
  1. if key = VK_DOWN then
  2.   SelectNext(Self as TWinControl, True, False);

A common way to do it is setting the property KeyPreview of your TForm as True.

But... Arrow keys are used by the system to move the keyboard cursor around the TEdit. That's why I've tried on the TEdit's key events, but for my surprise, SelectNext it's going back to the first control (TabStop = 0) every time I press the arrow down.

So, now I'm confusing:

Code: Pascal  [Select][+][-]
  1. //This is setting focus to the first control (TabStop = 0)
  2. SelectNext(Self as TWinControl, False, False);
  3.  
  4. //This is setting focus on the last control
  5. SelectNext(Self as TWinControl, False, False);


Sounds like a bug to me!
Lazarus 2.0.12 r64642 FPC 3.2.0 x86_64-win64-win32/win64/Manjaro KDE 21
AMD Ryzen 3 1300X Quad-Core Processor 3.50 GHz / 8,00 GB RAM / GTX 1500 TI / 2TB M.2 NVMe

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: need help for TEDIT component
« Reply #2 on: August 06, 2022, 09:56:54 pm »
Well, which control you think self is? 
Try using the parameter sender instead.

Gald

  • Full Member
  • ***
  • Posts: 107
Re: need help for TEDIT component
« Reply #3 on: August 06, 2022, 10:21:32 pm »
Well, which control you think self is? 
Try using the parameter sender instead.

Same results.
Lazarus 2.0.12 r64642 FPC 3.2.0 x86_64-win64-win32/win64/Manjaro KDE 21
AMD Ryzen 3 1300X Quad-Core Processor 3.50 GHz / 8,00 GB RAM / GTX 1500 TI / 2TB M.2 NVMe

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: need help for TEDIT component
« Reply #4 on: August 06, 2022, 10:24:43 pm »
Well, which control you think self is? 
Try using the parameter sender instead.

Same results.
attach a small sample that demonstrates the bug please.

Gald

  • Full Member
  • ***
  • Posts: 107
Re: need help for TEDIT component
« Reply #5 on: August 06, 2022, 10:39:45 pm »
Use Tab and ESC for better results.
Set focus to some control on the center of the screen to see this behavior.
Lazarus 2.0.12 r64642 FPC 3.2.0 x86_64-win64-win32/win64/Manjaro KDE 21
AMD Ryzen 3 1300X Quad-Core Processor 3.50 GHz / 8,00 GB RAM / GTX 1500 TI / 2TB M.2 NVMe

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: need help for TEDIT component
« Reply #6 on: August 06, 2022, 11:10:24 pm »
Use Tab and ESC for better results.
Set focus to some control on the center of the screen to see this behavior.
tab is the key that changes to the next control by default, which ends up calling SelectNext twice, don't use known keys, use unused keys instead, like up arrow and down arrow.

I'll have a look on the sample in a half an hour or so.

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: need help for TEDIT component
« Reply #7 on: August 06, 2022, 11:37:26 pm »
Attached is my take on the problem and here is the corrected formkeydown event for your code.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  2. begin
  3.   if Key = VK_DOWN then begin // VK_Return
  4.     SelectNext(ActiveControl, True, False);
  5.     Key := 0;
  6.   end;
  7.   if Key = VK_up then  begin // VK_Escape
  8.     SelectNext(ActiveControl, False, False);
  9.     Key := 0;
  10.   end;
  11. end;
  12.  

Gald

  • Full Member
  • ***
  • Posts: 107
Re: need help for TEDIT component
« Reply #8 on: August 07, 2022, 12:07:35 am »
tab is the key that changes to the next control by default

Three days without my sleep remedy.
I was trying to say Enter but typed TAB, sorry.

Attached is my take on the problem and here is the corrected formkeydown event for your code.

Nice! Thank you so much!
Lazarus 2.0.12 r64642 FPC 3.2.0 x86_64-win64-win32/win64/Manjaro KDE 21
AMD Ryzen 3 1300X Quad-Core Processor 3.50 GHz / 8,00 GB RAM / GTX 1500 TI / 2TB M.2 NVMe

compispezi@web.de

  • New member
  • *
  • Posts: 5
Re: need help for TEDIT component
« Reply #9 on: August 07, 2022, 10:43:19 am »
Tab and ESC is not the usual way while input datas.
I only would use keydown, keyup and Enter.

Thaddy

  • Hero Member
  • *****
  • Posts: 11911
Re: need help for TEDIT component
« Reply #10 on: August 07, 2022, 10:56:34 am »
Tab and ESC is not the usual way while input datas.
I only would use keydown, keyup and Enter.
Note KeyDown is repeating and KeyUp and Enter/Return are single shot. KeyDown should only be used if you need continuous control (navigate, game play) and expensive. The other two are usually much better options.
« Last Edit: August 07, 2022, 10:58:59 am by Thaddy »
Black themes should be banned.

compispezi@web.de

  • New member
  • *
  • Posts: 5
[solved] need help for TEDIT component
« Reply #11 on: August 08, 2022, 11:10:58 am »
Thank you for the very good tips. The resolution is:

procedure TForm1.Edit2KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  );
begin
      case key of
      38      : Edit1.SetFocus; //hoch
      40,13 : Edit3.SetFocus  //runter, Enter
      end;

end;             

So the question is solved.
« Last Edit: August 08, 2022, 03:52:43 pm by compispezi@web.de »

 

TinyPortal © 2005-2018