Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 09-08-2014, 04:03 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default Random Number Selection

Hi and Thanks in advanced for your help.

I have this list box with the below items inside. I found this code from Jasser, however how can when the list is randomly selected get the value of the selected list item, I tried "S:= listBox1.items[ListBox1.ItemIndex] but I am getting an index out of bound error.

1000
3000
4000
5500
200
345
845
310
6000


Here is the code from Jasser (By the way, Thanks Jasser)

Code:
var
          i : Integer;
          s : string;
begin
          for i:=0 to Listbox1.Items.Count-1 do
              Listbox1.Items.Move(i,Random(Listbox1.Items.Count - 1));

    s := "i need to get the value of the selected list item for example 6000, 845, 200 etc"
Thank so much for the help
Reply With Quote
  #2  
Old 09-08-2014, 04:24 PM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

Try this:
Code:
if ListBox1.ItemIndex <> -1 then
   s := ListBox1.Items.Strings[ListBox1.ItemIndex];
Reply With Quote
  #3  
Old 09-08-2014, 04:32 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

Hi Kolbasz

Thanks for your response

I am not getting the error now, but s is empty no string values

Thanks
Reply With Quote
  #4  
Old 09-08-2014, 04:36 PM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

Hi,

You're welcome!
Make sure at least on line/item is selected, otherwise Listbox1.Itemindex = -1

regards,
kolbasz
Reply With Quote
  #5  
Old 09-08-2014, 04:41 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

Yes, I think that's the problem, the code cycles through the list box and selects one of the items, however the item doesn't get highlighted in blue as normal. The list box item gets selected with a black rectangular box around the item, making me believe that indeed the code is randomly picking an item but not highlighting it to complete a true selection of the item.
Reply With Quote
  #6  
Old 09-08-2014, 04:55 PM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

You can alwas select an item this way:
Listbox1.Selected[Index] := True;
Where Index is a number between 0 and Listbox1.Items.Count - 1
Reply With Quote
  #7  
Old 09-08-2014, 05:06 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

Boyla!!

Done deal.

Thanks so much!

Code:
var
          i : Integer;
          s : string;
begin
          for i:=0 to Listbox1.Items.Count-1 do
          begin
              Listbox1.Items.Move(i,Random(Listbox1.Items.Count - 1));
              Listbox1.Selected[i] := True;
          end;

if ListBox1.ItemIndex <> -1 then
   s := ListBox1.Items.strings[ListBox1.ItemIndex];

   Label1.Caption := s;
Best Regards
Reply With Quote
  #8  
Old 09-08-2014, 05:19 PM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

You're welcome delphimpd!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 04:01 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.