Recent

Author Topic: [SOLVED]How to store strings in a given ListBox selection?  (Read 1837 times)

Matt723

  • New Member
  • *
  • Posts: 20
[SOLVED]How to store strings in a given ListBox selection?
« on: January 25, 2020, 12:16:10 pm »
Hi,

I have a ListBox and items can be added and removed at run-time. How can I "store" strings in a given selection in the ListBox (without it actually being the string that is visible)? So, for example, if I have a TextBox, I can type the string that I want to be stored (invisible, in the background), and press okay to add the string to the ListBox, and then, when that item in the ListBox is selected, I can press another button and that string that was stored will appear in different TextBox.

Thank you for your time,
Matt
« Last Edit: January 25, 2020, 06:08:39 pm by Matt723 »

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: How to store strings in a given ListBox selection?
« Reply #1 on: January 25, 2020, 12:43:24 pm »
Not fully understood what you want, but here I have demo for ListBox maybe you can learn something from it:
https://forum.lazarus.freepascal.org/index.php/topic,47651.msg341708.html#msg341708

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to store strings in a given ListBox selection?
« Reply #2 on: January 25, 2020, 02:56:27 pm »
The attached project shows you one way to do what you want, using the Objects property of TStrings, the data container class used by TListBox.

Matt723

  • New Member
  • *
  • Posts: 20
Re: How to store strings in a given ListBox selection?
« Reply #3 on: January 25, 2020, 06:07:56 pm »
The attached project shows you one way to do what you want, using the Objects property of TStrings, the data container class used by TListBox.
That's exactly what I was looking for! Thank you very much for taking the time to make the demo.

Matt723

  • New Member
  • *
  • Posts: 20
Re: [SOLVED]How to store strings in a given ListBox selection?
« Reply #4 on: January 25, 2020, 06:47:10 pm »
Hi Guys, it's me again. So, I was trying to store two different strings with the ListBox item, using howardpc's demo and, I'll admit, I'm not doing it very tidily and there's probably a much neater way but, regardless, I can't get this method to work for some reason:

Code: Pascal  [Select][+][-]
  1. uses
  2.  
  3. { TTextClass }
  4.  
  5. constructor TTextClass.Create(const aText: String);
  6. begin
  7.   text := aText;
  8. end;
  9.  
  10. constructor TTextClass2.Create(const aText2: String);
  11. begin
  12.   text2 := aText2;
  13. end;                      
  14.  
  15. procedure TForm1.AddHiddenTextToListbox;
  16. begin
  17.   if Form2.ListBox1.ItemIndex < 0 then
  18.     Exit;
  19.   Form2.ListBox1.Items.Objects[Form2.ListBox1.ItemIndex] := TTextClass.Create(Form2.LabeledEdit1.Text);
  20.   Form2.ListBox1.Items.Objects[Form2.ListBox1.ItemIndex] := TTextClass2.Create(Form2.LabeledEdit1.Text);
  21. end;  
  22.  
  23. procedure..........
  24.  
  25.   variable1 := TTextClass(Form2.ListBox1.Items.Objects[Form2.ListBox1.ItemIndex]).text;
  26.   variable2 := TTextClass2(Form2.ListBox1.Items.Objects[Form2.ListBox1.ItemIndex]).text2;  

I get absolutely no errors from this, but the output for
Code: Pascal  [Select][+][-]
  1. variable1
and
Code: Pascal  [Select][+][-]
  1. variable2
is the same (that of
Code: Pascal  [Select][+][-]
  1. text2
). Why is this?

Thank you.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [SOLVED]How to store strings in a given ListBox selection?
« Reply #5 on: January 25, 2020, 08:20:19 pm »
You don't show your full code. But you almost certainly don't want two different textclass types with two different constructors, but two separate instances of the one TTextClass type.
Code: Pascal  [Select][+][-]
  1. ...var
  2.   textclass1, textclass2: TTextClass;  
  3.  
  4.   var1, var2: String;
  5. begin
  6.  textclass1 := TTextClass.Create('text1');
  7.  
  8.  textclass2 := TTextClass.Create('text2');
  9.  ...  
  10.  
  11.  var1 := textclass1.text;
  12.  var2 := textclass2.text;
  13. ...
  14.  textclass1.Free;
  15.  textclass2.Free;
  16. ...
  17. etc.
« Last Edit: January 26, 2020, 12:01:30 am by howardpc »

Matt723

  • New Member
  • *
  • Posts: 20
Re: [SOLVED]How to store strings in a given ListBox selection?
« Reply #6 on: January 26, 2020, 01:46:03 am »
Hi howard,

I'm trying to fiddle around with it, but I keep getting exceptions like "SIGSEGV" (among others) when I try.  I tried reading about constructors, but I can't find a way to do it. Attached is a demo with all the code on, along with some of the code that I have tried but failed at (commented out). It would be much appreciated if you could take a look.

Thanks very much.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [SOLVED]How to store strings in a given ListBox selection?
« Reply #7 on: January 26, 2020, 09:50:53 am »
I think you want to do something like the attached altered Demo?

Matt723

  • New Member
  • *
  • Posts: 20
Re: [SOLVED]How to store strings in a given ListBox selection?
« Reply #8 on: January 26, 2020, 12:03:38 pm »
That's exactly what I needed. I cannot thank you enough for your help,

 

TinyPortal © 2005-2018