RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TWinControl.SetFocus Method

Gives the input focus to the control.

Pascal
procedure SetFocus; virtual;
C++
virtual __fastcall SetFocus();

Use SetFocus to change input focus to the control. When a control has focus, it receives keyboard events.  

C++ Examples: 

 

/*
The following OnFind event handler searches a memo component
for the text specified in the FindText property of a find
dialog component. If found, the first occurrence of the text
in Memo1 is selected. The code uses the Pos function to
compare strings, and stores the number of characters to skip
when determining the selection position in the SkipChars
variable. Because there is no handling of case, whole word,
or search direction in this algorithm, it is assumed that
the Options property of FindDialog1 is set to
[frHideMatchCase, frHideWholeWord, frHideUpDown].
*/
void __fastcall TForm1::FindDialog1Find(TObject *Sender)
{
  for (int I = 0; I < Memo1->Lines->Count; I++)
  {
    int PosReturn = Memo1->Lines->Strings[I].Pos(FindDialog1->FindText);
    if (PosReturn) //found!
    {
      int Skipchars = 0;
      for (int J = 0; J < I; J++)
        Skipchars += Memo1->Lines->Strings[J].Length();
      Skipchars += I*2; // add CR/LF for all skipped lines
      Skipchars += PosReturn - 1;
      Memo1->SetFocus();
      Memo1->SelStart = Skipchars;
      Memo1->SelLength = FindDialog1->FindText.Length();
      break;
    }
  }
}
/*
The following example demonstrates the use of the Perform method
to send a windows message. The EM_SCROLLCARET message scrolls the
caret in the rich edit into view. The caret position is set to the
contents of the mask edit prior to calling Perform.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    RichEdit1->SelStart = StrToInt(MaskEdit1->Text);
    Edit1->Text = IntToStr(RichEdit1->SelStart);
    RichEdit1->Perform(EM_SCROLLCARET, 0, 0);
    RichEdit1->SetFocus();
}

 

Delphi Examples: 

{
The following OnFind event handler searches a memo component
for the text specified in the FindText property of a find
dialog component. If found, the first occurrence of the text
in Memo1 is selected. The code uses the Pos function to
compare strings, and stores the number of characters to skip
when determining the selection position in the SkipChars
variable. Because there is no handling of case, whole word,
or search direction in this algorithm, it is assumed that
the Options property of FindDialog1 is set to
[frHideMatchCase, frHideWholeWord, frHideUpDown].
} 
procedure TForm1.FindDialog1Find(Sender: TObject);
var
  I, J, PosReturn, SkipChars: Integer;
begin
  for I := 0 to Memo1.Lines.Count do
  begin
    PosReturn := Pos(FindDialog1.FindText,Memo1.Lines[I]);
    if PosReturn <> 0 then {found!}
    begin
      SkipChars := 0;
      for J := 0 to I - 1 do
        SkipChars := SkipChars + Length(Memo1.Lines[J]);
      SkipChars := SkipChars + (I*2);
      SkipChars := SkipChars + PosReturn - 1;
      Memo1.SetFocus;
      Memo1.SelStart := SkipChars;
      Memo1.SelLength := Length(FindDialog1.FindText);
      Break;
    end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
const Path = 'country.list';
begin
  Memo1.Lines.LoadFromFile(Path);
end;
{
The following example demonstrates the use of the Perform method
to send a windows message. The EM_SCROLLCARET message scrolls the
caret in the rich edit into view. The caret position is set to the
contents of the mask edit prior to calling Perform.
}
procedure TForm1.Button1Click(Sender: TObject);
begin
with RichEdit1 do
  Begin
    SelStart := StrToInt(MaskEdit1.Text);
    Edit1.Text := InttoStr(SelStart);
    RichEdit1.Perform(EM_SCROLLCARET, 0, 0);
    RichEdit1.SetFocus;
  end;
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!