TCheckListBox strange behaviour, not showing first char

233 Views Asked by At

So I have a CheckListBox with 6 Items :

Items.Strings = (
    'Banana'
    'Apple'
    'Pomelo'
    'Orange'
    'Peach'
    'BlueBarry')

If I want to show them then into a ShowMessage dialog, the message printed is.

'anana','pple','omelo','range','each','lueBarry'.

The procedure I use is this.

procedure TForm1.Button1Click(Sender: TObject);
var I : Integer;
begin
     for I := 0 to CheckListBox1.Items.Count - 1 do
          ShowMessage(CheckListBox1.Items.ValueFromIndex[I]);
end;

Why can't I get the first char from my Item?

2

There are 2 best solutions below

4
On BEST ANSWER

Try to insert items in the right way like this

procedure TForm1.Button1Click(Sender: TObject);
begin
  CheckListBox1.Items.Add('Banana');
  CheckListBox1.Items.Add('Apple');
  CheckListBox1.Items.Add('Pomelo');
  CheckListBox1.Items.Add('Orange');
  CheckListBox1.Items.Add('Peach');
  CheckListBox1.Items.Add('BlueBarry');
end;

the result will be:

enter image description here

then...

procedure TForm1.Button2Click(Sender: TObject);
var I : Integer;
begin
     for I := 0 to CheckListBox1.Items.Count - 1 do
          ShowMessage(CheckListBox1.Items[I]);

end;
4
On

You cannot use ValueFromIndex for your porpouse.

TStrings.ValueFromIndex

Return the value part of a string based on it's index.

Declaration

public property TStrings.ValueFromIndex : string
  read GetValueFromIndex
  write SetValueFromIndex;

Description

ValueFromIndex returns the value part of a string based on the string index. The value part are all characters in the string after the NameValueSeparator character, or all characters if the NameValueSeparator character is not present.

TStrings.NameValueSeparator

Value of the character used to separate name,value pairs

Declaration

public property TStrings.NameValueSeparator : Char
  read FNameValueSeparator
  write SetNameValueSeparator;

Description

NameValueSeparator is the character used to separate name,value pair. By default, this is the equal sign (=), resulting in Name=Value pairs.

It can be set to a colon for Name : Value pairs.

thake a look at the vcl source :O

function TStrings.GetValueFromIndex(Index: Integer): string;
begin
  if Index >= 0 then
    Result := Copy(Get(Index), Length(Names[Index]) + 2, MaxInt) else
    Result := '';
end;