TCheckListBox奇怪的行为,没有显示第一个char

时间:2014-11-04 07:16:10

标签: delphi tchecklistbox

所以我有一个包含6个项目的CheckListBox:

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

如果我想将它们显示在ShowMessage对话框中,则打印的信息是。

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

我使用的程序就是这个。

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

为什么我不能从我的物品中获取第一个字符?

2 个答案:

答案 0 :(得分:4)

尝试以正确的方式插入项目

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;

结果将是:

enter image description here

...然后

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

end;

答案 1 :(得分:2)

您不能将ValueFromIndex用于您的porpouse。

TStrings.ValueFromIndex

根据字符串的索引返回字符串的值部分。

声明

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

描述

ValueFromIndex根据字符串索引返回字符串的值部分。值部分是NameValueSeparator字符后面的字符串中的所有字符,如果NameValueSeparator字符不存在,则是所有字符。

TStrings.NameValueSeparator

用于分隔名称,值对的字符的值

声明

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

描述

NameValueSeparator是用于分隔名称,值对的字符。默认情况下,这是等号(=),从而产生Name = Value对。

可以为Name:Value对设置冒号。

看看vcl源:O

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