LISTBOX ISSUE

aj85
aj85 used Ask the Experts™
on
I have a listbox gets populated from a text file.  When a user selects the items from the populated listbox, it changes a panel controls caption.  The problem is that the listbox has 2 columns of text, the primary text message and a description of the message that is being sent to the panel.  When I select row 1 or 2 both the primary message and description are posted to the panel.  I only want column 1 to post to the panel when the user selects a row.  How do I fix this issue?


Please see example below:

First line of data in Listbox --> Column 1: Open Column 2: Changes message on panel to "Open".

Second line of data in listbox --> Column 1: Closed - Column 2: Changes message on panel to "Closed".

 

Thanks,
aj85
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
post your code here for fast help
Ferruccio AccalaiSenior developer, analyst and customer assistance

Commented:
procedure TForm1.ListBox1Click(Sender: TObject);
begin
  Panel1.Caption := Copy(ListBox1.Items[ListBox1.ItemIndex], 1, pos(^I, ListBox1.Items[ListBox1.ItemIndex])-1);
end;

If you need exactly the quoted string "Open" or "Close" just use

procedure TForm1.ListBox1Click(Sender: TObject);
begin
  Panel1.Caption := QuotedStr(Copy(ListBox1.Items[ListBox1.ItemIndex], 1, pos(^I, ListBox1.Items[ListBox1.ItemIndex])-1));
end;

Author

Commented:
Hello,

Here is the code I am currently using.  Before I open the form with the listbox I populate it with a text file.

Listbox1.Items.LoadFromFile('C:\lgr.txt');

The text file is populated with the text I am using, however it seems that all of the date is being loaded into column 1 of the listbox, so when I select a row, all of the information is passed to the panel.  I guess what I really want to know also is how to load the listbox with only the data I want in column 1, then column 2, from the text file.

begin
  case frmMain.I of

  1: frmMain.Panel2.caption := ListBox1.Items[ListBox1.ItemIndex];
  2: frmMain.Panel3.caption := ListBox1.Items[ListBox1.ItemIndex];
  3: frmMain.Panel4.caption := ListBox1.Items[ListBox1.ItemIndex];

end;

This code assigns the text from the listbox to the caption of the panel that has been selected, but it assigns all of the values in that listbox row.  I only want the values in the first column when clicked.

Thanks in advance,
aj85
Ferruccio AccalaiSenior developer, analyst and customer assistance

Commented:
To add the text in multicolumn listbox you have to add it using ^I as separator
for example:
ListBox1.items.add('Open'^I'Changes message on panel to "Open".');

You have to set the TabWidth to the value you want to separate the columns

Then, using the code provided before you can get just the first column text

So instead of loading just your strings from the file you have to populate the listbox one by one
If you show us how the file lgr.txt is structured we could help more on solving this

Author

Commented:
Hello,

The file is structured as follows:

Col1 | Col2
Open - this sends open to the panel.
Closed - this ends closed to the panel.

Column 1 is the text that should be sent column 2 is just a description for the user.  Let me know if this is what you were asking for.
Thanks,aj85
Senior developer, analyst and customer assistance
Commented:
Is any column exactly divided by ' - '?
If so then take a look at this example

procedure TForm2.Button1Click(Sender: TObject);
begin
  ListBox1.Columns := 2;
  ListBox1.TabWidth := 5;
  ListBox1.Items.LoadFromFile('c:\lgr.txt');
  ListBox1.Items.Text := StringReplace(ListBox1.Items.Text, ' - ', ^I, [rfReplaceall]);
end;

procedure TForm2.ListBox1Click(Sender: TObject);
begin
  Panel1.Caption := Copy(ListBox1.Items[ListBox1.ItemIndex], 1, pos(^I, ListBox1.Items[ListBox1.ItemIndex])-1);
end;

Author

Commented:
Thanks it worked very well, and was easy to follow.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial