Board index » delphi » ListBox Question (Newbie)

ListBox Question (Newbie)

Greetings,
I have two listboxes on a form; each has a vertical scrollbar. I am
trying to understand how to read the selected item. In Delphi, does the
user have to scroll thru listbox list and then select the item in the
listbox before the program recognizes an item has been selected? That
seems to be one step too many; the user should just have to scroll thru
the list then stop at the item of choice.

If there's an event or property for the listboxes that I can use to read
the selection w/o the user having to scroll AND select?

Best,
Bill

 

Re:ListBox Question (Newbie)


You should be able to read the selected item regardless of whether the user
has scrolled the listbox or clicked on it.  Of course, when a list box is
initially created and then populated, there is no "default" selected item,
so either your application or the user must select an item in the list box
as the "selected" item in order for you to recognize it in your code.

To get the currently selected item, use:

var
  SelectedItem : String;
begin
  If (ListBox1.ItemIndex >= 0) Then
      SelectedItem := ListBox1.Items[ListBox1.ItemIndex];

Of couse, you must check if ItemIndex <> -1 first before referencing items
using that property.  If ItemIndex >= 0, then you can get the selected item.
If ItemIndex = -1, then no item has been selected and you should not try to
use ItemIndex as an index into the Items array.

Your app can initially set the first item in the list as the default
selected item by using the following code after the list has already been
populated:

If (Listbox1.Items.Count > 0) Then
    ListBox1.ItemIndex := 0;

Hope this helps.

--
Vinnie Murdico
Software with Brains, Inc.
SWBTracker - Professional Bug Tracking Software
http://www.softwarewithbrains.com

Re:ListBox Question (Newbie)


Thanks for your reply. I used your code example to get the values but I
still only get the listbox values to update when the user both scrolls
then selects an entry from the list. I use showmessage() to test the
code.

Here's some of the code:
I initially populate listboxes and radiogroup with default values in
FormCreate:

procedure TMainForm.FormCreate(Sender: TObject);
begin     lbDimension.ItemIndex := 1;
  lbKernel.ItemIndex := 0;
  rgEmbedState.ItemIndex := 2;
  rbClose.Checked := true;
end;

The user can make parameter selections from the listboxes or accept the
default values. He then hits a button to start the simulation.

procedure TMainForm.bStartClick(Sender: TObject);
//At this point in the program, the user has seleected
//a file and should have selected all the inputs
var
  i, j : integer;
  strKernel, dimen : string;
begin        //grab the dimension
  If (lbDimension.ItemIndex >= 0) Then
      dimen := lbDimension.Items[lbDimension.ItemIndex];
      showmessage('Here is dimension ' + dimen);

//grab the kernel type
  If (lbKernel.ItemIndex >= 0) Then
      strkernel := lbKernel.Items[lbKernel.ItemIndex];
      showmessage('Here is Kernel ' + strKernel);

//grab the dimension
  If (lbDimension.ItemIndex >= 0) Then
      strkernel := lbDimension.Items[lbDimension.ItemIndex];
      showmessage('Here is strkernel ' + strkernel);

....

When I run this code, the background color of the listboxes on my
mainform is green and when I press the StartSimulation button, the
showmessages display the correct values. However, when I change a value
in a listbox by using the scrollbar, the background color changes to
white but when I run the code again the showmessages don't update and
show the new values I've selected.  Only when I click on the field
inside the listbox (which turns the background color green) will the
showmessage display the correct (updated) values.

I don't know what's going on...

Bill

"Vinnie Murdico" <vin...@softwarewithbrainsSPAMKILL.com> wrote in
message news:4zWdnSTNAZgumqejXTWc3w@gbronline.com...

Quote
> You should be able to read the selected item regardless of whether the
user
> has scrolled the listbox or clicked on it.  Of course, when a list box
is
> initially created and then populated, there is no "default" selected
item,
> so either your application or the user must select an item in the list
box
> as the "selected" item in order for you to recognize it in your code.

> To get the currently selected item, use:

