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 strongm 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 refresh a combobox? 1

Status
Not open for further replies.

Jayz

ISP
Feb 17, 2002
59
Ok, I've created this combobox which displays a list of excel spreadsheets within a specified directory.
see combobox post here:
thread102-515425
Now, what I want is a refresh button which will refresh this combobox whenever a new excel spreadsheet is added to that directory.

At the moment I have to close my application and re-open it to see the new entry in the combobox.

I actually have 2 comboboxes, so I want both of them to refresh on a click of a single button

hope this makes sense.

Much appreciated,
Jayz
 
Where did you put the code that fills the comboboxes ?
If it is in your FormShow event or CreateForm event than put this code in a private procedure and call it when the form is created or when you press the refresh button.


private
procedure FillComboboxes;
end;


procedure Form1.FormShow(Sender: TObject)
begin
FillComboboxes;
end;

procedure Form1.btnRefreshClick(Sender: TObject)
begin
FillComboboxes;
end;

procedure Form1.FillComboboxes(Sender: TObject)
begin
Combobox1. ....
...
<Put Your code here>
end;
 
Immediately after the code which adds a spreadsheet to your combobox type the following line:
Code:
ComboBox1.Refresh;
Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
Hi Clive, unfortunately there is no code that adds the spreadsheet to the dir if that's what you mean. The user just saves it to this directory and then while the application is open, it should show up in the dropdownlist of the comboBox at the click of a refresh button.

The problem is, at the moment I have to close and re-open the application to see the new file added.

I tried creating a buttton and adding the code but this didn't work also.

procedure TForm1.Button1Click(Sender: TObject);
begin
ComboBox1.Refresh;
end;

any ideas?



 
I've just had an idea, there's probably a better way but something like this will work:
Code:
  FileListBox.Directory := 'c:\';
  FileListBox.Directory := 'c:\directoryIWantToRefresh';

This will basically switch to an alternative directory then switch back again, which cause the files within the directory to be reloaded into the control. Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
I also use that in my ShellTreeView to refresh, but it should have better ways...

ShellTV.Root := 'C:\';
ShellTV.Root := 'C:\WantedRefreshDir';

mha
 
It looks allright TonHu... :)

Have now installed that comp..

but just woundering what is best... to use that comp or just uses:

FileListBox.Directory := 'c:\';
FileListBox.Directory := 'c:\directoryIWantToRefresh';

it is up to one another... I believe

I will give you a star for that.

mha

 
The advantage of reacting to a windows Messsage is, you get to know it just after it has happened ;-) So you don't have to schedule your refresh, and your always up to date...

HTH
TonHu
 
Clive,
I tried your idea but I'm getting an access violation error when I click the button (at runtime).
&quot;Access Violation at address 6F747475. Read of address 6f747475&quot;

This is the code I used:
procedure TForm1.Button1Click(Sender: TObject);
var
FileListBox: TFileListBox;
begin
FileListBox.Directory := 'c:\';
FileListBox.Directory := (ExtractFilePath(Application.ExeName))+'vobs';

end;

Any ideas where I may be going wrong?.

Sorry for the delay in reply as I have been on holiday.

Regards,
Jayz
 
Check that ExtractFilePath(Application.ExeName) returns a path that includes a backslash. Also, I assume that 'vobs' is the name of your directory that you wish to refresh.


Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
All appears to be correct. I even tried just entering the default directory:
FileListBox.Directory := 'c:\';
FileListBox.Directory := 'c:\tasty\vobs';

I still get the error:(
 
Access violation's normally occur when you try and access something that hasn't been created. So it's probably because you haven't created the FileListBox and your trying to access its' property. Have you created the FileListBox elsewhere, either in code (i.e.
Code:
 FileListBox := TFileListBox.Create
) OR by dropping a component on your form?

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
Hi Clive,

The filelistbox is created at runtime as per your code thread102-515425.
I even tried dropping a component on the form and it still comes up with the access violation.

 
Jayz

Your code fragment in your first email dated Apr 19 does not appear to create the FileListBox hence you will get an access violation when it tries to execute
Code:
  FileListBox.Directory := 'c:\';
as the FileListBox variable is not pointing at anything. You need something like
Code:
  FileListBox := TFileListBox.Create ( Form1 );
and other bits of code to make your form the parent of the ListBox.

Andrew
 
Ok, I know this may not be the best way to do this but it has worked for me.
I ended up using a command which basically cleared the contents in the combox then updated the combobox.

procedure TForm1.Button1Click(Sender: TObject);
var
FileListBox: TFileListBox;
index : Integer;
begin
ComboBox1.Items.Clear; //clears the contents in the combobox
FileListBox := TFileListBox.Create(Form1);
FileListBox.Visible := False;
FileListBox.Parent := Form1;
FileListBox.Directory := (ExtractFilePath(Application.ExeName))+'vobs';
FileListBox.Mask := '*.xls';
For index := 0 to FileListBox.Count - 1 do
ComboBox1.Items.Add(FileListBox.Items[index]);
ComboBox1.ItemIndex := 0;
end;

Thank you all for your posts and keep up the good work.

Regards,
Jayz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top