Components

Using Splitter component

Posted in Components

This example shows, how to use Splitter component. It allows to get your application in good design style. Put one component to the form and set Align property to alLeft. Put Splitter to the form. Put other component and set Align property to AlClient value. Splitter has MinSize property. If the Align property is alLeft, the splitter can't resize the regions to its left any smaller than MinSize pixels.

procedure TForm1.Button1Click(Sender: TObject);
begin
  Splitter1.MinSize:=300;
end;

Use Edit component for password

Posted in Components

First of all, change PasswordChar property of Edit component that nobody saw your password, when you type it. In this example, length of password must be only 4 symbols.

procedure TForm1.Edit1Change(Sender: TObject);
begin
  with Sender as TEdit do
    if Length(Text)>4 then
    begin
      Edit1.Text:='';
      MessageBeep(0);
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  MessageBox(
    Form1.Handle, 
    PChar('You have just written - '+Edit1.Text),
    'Password checking',
    1);
end;

Undo the last operation in Edit

Posted in Components

You may use Perform method with EM_UNDO message for undo operation, and you can use EM_CANUNDO message to determine whether an edit operation can be undone.

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Perform(EM_UNDO, 0, 0);
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
  if Edit1.Perform(EM_CANUNDO, 0, 0)<>0 then
    Button1.Enabled:=True;
end;

Text positioning in Memo

Posted in Components

We will find spaces and tab symbols, when somebody press Enter-key. And we will add this symbols to the new string in Memo.

procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  LineNum: Integer;
  Str: string;
  i: Integer;
begin
  StrDop:='';
  if Key=13 then
  begin
    LineNum:=Memo1.Perform(EM_LINEFROMCHAR, Memo1.SelStart, 0);
    Str:=Memo1.Lines[LineNum];
    i:=1;
    if Str<>'' then
    begin
      while (Str[i]=' ')or(Str[i]=#9) do
        Inc(i);
      StrDop:=Copy(Str,1,i-1);
    end;
  end;
end;

procedure TForm1.Memo1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
  LineNum: Integer;
begin
  if StrDop<>'' then
  begin
    LineNum:=Memo1.Perform(EM_LINEFROMCHAR, Memo1.SelStart, 0);
    Memo1.Lines[LineNum]:=Memo1.Lines[LineNum]+StrDop;
  end;
end;