Board index » cppbuilder » ListBox -> Items -> Strings [...]

ListBox -> Items -> Strings [...]

Hi guys. I dinamically created a list box and assigned the first item:

TListBox* myListBox = new TListBox ( Application );
myListBox -> Parent = Form1;
myListBox -> Items -> Strings [ 0 ] = "some_string";

When I try to assign "some_string" to the first item in the listbox the
system generates an exception.

The same code works fine using TComboBox instead of TListBox. In other
words:

myListBox -> Items -> Strings [ 0 ] = "some_string";  <--- this does not
work

myComboBox -> Items -> Strings [ 0 ] = "some_string"; <--- this works
fine

Why ? TIA from Alvise.

 

Re:ListBox -> Items -> Strings [...]


Quote
"Alvise Valsecchi" <alv...@hochfeiler.it> wrote in message

news:3B568071.48C0A9C1@hochfeiler.it...

Quote

> Hi guys. I dinamically created a list box and assigned the first item:

> TListBox* myListBox = new TListBox ( Application );
> myListBox -> Parent = Form1;
> myListBox -> Items -> Strings [ 0 ] = "some_string";

> When I try to assign "some_string" to the first item in the listbox the
> system generates an exception.

> The same code works fine using TComboBox instead of TListBox. In other
> words:

> myListBox -> Items -> Strings [ 0 ] = "some_string";  <--- this does not
> work

> myComboBox -> Items -> Strings [ 0 ] = "some_string"; <--- this works
> fine

> Why ? TIA from Alvise.

Try: myListBox->Items->Append("some_string");

Markus

Re:ListBox -> Items -> Strings [...]


You should add some item to list box first e.g.

TListBox* myListBox = new TListBox ( Application );
myListBox -> Parent = Form1;
//there is no items in list box now
myListBox -> Items -> Add("some_string");
myListBox -> Items -> Add("another_string");
myListBox -> Items -> Strings [ 0 ] = "third_string";

Good luck

Re:ListBox -> Items -> Strings [...]


Quote
oleksa wrote:
> You should add some item to list box first e.g.

I suspected that, but TComboBox works *without* adding anything before
assigning the string. Why should I add something to the TListBox ?
Aren't TComboBox and TListBox similar ? Isn't it odd ?

Thanks for your response and regards from Alvise.

Re:ListBox -> Items -> Strings [...]


Hi Alvise,

    how about the next statement?

    myListBox->Items->Insert(0,"some_string");

    I am not 100% if it works, but 0 represents the index value.

Greetings,
Ramses Matabadal

Alvise Valsecchi <alv...@hochfeiler.it> schreef in berichtnieuws
3B568071.48C0A...@hochfeiler.it...

Quote

> Hi guys. I dinamically created a list box and assigned the first item:

> TListBox* myListBox = new TListBox ( Application );
> myListBox -> Parent = Form1;
> myListBox -> Items -> Strings [ 0 ] = "some_string";

> When I try to assign "some_string" to the first item in the listbox the
> system generates an exception.

> The same code works fine using TComboBox instead of TListBox. In other
> words:

> myListBox -> Items -> Strings [ 0 ] = "some_string";  <--- this does not
> work

> myComboBox -> Items -> Strings [ 0 ] = "some_string"; <--- this works
> fine

> Why ? TIA from Alvise.

Re:ListBox -> Items -> Strings [...]


Quote
> I suspected that, but TComboBox works *without* adding anything before
> assigning the string. Why should I add something to the TListBox ?
> Aren't TComboBox and TListBox similar ? Isn't it odd ?

8-0 Are you shure?
TComboBox and TListBox Items property has TStings type.
How you can access to the first element of _empty_ array?

Re:ListBox -> Items -> Strings [...]


Alvise,
If you check in the help it says that TStrings "contains pure virtual
methods" so I think it is possible that TComboBox is using different
methods on TStrings then TListBox, however, I am a little iffy on my C++
syntax, so I could be wrong about this.

Chris Nasr
    cnasr[at]mechtronix[dot]ca
Mechtronix Systems Inc.

Re:ListBox -> Items -> Strings [...]


Thanks oleksa and Chris for your answers.

I am pretty sure that TComboBox works fine and TListBox does not. I
revised my (very simple) code and did not find any Items->Add() before
directly assigning any string to the Items->Strings[] of the combo box.

This looks likely to be as Chris said: TComboBox makes a different use
of the pure virtual methods of TStrings.

BTW: in my new program I stopped using the listbox and started using a
combo box with a suitable style. It is better too !

Regards from Alvise.

Re:ListBox -> Items -> Strings [...]


Quote
Ramses Matabadal wrote:
>     how about the next statement?

>     myListBox->Items->Insert(0,"some_string");

>     I am not 100% if it works, but 0 represents the index value.

Hi. I am going to try it.

Anycase I decided to stop using the listbox and start using a combo box
with a suitable style.

Thanks and regards from Alvise.

Other Threads