Recent

Author Topic: changing focus on a form  (Read 23116 times)

bayparinc

  • New Member
  • *
  • Posts: 16
changing focus on a form
« on: June 23, 2009, 06:06:24 am »
Hi to all,

I have this scenario, a Form with a 3 TEdit, TMemo, 2 TButtons and 1 TBitbtn.  When the form is   displayed, the input focus is setting on edit box 1. What I want to do is put a code on the event of tbitbtn to change the focus from one control to the other, as if i have pressed the tab key from the keyboard.  I have use     
  SelectNext(Sender as TWinControl, True, True );  // but doesn't do the job
have tried also
  PerformTab( True );  //but does not work either
also DoKeyPress( VK_Tab );  doesn't work also

Is their any other solution that i can i try?  Many thanks

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
Re: changing focus on a form
« Reply #1 on: June 23, 2009, 11:03:48 am »
Why do you pass the sender to SelectNext? Is the control you want to select then next in the taborder after the clicked bitbtn? If not, SelectNext(ActiveControl,true, true); may work better.

bayparinc

  • New Member
  • *
  • Posts: 16
Re: changing focus on a form
« Reply #2 on: June 24, 2009, 03:14:20 am »
yes, i want to pass the control to next taborder after the clicked on bitbtn, better solution if it will work for other keys also.

I have tried SelectNext( ActiveControl, true, true); but doesn't do the job too. 

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
    SelectNext( ActiveControl, true, true );  // this one jumps into one of the control but not the next      taborder on the first clicked only

   //SelectNext(Sender as TWinControl, True, True );  // but doesn't do the job
   //PerformTab( True );  //but does not work either
   //DoKeyPress( VK_Tab );  doesn't work also

end;

I'm trying the postmessage that works with delphi but doesn't compile in lazarus maybe because it's not a cross platform.


clauslack

  • Sr. Member
  • ****
  • Posts: 275
Re: changing focus on a form
« Reply #3 on: June 24, 2009, 04:25:01 am »
ENTER key simulates TAB key betwen TEdit controls. You can adapt this to your needs.
Code: [Select]
Form1.KeyPreview:=true;

Code: [Select]
procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
begin
   if key=#13 then
   if (ActiveControl is TEdit) then
   begin
        key:=#0;
        SelectNext(ActiveControl,true, true);
    end;
end;           

Regards




bayparinc

  • New Member
  • *
  • Posts: 16
Re: changing focus on a form
« Reply #4 on: June 24, 2009, 04:58:26 am »
the application that i'm writing is actually a touch screen, possibility that there will be no keyboard.  So i'm attempting to mimic a tab or enter key after clicking the bitbtn. 

SelectNext(ActiveControl, true, true); works fine using keyboard.

Thanks for the help.
 

bayparinc

  • New Member
  • *
  • Posts: 16
Re: changing focus on a form
« Reply #5 on: June 26, 2009, 04:02:30 am »
I'm still looking for a way in which i can force my program to drive a keypress using buttons.  I'm doing a program with a numeric tbuttons and 3 tedit boxes, a tab button and esc button in form.  If any of the numeric buttons is pressed, it will be an input to the tedit box depending on the focus.  Tab button will move the focus on the next tedit, while esc button will close the form.  Initial focus will be tedit1 when the form show.

I'm thinking that if my user click those buttons it will push a keypress to the keyboard buffer so that it can be trap using the  keypreview := true and keypress which will be much easier to trap.

Upto now i don't have a lack of solving it:-( Any suggestions will be much appreciated. Thanks again.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: changing focus on a form
« Reply #6 on: June 26, 2009, 09:37:03 am »
Quote
I'm still looking for a way in which i can force my program to drive a keypress using buttons.
You can call OnKeyPress from button's OnClick, for instance:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  c: Char = #13;
begin
  Form1KeyPress(Sender,c); // send enter key
end;

bayparinc

  • New Member
  • *
  • Posts: 16
Re: changing focus on a form
« Reply #7 on: June 26, 2009, 12:49:44 pm »
Quote
I'm still looking for a way in which i can force my program to drive a keypress using buttons.
You can call OnKeyPress from button's OnClick, for instance:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  c: Char = #13;
begin
  Form1KeyPress(Sender,c); // send enter key
end;

Thanks Leledumbo,
   Form1.KeyPress( c ); works:-)

But i face another challenge, i need the focus to stay on my edit box when clicking buttons. I think the focus is given on the clicked button, so the KeyPress which is passed by the onclick event is not processed/loss.

Hope someone had work on this before. 

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: changing focus on a form
« Reply #8 on: June 26, 2009, 01:57:45 pm »
Quote
But i face another challenge, i need the focus to stay on my edit box when clicking buttons.
Add
Code: [Select]
Edit1.SetFocus; // or any of the TEdit
somewhere in the button's OnClick.

