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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding to a Tcombobox 1

Status
Not open for further replies.

pierrotsc

Programmer
Nov 25, 2007
358
US
Before I posted this question, i did a search in the forum but could not find the same topic.
I want to be able to read a text file from my tcombobox. Like that, if i want to add more items to my list, i just have to edit the text file.
How would I do that?
Thanks.
PO
 
You might want to try putting something like this in your code wherever you're trying to do that. (Form.show, ButtonClick, etc.)

Code:
var
  InFile: TextFile;
  path: string
  filename: string;
  line: string;
begin
  cmbxYourComboBox.Items.Clear;
  path := 'Path\To\Your\File\'
  filename := 'Your_File.ext';
  AssignFile(InFile, path + filename);
  Reset(InFile);
  try
    while not eof(InFile) do
    begin
      Readln(InFile, line);
      cmbxYourComboBox.Items.Add(line)
    end;
  finally
    CloseFile(InFile);
  end;
end;

This will clear out whatever is currently in the ComboBox component, then reload it from scratch with the contents of the text file. Hope this helps.
 
Hi,
while CHeighlund's example will perfectly do the job, I will show you a more VCL oriented example:

Code:
Uses Classes, SysUtils,...;
....
procedure UpdateCombo(Combo : TComboBox; Filename : string);

var StringList : TStringlist;

begin
 StringList := TStringList.Create;
 try
  StringList.LoadFromFile(Filename);
  Combo.Items.Assign(StringList);
 finally
  FreeAndNil(StringList);
 end;
end;

//call like this

 UpdateCombo(ComboBox1, 'c:\test.txt');

nice, clean and small!

Cheers,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
nicer, cleaner and smaller is
Code:
procedure UpdateCombo(Combo : TComboBox; Filename : string);
begin
  Combo.Items.LoadFromFile( Filename );
end;

Andrew
Hampshire, UK
 
Lol Towerbase,

A+

obvious one :)

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
star for the smallest solution :)

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Thanks, guys. The real credit should go to the smart people at Borland / Codegear (and, dare I say it, Microsoft) who designed the VCL and controls.

Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top