Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

SearchReplace selected items

Title: Search/Replace selected items Question: How can I search for a string in (eg) a tListBox and then only have repolaced the selected lines/items changed? Answer: Here's a project that shows how to search for a string in a StringList, have those put into a tCheckListBox and then have the user select which of those found lines/items must be replaced.You can also learn how to set/chang/use flags as the programm runs. I am using a TabbedNotebook; on the 1st page, I put the ListBox with some strings and on the 2nd. a tCheckListBox; you need two checkboxes (for the flags, two bitbtns and two TEdits). I entered it with Delphi 5. If you want the project, just e-mail me...... Omer Yasar Can omercan@home.nl unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, rsStorage, rsBinData, ComCtrls, rsIniData, ExtCtrls, Tabnotbk, CheckLst; type TForm1 = class(TForm) rsStorage1: TrsStorage; BitBtn_SearchAndReplaceAndSave: TBitBtn; StatusBar1: TStatusBar; rsIniData1: TrsIniData; Edit1: TEdit; Panel1: TPanel; Edit2: TEdit; TabbedNotebook1: TTabbedNotebook; CheckListBox1: TCheckListBox; ListBox1: TListBox; BitBtn_ReplaceFound: TBitBtn; GroupBox1: TGroupBox; CheckBox_IgnoreCare: TCheckBox; CheckBox_ReplaceAll: TCheckBox; Memo1: TMemo; procedure BitBtn_SearchAndReplaceAndSaveClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure BitBtn_ReplaceFoundClick(Sender: TObject); procedure CheckBox_IgnoreCareClick(Sender: TObject); procedure CheckBox_ReplaceAllClick(Sender: TObject); private { Private-Deklarationen } public { Public-Deklarationen } end; var Form1: TForm1; FoundIndex : array [1..30] of integer; //max-number of lines mFlags: TReplaceFlags; implementation {$R *.DFM} procedure TForm1.BitBtn_SearchAndReplaceAndSaveClick(Sender: TObject); var i,j: integer; foundstr: string; begin CheckListBox1.Clear;j:=0; for i:=0 to ListBox1.Items.count-1 do begin foundstr:=(StringReplace(ListBox1.Items[i],Edit1.text,Edit2.text,mFlags)); // use flags (see Delphi's help: StringReplace (Funktion)) if foundstr ListBox1.Items[i] then begin CheckListBox1.Items.Add(foundstr); FoundIndex[i] := j; inc(j); end; end; TabbedNotebook1.PageIndex:=1; end; procedure TForm1.BitBtn_ReplaceFoundClick(Sender: TObject); var i: integer; begin for i:=0 to CheckListBox1.Items.count-1 do if CheckListBox1.checked[i] then ListBox1.Items[FoundIndex[i]]:=CheckListBox1.Items[i]; TabbedNotebook1.PageIndex := 0; end; //============================================================================= procedure TForm1.FormCreate(Sender: TObject); begin BitBtn_ReplaceFound.caption := 'now chack the lines you want to' + chr(13) + 'replance and click on this BitBtn'; end; //============================================================================= procedure TForm1.CheckBox_IgnoreCareClick(Sender: TObject); begin // change/set flag (see Delphi's help: StringReplace (Funktion)) if CheckBox_IgnoreCare.checked then mFlags := mFlags + [rfIgnoreCase] else mFlags := mFlags - [rfIgnoreCase]; end; procedure TForm1.CheckBox_ReplaceAllClick(Sender: TObject); begin // change/set flag (see Delphi's help: StringReplace (Funktion)) if CheckBox_ReplaceAll.checked then mFlags := mFlags + [rfReplaceAll] else mFlags := mFlags - [rfReplaceAll]; end; end.