quick q

hi,

can anyone correct this code :|

var
n:integer;
begin
for N := 1 to 18 do
panel1.color:=StringToColor(listbox1.Items.Names[n]);

listbox1 has color names eg clred about 18 different colors and when i click on the listbox it gives me an error saying " is not a valid integer.

cheers
classic_gaming
classic_gamingAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Mohammed NasmanConnect With a Mentor Software DeveloperCommented:
Hello
what do you want to exactly?

  is what you are looking is to change the panel color to the color that u select in the list box?, if that just this line will work with u

 Panel1.Color:=StringToColor(Listbox1.Items.Strings[ListBox1.ItemIndex]);

if not, try to change ur code to this

var
  n:integer;
begin
for N := 0 to listbox1.items.Count -1 do
 Panel1.Color:=StringToColor(Listbox1.Items.Strings[ListBox1.ItemIndex]);


Start ur loop with 0 to the all items in the listbox
and use
Listbox1.Items.Strings
instead of
Listbox1.Items.Names

Mohammed
0
 
edeyCommented:
try 0 to 17

Gl
Mike
0
 
DraregCommented:

The first element of a TListBox ( or TStringList, etc. ) has an index of 0, the second element has the index = 1, etc.
So, edey is right.  If you write
For N := 0 To 17 Do...  will work
Or
For N := 0 To ListBox1.Iems.Count - 1 Do ...

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
h_mohsenianCommented:
hello,

edey is right . but it is not your problem , you have a wrong Color Name in that Listbox1.

it means at first you must check all listbox1 color names. when u corrected the wrong color name you must also change ( 1 to 18 ) to (0 to 17) for solve the message 'list index out of bound' problem

best regards
hamed
0
 
h_mohsenianCommented:
more clear : 'is not a valid integer' error message is becouse of wrong color name in your listbox. but it is not ur just mistake you must also do that edey & Drareg for solve ur futare problem with error message 'list index out of bound'
0
 
Mohammed NasmanSoftware DeveloperCommented:
Hello

  Check your listbox items, i think you have empty item
 the error message " is not a valid integer, mean that you have an item in ur list box empty, and you are trying to change the color to that name, so go to the items in ur listbox, and delete all the empty lines

Mohammed
0
 
h_mohsenianCommented:
hi Mohammed , Insha Allah you are fine.

if there was some items in the ListBox1 but one of them was bad name for example clrde instead of clred it makes this error message. but if the listbox1 was empty an error occured when he want to read an item with title 'list index out of bound' becouse it is an reading error but i think the exception call with StringToColor function. are you agree with me.

did you recieve my last email . did you think about my suggestion ( at the end of email )?

Salam
hamed
0
 
d32coderCommented:
Instead of Items such as clRed, use the $000000 numbers.  Then you can skip the conversion process.

Don
0
 
classic_gamingAuthor Commented:
0 to 17 still gives an error.

var
n:integer;
begin
for N := 0 to listbox1.SelCount dopanel1.color:=StringToColor(listbox1.Items.Names[n]);

that dont give errors now.... but do i need to create if files for the 0 to the last number?

cheers
classic_gaming
0
 
FelixinCommented:
Agreeing with what has been said about the index (it should be from 0 to 17) and presuming that the Items that you have stored are not like "clRed=RedColor", but they are "clRed", then you should use

var
  n:integer;
begin
  for N := 0 to 17 do
    panel1.color:=StringToColor(listbox1.Items [n]);
end;                                     **********

This

panel1.color:=StringToColor(listbox1.Items.Names[n]);

only works with strings like "RedColor=clRed",

where "RedColor" is the name and "clRed" is the value.

The error comes you don't have "Names", I mean, there is no left part of the string with the form "AName=".



0
 
edeyCommented:
Felixin, though your comment may contain an actual answer, I think it's prob. not original/conclusive enough to merit being an "answer".

GL
Mike
0
 
FelixinCommented:
I may be wrong, but I understood that he wanted to correct the code, and the only problem I see is the access to the Names property in an TStrings, and the index question that you pointed at the begining.

I would like to know why the answer has been rejected, what is wrong whit it, even if I respect the decision and respect and thank your comments, Edey.

0
 
classic_gamingAuthor Commented:
hi,

Felixin i found mnasman code more helpful and i've even got the code to work let me post the code:

var
n,n1:integer;
s:string;
begin
for N := 0 to listbox1.items.Count -1 do
N1:=StringToColor('cl'+Listbox1.Items.Strings[ListBox1.ItemIndex]);
s:=inttostr(N1);
panel1.color:=stringtocolor(s);

i just made a color turn into a string then back to a int then back to a string and it worked fine :-)

so mnasman you get the points
thanks for all the help
classic_gaming
0
 
classic_gamingAuthor Commented:
all i need to do was change a few bits and it worked fine :-)
0
 
d32coderCommented:
 Colors := array [1..16] of TColor;

  In retrospect, that would have been a good starting point.

  Panel1.Color := Colors[x];
0
All Courses

From novice to tech pro — start learning today.