Re: Editing strings in a file using pointers?

Posted by Johan on April 04, 2005

In Reply to: Editing strings in a file using pointers? posted by Scott

I have a file storing records of special offers.
Offers_Rec=record
OfferName : string[20];
Discount : integer;
How do i:
Get a drop down box to list just the OfferNames that are in the file?
When an OfferName is selected from the dropdown, it is displayed in an editbox besides the dropdown so i can edit the name and replace the existing name in the file?
Get the discount relating to that specific Offername displayed in another edit box which also allows me to edit and replace the existing value in the file?
There are multiple records stored in the file, so im assuming i have to use pointers? If anyone can help me code this i'd appreciate it!

I think it's easier if you use a text file to save offernames and discounts, 1 per line, like this:

t-shirts:10
guitars:8
strings for guitars:5

You can read the textfile into the combobox like this:

  ComboBox1.Items.LoadFromFile('c:\offers.txt');
  ComboBox1.ItemIndex := 0; 

When an item is clicked:

procedure TForm1.ComboBox1Click(Sender: TObject);
var
  S: string;
  P: integer; // position of separator
begin
  S := ComboBox1.Items[ComboBox1.ItemIndex];
  P := Pos(':', S);
  Edit1.Text := Copy(S, 1, P - 1);
  Edit2.Text := Copy(S, P + 1, Length(S) - P);
end;

After modifying the edit-boxes, put item back at the same place:

procedure TForm1.Button1Click(Sender: TObject);
var
  N: integer;
  S: string;
begin
  N := ComboBox1.ItemIndex;
  S := Edit1.Text + ':' + Edit2.Text;
  ComboBox1.Items[N] := S;
  ComboBox1.ItemIndex := N;
end;

At the end, save combobox items back to text file:

  ComboBox1.Items.SaveToFile('c:\offers.txt');

 

Related Articles and Replies