bayparinc

  • New Member
  • *
  • Posts: 16
Re: changing focus on a form
« Reply #9 on: June 29, 2009, 03:10:34 am »
Thanks again leledumbo for your prompt reply.

Sorry, this one is i think a basic in programming, but it seems i don't know how to make it.  Basically the keypress are not posted on the edit entry, another thing is how to know where is the focus entry?

Thanks for your tips, it is much appreciated.

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, Buttons;

type

  { TForm1 }

  TForm1 = class(TForm)
    btn2: TBitBtn;
    btn3: TBitBtn;
    btnEnter: TBitBtn;
    btnTab: TBitBtn;
    btnEsc: TBitBtn;
    btn1: TBitBtn;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedure btnTabClick(Sender: TObject);
    procedure btnEscClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: char);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;


var
  Form1: TForm1;

implementation

{ TForm1 }

procedure TForm1.btn1Click(Sender: TObject);
var c:char;
begin
  c:= '1';

  Edit1.SetFocus;       //currently hardcoded to be in edit1; enhancement is to know who has the focus when a keypress is done

  Form1.Keypress(c);    // it doesn't work, it does not post the char in my edit box ;-(

end;

procedure TForm1.btn2Click(Sender: TObject);
var c:char;
begin
  c:= '2';

  Edit1.SetFocus;         //currently hardcoded to be in edit1; enhancement is to know who has the focus when a keypress is done
  Form1.Keypress(c);    // it doesn't work, it does not post the char in my edit box ;-(

end;

procedure TForm1.btn3Click(Sender: TObject);
var c:char;
begin
  c:= '3';

  Edit1.SetFocus;     //currently hardcoded to be in edit1; enhancement is to know who has the focus when a keypress is done

  Form1.Keypress(c);     // it doesn't work, it does not post the char in my edit box ;-(

end;

procedure TForm1.btnTabClick(Sender: TObject);
var c:char;
begin

  c:=chr(9);               // should simulate a tab key
  Form1.Keypress(c);       //

end;

procedure TForm1.btnEscClick(Sender: TObject);
var c:char;
begin

  c:=#27;
  Form1.Keypress(c);              // this one work; the form close

end;

procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
begin

  if Key = #27 then close ;
  ShowMessage('Key=' + Key);   // shows correct as specified on my keypress; but the key is not posted on the edit box

end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Edit1.SetFocus;

end;

initialization
  {$I unit1.lrs}

end.
« Last Edit: June 29, 2009, 11:16:25 am by Vincent Snijders »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: changing focus on a form
« Reply #10 on: June 29, 2009, 11:16:16 am »
Quote
Basically the keypress are not posted on the edit entry
Well... that's because you're calling the form's OnKeyPress instead of the edit's one. You can try the following code (untested):
Code: [Select]
procedure TForm1.Btn1Click(Sender: TObject);
var
  c: Char;
begin
  c:='1';
  Edit1.OnKeyPress(Edit1,c);
end;

procedure TForm1.Btn2Click(Sender: TObject);
var
  c: Char;
begin
  c:='2';
  Edit1.OnKeyPress(Edit1,c);
end;

procedure TForm1.Btn3Click(Sender: TObject);
var
  c: Char;
begin
  c:='3';
  Edit1.OnKeyPress(Edit1,c);
end;
Quote
another thing is how to know where is the focus entry?
You can get it from ActiveControl property.

bayparinc

  • New Member
  • *
  • Posts: 16
Re: changing focus on a form
« Reply #11 on: June 30, 2009, 02:26:30 am »
the code also failed, it gives me "External: SIGSEGV" and exception error "RunError(216)".

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: changing focus on a form
« Reply #12 on: June 30, 2009, 11:58:18 am »
Quote
the code also failed, it gives me "External: SIGSEGV" and exception error "RunError(216)"
OK, I thought I did it wrong, the Edit's OnKeyPress must be nil because it's not set. That's the cause of GPF (i.e. RTE 216). Try this (still untested):
Code: [Select]
procedure TForm1.Btn1Click(Sender: TObject);
begin
  if ActiveControl is TEdit then
    ActiveControl.Text:=ActiveControl.Text+'1';
end;

bayparinc

  • New Member
  • *
  • Posts: 16
Re: changing focus on a form
« Reply #13 on: July 01, 2009, 02:19:39 am »
I got error during complilation, cause the text is not a property of active control
["identifier indents no member "text"]

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
Re: changing focus on a form
« Reply #14 on: July 01, 2009, 08:44:11 am »
I guess Leledumbo meant:
Code: [Select]
TEdit(ActiveControl).Text:=TEdit(ActiveControl).Text+'1';

 

TinyPortal © 2005-2018