Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I replace text according to a combobox value? 1

Status
Not open for further replies.

Jayz

ISP
Feb 17, 2002
59
Ok, I know this sounds simple but I'm new to Delphi and I hope this will be easy to understand.

I currently have 2 combo boxes with the values 60, 120, 240 in cmbox1 and 0, 10, 12 in cmbox2.

I want to be able to open a txt file and replace certain text in that file with the value from the combo boxes. And then save it as a batch file

Here is my current code:
//=====================
var
L:TStringList;

procedure TForm1.saveClick(Sender: TObject);
begin
L := TStringList.create;
L.loadfromfile('c:\template\btchmultiplex.txt');

L.text := StringReplace(L.text,'%1','160',[rfReplaceAll]);
L.text := StringReplace(L.text,'%2','12',[rfReplaceAll]);

L.savetofile('c:\template\yahoo.bat');
L.free;


end;

//===============================

I want to replace '%1' with a value from cmbobx1
I want to replace '%2' with a value from cmbobx2

Your help would be much appreciated.



 
You've already done most of the work. If you make these functions available:
Code:
var
  sText:string;

procedure TForm1.FormCreate(Sender: TObject);
begin
  sText := 'blah blah %1 blah %2 blah';
end;

function ReplaceParms( Template: string; Parms: array of string ): string;
var
  i:integer;
begin
  Result := Template;
  for i := Low(Parms) to High(Parms) do
    Result := StringReplace(Result,
       '%' + IntToStr(i+1), Parms[i], [rfReplaceAll]);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Edit1.Text := ReplaceParms( sText, [ComboBox1.Text,ComboBox2.Text] );
end;
Then all you need to do is code it this way:
Code:
L.text := ReplaceParms( L.text, [ComboBox1.Text, ComboBox2.Text] );


 
Thanks very much for that zathras. That was just what I was after.

Muxh appreciated,
Jayz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top