R
R
Romanson2016-03-17 06:54:30
Delphi
Romanson, 2016-03-17 06:54:30

Who will help to understand the code for the ListBox?

309e4281c2264f919aadb1ae9b7422af.pngThere are 2 listbox and memo on the form, what I'm trying to do on an android application - There is some base in memo in it lines like "#0 Hotel 1", "#0 Hotel 2", "#1 Restaurant 1", "#1 Restaurant 2", etc. Listbox1 acts as a navigation menu, it also has a Prefix in the name of the line "#0 Hotels", "#1 Restaurants".
I'm trying to display the result in lstbox2 by the OnItemClick event -
Checks if the number of the pressed line matches the line prefix in the Listbox (I'm looking for the number by the second character) i.e. with "#0" I get just "0", then in listbox2 the list of lines starting with #0 was deduced from the Memo base.
So - I managed to get the number of the pressed line in listbox1 "Navigation Menu" but I can not display the result from the memo ... help

procedure TForm1.ListBox1ItemClick(const Sender: TCustomListBox;
  const Item: TListBoxItem);
var
  n, i: integer;
  s, k, t, d: string;
begin
  n := ListBox1.ItemIndex;

  k := ListBox1.Items[ListBox1.ItemIndex];
  if length(k) = 1 then
    s := k
  else
    s := k[2];

  if n = strtoint(s) then
  begin
    ListBox2.Clear;

    for i := 0 to Memo1.Lines.Count - 1 do

      t := Memo1.Lines[i];
    if length(t) = 1 then
      t := d
    else
      t := d[2];

    if t = inttostr(n) then

      ListBox1.Items.Add(Memo1.Lines[i]);

  end;

end;

ANSWER Maybe someone needs:

procedure TForm1.ListBox1ItemClick(const Sender: TCustomListBox;
  const Item: TListBoxItem);
var
  n, i: integer;
  s, k, c, v, d: string;
begin
  ListBox2.Clear;
  n := ListBox1.ItemIndex;

  k := ListBox1.Items[ListBox1.ItemIndex];
  if length(k) = 1 then
    s := k
  else
    s := k[2];

  if n = strtoint(s) then
  begin
    for i := 0 to Memo1.Lines.Count - 1 do
    begin

      v := Memo1.Lines.Strings[i];

      if length(v) = 1 then
        c := v

      else
      begin
        c := v[2];
        if c = s then
        begin

          d := Memo1.Lines.Strings[i];
          ListBox2.Items.Add(d);
        end;
      end;
    end;
  end;

end;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
zRabbit, 2016-03-17
@zRabbit

Replace k := ListBox1.Items[ListBox1.ItemIndex]; to k := ListBox1.Items.Strings[ListBox1.ItemIndex];
Something like this, there is an error in accessing a non-existent idnex, you may need to use -1 (index)

R
Romanson, 2016-03-17
@Romanson

77e86ca2258d4edcb96d1f2eb466b526.pngthis is what it outputs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question