首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

怎么让Listbox绑定多列

2013-09-06 
如何让Listbox绑定多列?如何能让Delphi中的Listbox像C#一样绑定多列,即一列值用于显示,一列值用于后台处理

如何让Listbox绑定多列?
如何能让Delphi中的Listbox像C#一样绑定多列,即一列值用于显示,一列值用于后台处理。
譬如, listbox显示 一,二,三。。。
              选择后获取其对应值  A, B, C ...

为何使用以下代码,却获取不到正确值?

 

TModel   =   class(TObject)
    private 
        pValue:   string;
    public
       property   Value:   string   read   pValue   write   pValue;
    end;

//初始化ListBox数据  
 aUser := TModel.Create;
   for I := 0 to 10 do
   begin
     aUser.Value :='测试00' + inttostr(i);
     listBox1.Items.AddObject ('第'+ inttostr(i)+'条信息',auser);
   end;

//listBox1Click中显示
ShowMessage(listBox1.Items.Strings[listBox1.ItemIndex] +#13 +'Value ' + (listBox1.Items.Objects[listBox1.ItemIndex] as  TModel).Value);


为何 showmessagebox显示的 Value 总得不到正确值?
另外,有没有比较好的处理办法,或者推荐一个好用的控件?



[解决办法]
procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
    pcode:Pstring;
begin
  for I := 0 to 9 do
  begin
    new(pcode);
    pCode^:='第'+inttostr(i)+'条记录';
    listbox1.AddItem(inttostr(i),Tobject(pcode));
  end;
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
  self.Caption:= pstring(self.ListBox1.Items.Objects[Listbox1.ItemIndex])^;

end;
[解决办法]
方法很多,用指针是比较好的方法,如结构体指针,存更多值;不用指针可以用TStringList;

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
  sList:TStringList;


begin
  for I := 0 to 9 do
  begin
    sList:=TStringList.Create;
    sList.Add('第'+inttostr(i)+'条记录');
    sList.Add('Hello'+inttostr(i));
    listbox1.AddItem(inttostr(i),sList);
  end;
end;

取值:
sList:=TStringList(ListBox1.Items.Objects[Listbox1.ItemIndex]);
showmessage(sList.strings[0]);
showmessage(sList.strings[1]);

热点排行