I want to check if an item has been selected in ListBox when user click on label
if i execute like that i got this error list index out of bounds
procedure TfrMain.Label15Click(Sender: TObject);
var
saveDialog : TSaveDialog;
FileContents: TStringStream;
saveLine,Selected : String;
begin
saveDialog := TSaveDialog.Create(self);
saveDialog.Title := 'Save your text or word file';
saveDialog.InitialDir := GetCurrentDir;
saveDialog.Filter := 'text file|*.txt';
saveDialog.DefaultExt := 'txt';
saveDialog.FilterIndex := 1;
Selected := ListBox1.Items.Strings[ListBox1.ItemIndex];
if Selected <> '' then
begin
if saveDialog.Execute then
begin
FileContents := TStringStream.Create('', TEncoding.UTF8);
FileContents.LoadFromFile(ListBox1.Items.Strings[ListBox1.ItemIndex]);
FileContents.SaveToFile(saveDialog.Filename);
ShowMessage('File : '+saveDialog.FileName)
end
else ShowMessage('Save file was not succesful');
saveDialog.Free;
end;
end;
you can determine the selected item(s) in the ListBox control by enumerating the Items collection and testing the Selected value for each ListItem element. Save this answer.
ListBox control has a GetItemText which helps you to get the item text regardless of the type of object you added as item. It really needs such GetItemValue method. Using above method you don't need to worry about settings of ListBox and it will return expected Value for an item.
The ListBox represents a Windows control to display a list of items to a user. A user can select an item from the list. It allows the programmer to add items at design time by using the properties window or at the runtime.
This code
if Selected then
will not compile because Selected
is a string. I guess you were experimenting just before posting this.
All the same you error messages and the question title suggests that ListBox1.ItemIndex
is equal to -1. Hence the list index out of bounds error.
You need to add a check that ListBox1.ItemIndex
is not -1 before reading from the list box. ItemIndex=-1
is the way you detect that no item is selected. Your code should therefore look like this:
.....
saveDialog.DefaultExt := 'txt';
saveDialog.FilterIndex := 1;
if ListBox1.ItemIndex <> -1 then
begin
.....
This occurs if nothing is selected in the list box.
Try Using:
procedure TfrMain.Label15Click(Sender: TObject);
var
saveDialog : TSaveDialog;
FileContents: TStringStream;
saveLine,Selected : String;
begin
saveDialog := TSaveDialog.Create(self);
saveDialog.Title := 'Save your text or word file';
saveDialog.InitialDir := GetCurrentDir;
saveDialog.Filter := 'text file|*.txt';
saveDialog.DefaultExt := 'txt';
saveDialog.FilterIndex := 1;
if ListBox1.ItemIndex >= 0 then
begin
Selected := ListBox1.Items.Strings[ListBox1.ItemIndex]
if saveDialog.Execute then
begin
FileContents := TStringStream.Create('', TEncoding.UTF8);
FileContents.LoadFromFile(Selected);
FileContents.SaveToFile(saveDialog.Filename);
ShowMessage('File : '+saveDialog.FileName)
end
else ShowMessage('Save file was not succesful');
saveDialog.Free;
end;
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With