Recent

Author Topic: TComboBox error  (Read 7098 times)

T-bear

  • Full Member
  • ***
  • Posts: 160
TComboBox error
« on: April 19, 2011, 08:21:50 pm »
Hi, i am making a program that uses TCombobox, but i have a little problem.
I want to set one string in the items property of the combobox.
This seems to work fine:
ComboBox1.Items[X]       :=Quests[X].Name;

But this in another unit doesnt:
Form1.ComboBox.Items[Z]:=Unit4.Quests[X].Name;

I get the following error:
List Index (0) out of bounds

lazarus: v. 0.9.30
fpc:v.2.4.2
Windows XP

Can you please help me!
Thanks! :D

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: TComboBox error
« Reply #1 on: April 19, 2011, 08:34:14 pm »
Probably because Z is larger than the number of ComboBox items. ComboBox items array is zero based.

T-bear

  • Full Member
  • ***
  • Posts: 160
Re: TComboBox error
« Reply #2 on: April 19, 2011, 08:36:23 pm »
Do i have to create them? cos i havent done that in the first code that worked well either. And Z is just a variable in a For XXX TO YYY statement from 0 to a variable number that in this case is 1.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: TComboBox error
« Reply #3 on: April 19, 2011, 08:39:40 pm »
Probably because ComboBox is empty.

T-bear

  • Full Member
  • ***
  • Posts: 160
Re: TComboBox error
« Reply #4 on: April 19, 2011, 08:40:36 pm »
Yeah, it is that in the first example to, but there it works. What do i then have to do to add a new item?

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: TComboBox error
« Reply #5 on: April 19, 2011, 08:47:22 pm »
You need to limit FOR loop to ComboBox.Items.Count - 1.

T-bear

  • Full Member
  • ***
  • Posts: 160
Re: TComboBox error
« Reply #6 on: April 19, 2011, 08:51:03 pm »
And if i want to make a new item at the end? Because the combobox is emty at the beginning, and should be filled with items after a while.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: TComboBox error
« Reply #7 on: April 19, 2011, 08:53:15 pm »
Items.Count is dynamical.

T-bear

  • Full Member
  • ***
  • Posts: 160
Re: TComboBox error
« Reply #8 on: April 19, 2011, 08:58:42 pm »
Now it doesnt put anything into it.
Code: [Select]
For Z:=0 To Quest-1 Do
            If Z < KEQuest.Quest.Items.Count Then
            begin
              Unit4.Quests[Z]       :=TQuest.Create;
              //-----------------------------------\\
              Unit4.Quests[Z].Name  :=OpenList[Count];
              KEQuest.Quest.Items[Z]:=Unit4.Quests[Z].Name;
              Count:=Count+1;

              //...
            end;
The working code looks like this, and i just dont get whats wrong:
Code: [Select]
Quest.Items[Quest.itemindex]:=QuestName.Text;
The scond code works, but the first doesnt, and in the second example the combobox also is emty.
Any ideas?
Thanks!

PS: Sorry if you already have told the answer, im still pretty new to lazarus and pascal

 

TinyPortal © 2005-2018