21. november 2002 - 09:34Der er
4 kommentarer og 2 løsninger
tekst editor
jeg er ved at lave en teksteditor (i delphi4) men har nogle spm
1: hvordan gør jeg min memo ren for tekst (når programmet køre) filer>ny (hvad er koden) 2: hvordan kan jeg søge i teksten med en find dialog1 3: hvordan får jeg gem til at virke så at den gemmer til den fil der er åbn
procedure TForm2.FindDialog1Find(Sender: TObject); var S: String; startpos: Integer; begin with TFindDialog(Sender) do begin {If the stored position is 0 this cannot be a find next. } If FSelPos = 0 Then Options := Options - [frFindNext];
{ Figure out where to start the search and get the corresponding text from the memo. } If frfindNext In Options Then Begin { This is a find next, start after the end of the last found word. } StartPos := FSelPos + Length( Findtext ) ; S:= Copy( Memo1.Lines.Text, StartPos, MaxInt ); End Else Begin { This is a find first, start at the, well, start. } S:= Memo1.Lines.Text; StartPos := 1; End; { Perform a non-global case-sensitive search for FindText in S } FSelPos := Pos(AnsiLowerCase(FindText), AnsiLowerCase(S)); if FSelPos > 0 then begin { Found something, correct position for the location of the start of search. } FSelPos := FSelPos + StartPos - 1; Memo1.SelStart := FSelPos - 1; Memo1.SelLength := Length(FindText); end else begin { No joy, show a message. } If frfindNext In Options Then begin S:= Concat('Slutningen af teksten er nået.'); FindDialog1.CloseDialog; end Else S:= Concat('Kunne ikke finde "', FindText); MessageDlg(S, mtError, [mbOk], 0); end; end; end;
I nr. 2 kan det godt være du bliver nødt til at lave hele koden selv, men alternativt kan du bruge en TRichEdit istedet. Hvis du kikker i hjælpen til denne under methods->FindText er det et eksempel til hvordan du kan bruge en TFindDialog.
This example requires a TRichEdit, a TButton, and a TFindDialog. Clicking the button click will display a Find Dialog to the right of the edit control. Filling in the "Find what" text and pressing the Find Next button will select the first matching string in the Rich Edit control that follows the previous selection.
procedure TForm1.Button1Click(Sender: TObject);
begin FindDialog1.Position := Point(RichEdit1.Left + RichEdit1.Width, RichEdit1.Top); FindDialog1.Execute; end;
procedure TForm1.FindDialog1Find(Sender: TObject); var FoundAt: LongInt; StartPos, ToEnd: Integer; begin with RichEdit1 do begin { begin the search after the current selection if there is one } { otherwise, begin at the start of the text } if SelLength <> 0 then
StartPos := SelStart + SelLength else
StartPos := 0;
{ ToEnd is the length from StartPos to the end of the text in the rich edit control }
ToEnd := Length(Text) - StartPos;
FoundAt := FindText(FindDialog1.FindText, StartPos, ToEnd, [stMatchCase]); if FoundAt <> -1 then begin SetFocus; SelStart := FoundAt; SelLength := Length(FindDialog1.FindText); end; end; end;
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.