Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if an item has been selected in ListBox

Tags:

listbox

delphi

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;
like image 578
Аймен Ахмед Avatar asked Apr 20 '12 19:04

Аймен Ахмед


People also ask

How to check if a ListBox has a selected item c#?

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.

Which function is used to fetch a value based on the number returned by a ListBox control?

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.

What is ListBox in VB?

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.


2 Answers

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
.....
like image 92
David Heffernan Avatar answered Sep 16 '22 12:09

David Heffernan


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;
like image 28
Vahid Nasehi Avatar answered Sep 16 '22 12:09

Vahid Nasehi