> var
>   SelectedItem : String;
> begin
>   If (ListBox1.ItemIndex >= 0) Then
>       SelectedItem := ListBox1.Items[ListBox1.ItemIndex];

> Of couse, you must check if ItemIndex <> -1 first before referencing
items
> using that property.  If ItemIndex >= 0, then you can get the selected
item.
> If ItemIndex = -1, then no item has been selected and you should not
try to
> use ItemIndex as an index into the Items array.

> Your app can initially set the first item in the list as the default
> selected item by using the following code after the list has already
been
> populated:

> If (Listbox1.Items.Count > 0) Then
>     ListBox1.ItemIndex := 0;

> Hope this helps.

> --
> Vinnie Murdico
> Software with Brains, Inc.
> SWBTracker - Professional Bug Tracking Software
> http://www.softwarewithbrains.com

Re:ListBox Question (Newbie)


In article <Xjm_9.6267$ZX5.169...@twister.austin.rr.com>, "William Vedder"

Quote
<wved...@houston.rr.com> writes:
>I have two listboxes on a form; each has a vertical scrollbar. I am
>trying to understand how to read the selected item. In Delphi, does the
>user have to scroll thru listbox list and then select the item in the
>listbox before the program recognizes an item has been selected? That
>seems to be one step too many; the user should just have to scroll thru
>the list then stop at the item of choice.

>If there's an event or property for the listboxes that I can use to read
>the selection w/o the user having to scroll AND select?

Any item in a TListBox is selected by clicking on the displayed item. Once an
item has been selected, then TListBox.ItemIndex is set to the index of the
selected item. That selection and ItemIndex remains as ir was set even if the
listbox is scrolled.

The user can select an item _only_ by clicking on it (multi-select selects
additional items by clicks with a modifier key).

While a TListBox has a TopIndex which reads or sets the item at the top of a
displayed listbox, one cannot sense when a mouse-up occurs on the scroll-bar so
that one might select the top item on that event for example.

I think you cannot do what you appear to want to do.

Alan Lloyd
alangll...@aol.com

Re:ListBox Question (Newbie)


Thanks Alan.  I was hoping that there was an "onchange" event or
something that I was missing.  It seems senseless to have to scroll AND
select.

Best,
Bill

Quote
"AlanGLLoyd" <alangll...@aol.com> wrote in message

news:20030201085805.04274.00000054@mb-co.aol.com...
Quote
> In article <Xjm_9.6267$ZX5.169...@twister.austin.rr.com>, "William
Vedder"
> <wved...@houston.rr.com> writes:

> >I have two listboxes on a form; each has a vertical scrollbar. I am
> >trying to understand how to read the selected item. In Delphi, does
the
> >user have to scroll thru listbox list and then select the item in the
> >listbox before the program recognizes an item has been selected? That
> >seems to be one step too many; the user should just have to scroll
thru
> >the list then stop at the item of choice.

> >If there's an event or property for the listboxes that I can use to
read
> >the selection w/o the user having to scroll AND select?

> Any item in a TListBox is selected by clicking on the displayed item.
Once an
> item has been selected, then TListBox.ItemIndex is set to the index of
the
> selected item. That selection and ItemIndex remains as ir was set even
if the
> listbox is scrolled.

> The user can select an item _only_ by clicking on it (multi-select
selects
> additional items by clicks with a modifier key).

> While a TListBox has a TopIndex which reads or sets the item at the
top of a
> displayed listbox, one cannot sense when a mouse-up occurs on the
scroll-bar so
> that one might select the top item on that event for example.

> I think you cannot do what you appear to want to do.

> Alan Lloyd
> alangll...@aol.com

Re:ListBox Question (Newbie)


Please don't include attached files with your messages. Post the code if
necessary (and not too long).

Re:ListBox Question (Newbie)


In article <fuV_9.12285$2y.582...@twister.austin.rr.com>, "William Vedder"

Quote
<wved...@houston.rr.com> writes:
> It seems senseless to have to scroll AND
>select.

But how do you know which (of the many items displayed when scrolling) the user
wants to select. Or are you displaying only one item as the scrolling takes
place. If that's the case, then there are other ways of doing that sort of
thing.

