我看了一个例子:他是使用treeview实现的,他在数组中设定了一个TTreeNode的变量,他的实现方法如下:例如数组变量名称为: Puser = ^user; user = record ID :Integer; Name :String[40]; userName :String[40]; Node :TTreeNode; AThread :TIdPeerThread; end;他在实现的时候调用的是:puser(treeview.Selected.Data).ID来调用的。那在使用LISTBOX应该如何实现呢?
blazingfire 帮帮忙吧
如果代码是Integer型,直接在加的时候把ID强制转化为ID加进去,取的时候再强制转化为Integer就可以了 加: ListBox1.Items.AddObject(UserName, TObject(ID)); 取: var i: Integer; begin //如取名字为“blazingfire”的ID就可以这样了 i := ListBox1.Items.IndexOf('blazingfire'); if i > -1 then begin i := Integer(ListBox1.Items.Objects[i]);//强制转化为Integer ShowMessage('blazingfire的ID为:'+IntToStr(i)); end else ShowMessage('没有找到!'); end; 以上代码随手写了,具体的你要自己调一下
呵呵,分,我给你加分; 怎么读取当前选中项目的名字为:blazingfire i := ListBox1.Items.IndexOf('这里是怎么读取呢'); 解决后一定给您加分
begin
ListBox1.Style := lbOwnerDrawVariable; //自己画每一项,项高度可调
ListBox1.ItemHeight := 30;
end;procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
ImgRect, CalcRect: TRect;
Cnvs: TCanvas;
B: TBitmap;
S: string;
H: Integer;
FmtExt: Integer;
begin
Cnvs := ListBox1.Canvas;
ImgRect := Rect;
ImgRect.Right := ImgRect.Left + (ImgRect.Bottom - ImgRect.Top); //画图区域 if odSelected in State then
begin
Cnvs.Brush.Color := clSkyBlue;
Cnvs.Pen.Color := clNavy;
end
else
begin
Cnvs.Brush.Color := ListBox1.Color;
Cnvs.Pen.Color := ListBox1.Color;
end;
Cnvs.Brush.Style := bsSolid;
Cnvs.Rectangle(Rect); Cnvs.Brush.Style := bsClear;
if ImageList.Count > 0 then
begin
B := TBitmap.Create;
try
ImageList.GetBitmap(0, B);
B.PixelFormat := pf24bit;
B.Transparent := True;
InflateRect(ImgRect, -2, -2);
Cnvs.StretchDraw(ImgRect, B);
finally
B.Free;
end;
end; Rect.Left := ImgRect.Right;
CalcRect := Rect;
S := ListBox1.Items[Index];
Cnvs.Font.Assign(ListBox1.Font);
DrawText(Cnvs.Handle, PChar(S), -1,
CalcRect, DT_WORDBREAK or DT_CALCRECT); //算出高度为了垂直居中
H := Abs(CalcRect.Top - CalcRect.Bottom); //高度-->i
H := Abs(Rect.Top - Rect.Bottom) - H; //画布提供的高度与实际要的高度之差-->i
if H > 0 then
begin
Inc(Rect.Top, H div 2);
Inc(Rect.Bottom, H div 2);
FmtExt := 0;
end
else
FmtExt := DT_EDITCONTROL; //画不完整的就不画了
DrawText(Cnvs.Handle, PChar(S), -1,
Rect, DT_WORDBREAK or FmtExt); //垂直调整居中以后画出文字
end;
Puser = ^user;
user = record
ID :Integer;
Name :String[40];
userName :String[40];
Node :TTreeNode;
AThread :TIdPeerThread;
end;他在实现的时候调用的是:puser(treeview.Selected.Data).ID来调用的。那在使用LISTBOX应该如何实现呢?
加:
ListBox1.Items.AddObject(UserName, TObject(ID));
取:
var
i: Integer;
begin
//如取名字为“blazingfire”的ID就可以这样了
i := ListBox1.Items.IndexOf('blazingfire');
if i > -1 then
begin
i := Integer(ListBox1.Items.Objects[i]);//强制转化为Integer
ShowMessage('blazingfire的ID为:'+IntToStr(i));
end
else
ShowMessage('没有找到!');
end;
以上代码随手写了,具体的你要自己调一下
怎么读取当前选中项目的名字为:blazingfire i := ListBox1.Items.IndexOf('这里是怎么读取呢');
解决后一定给您加分