两列的ListBox如何使用??
一个两列的ListBox,如何添加数据?如何取得第二列的数据?? 问题点数:100、回复次数:7Top
1 楼rainfall19831109(恩恩)回复于 2006-07-02 03:52:17 得分 0
那要看看那个ListBox的Item属性是个什么类型的数据(不一定Item就是那个属性,具体要看控件,反正意思就是指示Item的属性)
根据我推测,应该是个TStringList类型的数据(也可能是控件定义的类型)
如果是TStringList的话,添加数据就直接给对应的Item调用TStringList的Add方法添加
读的时候就调用对应Item的Strings[Index]取数据Top
2 楼cbdxl(JF)回复于 2006-07-02 04:07:48 得分 0
楼主辛苦了!Top
3 楼lxj1984()回复于 2006-07-02 10:27:04 得分 0
谢谢,第一列的读写我会,可是第二列的,我就不知道了,不知道如何定位到第二列...Top
4 楼weixing979(★★★闪电侠★★★)回复于 2006-07-02 13:52:42 得分 20
两列的ListBox,是ListView吧.
如果是ListView
读写数据
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//写数据
TListItem * addItem ;
addItem = ListView1->Items->Add();
addItem->Caption = "caption";
addItem->SubItems->Add("sub1");
addItem->SubItems->Add("sub2");
//读数据
Edit1->Text = ListView1->Items->Item[0]->Caption;
Edit2->Text = ListView1->Items->Item[0]->SubItems->Strings[0];
Edit3->Text = ListView1->Items->Item[0]->SubItems->Strings[1];
}Top
5 楼lother(阿艺)回复于 2006-07-02 13:58:53 得分 0
同意楼上Top
6 楼daydayup234(关中刀客)回复于 2006-07-03 10:29:54 得分 10
__fastcall TForm1::TForm1(TComponent* Owner):TForm(Owner)
{
ListView1->ViewStyle=vsReport;
ListView1->Columns->Add();
ListView1->Columns->Add();
ListView1->Columns->Add();
ListView1->Columns->Items[0]->Caption="主列";
ListView1->Columns->Items[1]->Caption="子列1";
ListView1->Columns->Items[2]->Caption="子列2";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) //添加数据
{
ListView1->Clear();
TListItem* theItem;
//以下添加主列
theItem=ListView1->Items->Add();
theItem->Caption="one";
//以下添加“子列 ”相当你说 第2列 与 主列添加 不同
theItem->SubItems->Add("one的 子1");
//再添加
theItem=ListView1->Items->Add();
theItem->Caption="two";
theItem->SubItems->Add("two的 子1");
theItem->SubItems->Add("two的 子2");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)//取“第二列的数据”
{
TListItem*item;
item=ListView1->Items->Item[1]; //第二行 注意 ListView1的Items从0算起
AnsiString s;
s=item->SubItems->Strings[0];
//注意 “第二列的数据”是 子列 的第一个,但是它从0算起 所以 ->Strings[0]
ShowMessage(s);
}
//---------------------------------------------------------------------------
Top
7 楼BlueDeepOcean(蓝色·深海)回复于 2006-07-04 00:34:09 得分 70
楼上的,楼主说的是ListBox,而不是ListView。
看看我的代码:比如获得第二列:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int rpc = ListBox1->ClientHeight / ListBox1->ItemHeight;
int col = ListBox1->Items->Count / rpc;
if (col != ListBox1->Columns)
{
if (col < ListBox1->Columns)
for (int idx = 0;idx < rpc;idx ++)
ShowMessage(ListBox1->Items->Strings[idx + rpc]);
if (col > ListBox1->Columns && ListBox1->Columns != 0)
{
rpc = ListBox1->ClientHeight / ListBox1->ItemHeight;
col = ListBox1->Items->Count / rpc;
for (int idx = 0;idx < rpc;idx ++)
ShowMessage(ListBox1->Items->Strings[idx + rpc]);
}
}
else
for (int idx = 0;idx < rpc;idx ++)
ShowMessage(ListBox1->Items->Strings[idx + rpc]);
}
//---------------------------------------------------------------------------Top