Alan Lloyd
alangll...@aol.com

Re:ListBox Question (Newbie)


Yes, I have sized the listboxes such that the user sees only one item at
a time. I am interested to hear other suggestions besides a listbox... I
looked at doing this w/ radio buttons but that's ugly.

Bill

Quote
"AlanGLLoyd" <alangll...@aol.com> wrote in message

news:20030201150949.18097.00000393@mb-mc.aol.com...
Quote
> In article <fuV_9.12285$2y.582...@twister.austin.rr.com>, "William
Vedder"
> <wved...@houston.rr.com> writes:

> > It seems senseless to have to scroll AND
> >select.

> But how do you know which (of the many items displayed when scrolling)
the user
> wants to select. Or are you displaying only one item as the scrolling
takes
> place. If that's the case, then there are other ways of doing that
sort of
> thing.

> Alan Lloyd
> alangll...@aol.com

Re:ListBox Question (Newbie)


I apologize for the attachment but that's not what I sent or at least
intended to send. I cut/pasted some code into my email response. I don't
know how it ended up as an attachment.

Best,
Bill
"John of Aix" <j.murphyNOS...@libertysurf.fr> wrote in message
news:b1h87g$12t1si$13@ID-157326.news.dfncis.de...

Quote
> Please don't include attached files with your messages. Post the code
if
> necessary (and not too long).

Re:ListBox Question (Newbie)


Quote
William Vedder wrote in message ...
>Yes, I have sized the listboxes such that the user sees only one item at
>a time. I am interested to hear other suggestions besides a listbox... I
>looked at doing this w/ radio buttons but that's ugly.

This screams for a dropdown-listbox (a specific style of combobox).

In normal listboxes, you can scroll the selection out of sight.
That can be somewhat useful sometimes.

A propos the radio buttons versus the dropdown-listbox, this is
the exact difference between NT4's and NT5's logoff dialogs.
In NT4, you can _see_ the options without clicking anything. As
a result, I can pick them blindly using the cursor keys. This is
convenient. In NT5, not only do I need more motions, but I have
to read the list anew every time. It just doesn't stick. This is
_not_ convenient.

Groetjes,
Maarten Wiltink

Re:ListBox Question (Newbie)


In article <CBX_9.12382$2y.610...@twister.austin.rr.com>, "William Vedder"

Quote
<wved...@houston.rr.com> writes:
>Yes, I have sized the listboxes such that the user sees only one item at
>a time. I am interested to hear other suggestions besides a listbox... I
>looked at doing this w/ radio buttons but that's ugly.

A TStringList, a read-only TEdit, and a TUpDown would IMO be the best as
follows ...

var
  MyStrList : TStringList;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Edit1.AutoSelect := false;   // ... or set in IDE
  Edit1.ReadOnly := true;      // ... or set in IDE
  {create a TStringList with the user options}
  MyStrList := TStringList.Create;
  MyStrList.Text := 'One'#13'Two'#13'Three'#13'Four'#13'Five';
  {set the UpDown}
  UpDown1.Min := 0;
  UpDown1.Max := MyStrList.Count - 1;
  UpDown1.Position := 0;
  {display first selection}
  UpDown1Click(nil, btNext);
end;

procedure TForm1.UpDown1Click(Sender: TObject; Button: TUDBtnType);
begin
  Edit1.Text := MyStrList.Strings[UpDown1.Position];
end;

Alan Lloyd
alangll...@aol.com

Re:ListBox Question (Newbie)


Quote
Maarten Wiltink wrote:
> A propos the radio buttons versus the dropdown-listbox, this is
> the exact difference between NT4's and NT5's logoff dialogs.
> In NT4, you can _see_ the options without clicking anything. As
> a result, I can pick them blindly using the cursor keys. This is
> convenient. In NT5, not only do I need more motions, but I have
> to read the list anew every time. It just doesn't stick. This is
> _not_ convenient.

It's not *that* bad as you can select a combo box entry
by typing its initial letter. Nevertheless the old design
was better IMHO, too.
Uli.

Other Threads