Einzelnen Beitrag anzeigen

tcoman
(Gast)

n/a Beiträge
 
#18

AW: TEdit Cursorposition verwenden

  Alt 3. Aug 2016, 15:51
Final version of this problem.
Thanks to you all for minimize my time.
Delphi-Quellcode:
unit PEnhInpText_Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons;

type
  TPEnhInpText = class(TForm)
    Edit1: TEdit;
    ListBox1: TListBox;
    bExit: TBitBtn;
    Label1: TLabel;
    Label2: TLabel;
    Edit2: TEdit;
    Label3: TLabel;
    procedure ListBox1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Edit2KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure Edit2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Edit2KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
                                Shift: TShiftState; X, Y: Integer);
    procedure Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure Edit1MouseDown(Sender: TObject; Button: TMouseButton;
                             Shift: TShiftState; X, Y: Integer);
    procedure bExitClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
    prvCaretPosition1, prvCaretPosition2 : integer;
    prvEdit1Focused : boolean;
  public
    { Public-Deklarationen }
  end;

var
  PEnhInpText: TPEnhInpText;

implementation

{$R *.dfm}

{------------------------------------------------------------------------------}

procedure TPEnhInpText.FormCreate(Sender: TObject);
begin
{-}
KeyPreview:=False;
BorderStyle:=bsSingle;
FormStyle:=fsNormal;
ClientWidth:=600+8;
ClientHeight:=600+8;
Color:=clSkyBlue;
Font.Name:='Tahoma';
Font.Size:=10;
Position:=poDesktopCenter;
Caption:='PEnhInpText - Working Test Example for wwww.delphipraxis.net';
{-}
Application.Title:='PEnhInpText';
Application.HintHidePause:=30000;
Application.HintPause:=0;
{-}
{-}
prvCaretPosition1:=0;
prvCaretPosition2:=0;
prvEdit1Focused:=True;
{-}
{-}
ListBox1.SetBounds(4, 4, 300, 600-30);
ListBox1.MultiSelect:=False;
ListBox1.Sorted:=False;
ListBox1.ShowHint:=True;
ListBox1.Hint:='ListBox';
ListBox1.TabOrder:=3;
ListBox1.Color:=clMoneyGreen;
ListBox1.Clear;
ListBox1.Items.Add('One ');
ListBox1.Items.Add('Two ');
ListBox1.Items.Add('Three ');
ListBox1.Items.Add('Four ');
ListBox1.Items.Add('Five ');
{-}
{-}
Label1.SetBounds(300+8, 300-6*26, 300-4, 26);
Label1.AutoSize:=False;
Label1.Caption:='CaretPosition of Edit1: 0';
{-}
{-}
Edit1.SetBounds(300+8, 300-5*26, 300-4, 26);
Edit1.ShowHint:=True;
Edit1.Hint:='Edit1';
Edit1.AutoSize:=False;
Edit1.TabOrder:=1;
Edit1.Color:=clSilver;
Edit1.Text:='and is Three';
Edit1.SelStart:=0;
Edit1.SelLength:=0;
Edit1.SelText:='';
{-}
{-}
Label2.SetBounds(300+8, 300-4*26, 300-4, 26);
Label2.AutoSize:=False;
Label2.Caption:='CaretPosition of Edit2: 0';
{-}
{-}
Edit2.SetBounds(300+8, 300-3*26, 300-4, 26);
Edit2.ShowHint:=True;
Edit2.Hint:='Edit2';
Edit2.AutoSize:=False;
Edit2.TabOrder:=2;
Edit2.Color:=clMoneyGreen;
Edit2.Text:='and is Three';
Edit2.SelStart:=0;
Edit2.SelLength:=0;
Edit2.SelText:='';
{-}
{-}
Label3.SetBounds(300+8, 300-26, 300-4, 300);
Label3.AutoSize:=False;
Label3.Color:=clGreen;
Label3.Caption:='Focused Edit is Silver else MoneyGreen.'+#13+
                #13+
                'Click with mouse into Edit calculates'+#13+
                'the CaretPosition and sets Focus.'+#13+
                #13+
                'Use Keys Pos1, Left, Right, End'+#13+
                'gives CaretPosition.'+#13+
                #13+
                'Use Keys Up, Down swaps between Edit1 + 2.'+#13+
                #13+
                'Click into ListBox to select'+#13+
                'a line and insert this text into'+#13+
                'text of focused Edit at CaretPosition.'+#13+
                'Enter ListBox line does the same.'+#13+
                #13+
                'Move new CaretPosition after inserted'+#13+
                'text.';
{-}
{-}
bExit.SetBounds(4, 600-22, 64, 26);
bExit.Caption:='E&xit';
bExit.TabOrder:=0;
{-}
{-}
end;

{------------------------------------------------------------------------------}

procedure TPEnhInpText.bExitClick(Sender: TObject);
begin
Close;
end;

{------------------------------------------------------------------------------}

procedure TPEnhInpText.Edit1MouseDown(Sender: TObject; Button: TMouseButton;
                                      Shift: TShiftState; X, Y: Integer);
begin
{-}
prvEdit1Focused:=True;
Edit1.Color:=clSilver;
Edit2.Color:=clMoneyGreen;
prvCaretPosition1:=Edit1.SelStart;
Label1.Caption:='CaretPosition of Edit1: '+IntToStr(Edit1.SelStart);
{-}
end;

{------------------------------------------------------------------------------}

procedure TPEnhInpText.Edit1KeyDown(Sender: TObject; var Key: Word;
                                    Shift: TShiftState);
begin
{-}
if ((Key = VK_UP) or (Key = VK_DOWN)) then begin
   Key:=0;
   prvEdit1Focused:=False;
   Edit2.SetFocus;
   Edit2.SelLength:=0;
   Edit2.SelStart:=prvCaretPosition2;
   Edit1.Color:=clMoneyGreen;
   Edit2.Color:=clSilver;
end;
{-}
end;

{------------------------------------------------------------------------------}

procedure TPEnhInpText.Edit1KeyUp(Sender: TObject; var Key: Word;
                                  Shift: TShiftState);
begin
{-}
if ((Key = VK_HOME) or (Key = VK_LEFT) or (Key = VK_BACK) or
    (Key = VK_RIGHT) or (Key = VK_END)) then begin
   Key:=0;
end;
{-}
prvCaretPosition1:=Edit1.SelStart;
Label1.Caption:='CaretPosition of Edit1: '+IntToStr(Edit1.SelStart);
{-}
end;

{------------------------------------------------------------------------------}

procedure TPEnhInpText.Edit2KeyDown(Sender: TObject; var Key: Word;
                                    Shift: TShiftState);
begin
{-}
if ((Key = VK_UP) or (Key = VK_DOWN)) then begin
   Key:=0;
   prvEdit1Focused:=True;
   Edit1.SetFocus;
   Edit1.SelLength:=0;
   Edit1.SelStart:=prvCaretPosition1;
   Edit1.Color:=clSilver;
   Edit2.Color:=clMoneyGreen;
end;
{-}
end;

{------------------------------------------------------------------------------}

procedure TPEnhInpText.Edit2KeyUp(Sender: TObject; var Key: Word;
                                  Shift: TShiftState);
begin
{-}
if ((Key = VK_HOME) or (Key = VK_LEFT) or (Key = VK_BACK) or
    (Key = VK_RIGHT) or (Key = VK_END)) then begin
   Key:=0;
end;
{-}
prvCaretPosition2:=Edit2.SelStart;
Label2.Caption:='CaretPosition of Edit2: '+IntToStr(Edit2.SelStart);
{-}
end;

{------------------------------------------------------------------------------}

procedure TPEnhInpText.Edit2MouseDown(Sender: TObject; Button: TMouseButton;
                                      Shift: TShiftState; X, Y: Integer);
begin
{-}
prvEdit1Focused:=False;
Edit1.Color:=clMoneyGreen;
Edit2.Color:=clSilver;
prvCaretPosition2:=Edit2.SelStart;
Label2.Caption:='CaretPosition of Edit2: '+IntToStr(Edit2.SelStart);
{-}
end;

{------------------------------------------------------------------------------}

procedure TPEnhInpText.ListBox1KeyDown(Sender: TObject; var Key: Word;
                                       Shift: TShiftState);
begin
{-}
if (Key = VK_RETURN) then begin
   Key:=0;
   if prvEdit1Focused then begin
      Edit1.SelLength:=0;
      Edit1.SelText:=ListBox1.Items.Strings[ListBox1.ItemIndex];
      Label1.Caption:='CaretPosition of Edit1: '+IntToStr(Edit1.SelStart);
   end else begin
      Edit2.SelLength:=0;
      Edit2.SelText:=ListBox1.Items.Strings[ListBox1.ItemIndex];
      Label2.Caption:='CaretPosition of Edit2: '+IntToStr(Edit2.SelStart);
   end;
end;
{-}
end;

{------------------------------------------------------------------------------}

procedure TPEnhInpText.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
                                         Shift: TShiftState; X, Y: Integer);
begin
{-}
if prvEdit1Focused then begin
   Edit1.SelLength:=0;
   Edit1.SelText:=ListBox1.Items.Strings[ListBox1.ItemIndex];
   Label1.Caption:='CaretPosition of Edit1: '+IntToStr(Edit1.SelStart);
end else begin
   Edit2.SelLength:=0;
   Edit2.SelText:=ListBox1.Items.Strings[ListBox1.ItemIndex];
   Label2.Caption:='CaretPosition of Edit2: '+IntToStr(Edit2.SelStart);
end;
{-}
end;

{------------------------------------------------------------------------------}

end.
Wbw,
Terence
Miniaturansicht angehängter Grafiken
penhinptext-test_2016_08_03.jpg  
Angehängte Dateien
Dateityp: zip PEnhInpText-Test_2016_08_03.zip (228,9 KB, 5x aufgerufen)
  Mit Zitat antworten Zitat