Einzelnen Beitrag anzeigen

tcoman
(Gast)

n/a Beiträge
 
#10

AW: TEdit Cursorposition verwenden

  Alt 1. Aug 2016, 15:33
Leider fuegt mir mein Programm den Text am Ende des Textes an,
Zeig mal, was du da machst.
Nach reichlich probieren habe ich folgendes produziert.

Scheitert weiterhin daran, dass es mir nicht gelingt
die CursorPosition des TEdit Feldes zu bestimmen...

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;
    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 }
    prvCursorPosition : integer;
  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 - Test Example for wwww.delphipraxis.net';
{-}
Application.Title:='PEnhInpText';
Application.HintHidePause:=30000;
Application.HintPause:=0;
{-}
{-}
prvCursorPosition:=0;
{-}
{-}
ListBox1.SetBounds(4, 4, 300, 600-30);
ListBox1.MultiSelect:=False;
ListBox1.Sorted:=False;
ListBox1.ShowHint:=True;
ListBox1.Hint:='ListBox';
ListBox1.TabOrder:=2;
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-3*26, 300-4, 3*26);
Label1.AutoSize:=False;
Label1.Caption:='CursorPosition: '+IntToStr(prvCursorPosition)+#13+
                'Xmousedown: 0'+#13+
                'Xcanvas: 0';
{-}
{-}
Edit1.SetBounds(300+8, 300, 300-4, 26);
Edit1.ShowHint:=True;
Edit1.Hint:='TEdit';
Edit1.AutoSize:=False;
Edit1.TabOrder:=1;
Edit1.Color:=clMoneyGreen;
Edit1.Text:='and is Three';
{-}
{-}
Label2.SetBounds(300+8, 300+26, 300-4, 300-26-26);
Label2.AutoSize:=False;
Label2.Caption:='Click with mouse into TEdit calculates'+#13+
                'the CursorPosition.'+#13+
                #13+
                'Use Keys Pos1, Left, Right, End'+#13+
                'gives CursorPosition.'+#13+
                #13+
                'Click into TListBox to select'+#13+
                'a line and insert this text into'+#13+
                'text of TEdit at CursorPosition.'+#13+
                #13+
                'Move new CursorPosition 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);
var i, xcanvas : integer;

begin
{-}
{-!!! here are my troubles. Need an idea, how to calculate-}
{-!!! the correct CursorPosition. Work with Canvas formular-}
{-}
for i:=1 to Length(Edit1.Text)+1 do begin
    xcanvas:=Canvas.TextWidth(copy(Edit1.Text+' ', 1, i));
    if (X < xcanvas) then begin
       prvCursorPosition:=i-1;
       break;
    end;
end;
{-}
Label1.Caption:='CursorPosition: '+IntToStr(prvCursorPosition)+#13+
                'Xmousedown: '+IntToStr(X)+#13+
                'Xcanvas: '+IntToStr(Xcanvas);
{-}
end;

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

procedure TPEnhInpText.Edit1KeyUp(Sender: TObject; var Key: Word;
                                  Shift: TShiftState);
begin
{-}
if (Key = VK_HOME) then begin
   Key:=0;
   prvCursorPosition:=0;
   Label1.Caption:='CursorPosition: '+IntToStr(prvCursorPosition);
end;
{-}
if ((Key = VK_LEFT) or (Key = VK_BACK)) then begin
   Key:=0;
   dec(prvCursorPosition);
   if (prvCursorPosition < 0) then prvCursorPosition:=0;
   Label1.Caption:='CursorPosition: '+IntToStr(prvCursorPosition);
end;
{-}
if (Key = VK_RIGHT) then begin
   Key:=0;
   inc(prvCursorPosition);
   if (prvCursorPosition > Length(Edit1.Text)) then prvCursorPosition:=Length(Edit1.Text);
   Label1.Caption:='CursorPosition: '+IntToStr(prvCursorPosition);
end;
{-}
if (Key = VK_END) then begin
   Key:=0;
   prvCursorPosition:=Length(Edit1.Text);
   Label1.Caption:='CursorPosition: '+IntToStr(prvCursorPosition);
end;
{-}
end;

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

procedure TPEnhInpText.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var sListBox,
    sEdit : string;
begin
{-}
sListBox:=ListBox1.Items.Strings[ListBox1.ItemIndex];
sEdit:=Edit1.Text;
Insert(sListBox, sEdit, prvCursorPosition);
Edit1.Text:=sEdit;
inc(prvCursorPosition, Length(sListBox));
Label1.Caption:='CursorPosition: '+IntToStr(prvCursorPosition);
{-}
end;

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

end.
MfG,
Terence
Miniaturansicht angehängter Grafiken
penhinptext.jpg  
Angehängte Dateien
Dateityp: zip PEnhInpText-Test.zip (228,1 KB, 2x aufgerufen)

Geändert von tcoman ( 1. Aug 2016 um 15:38 Uhr)
  Mit Zitat antworten Zitat