Link to home
Start Free TrialLog in
Avatar of aj85
aj85

asked on

LISTBOX ISSUE

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
Avatar of karunamoorthy
karunamoorthy
Flag of India image

post your code here for fast help
Avatar of Ferruccio Accalai
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;
Avatar of aj85
aj85

ASKER

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
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
Avatar of aj85

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aj85

ASKER

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