如何使listbox只显示文件名(不含路径)而单击时又能取得相应文件的路径?
如
C:\windows\1345.jpg
在listbox中显示为
1345.jpg
而在上面单击时却能取得包含路径的全文件名C:\windows\1345.jpg并且要求可以在listbox中进行各种操作如删除、添加、移动等。
如
C:\windows\1345.jpg
在listbox中显示为
1345.jpg
而在上面单击时却能取得包含路径的全文件名C:\windows\1345.jpg并且要求可以在listbox中进行各种操作如删除、添加、移动等。
TListBox.Items.AddObject
方法,可以让字符串关联一个对象,而这个对象里面随便你放什么
ListBox1.Items里的内容要为全路径,如C:\windows\1345.jpg;
设置属性 ListBox1.Style := lbOwnerDrawFixed;
然后在ListBox1的OnDrawItem事件里写如下代码:
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
c: TCanvas;
begin
c := TCanvas.Create;
c.Handle := GetDC(Control.Handle);
if odSelected in State then begin
c.Brush.Color := clActiveCaption;
c.Font.Color := clWhite;
c.FillRect(rect);
c.TextOut(Rect.Left, Rect.Top, ListBox1.Items[Index])
end
else begin
c.FillRect(rect);
c.TextOut(Rect.Left, Rect.Top, ExtractFileName(ListBox1.Items[Index]));
end;
c.Free;
end;
呵呵,老大又是你啊,你的方法我还没试过可是总觉得有点复杂。
ysai(赛赛)
你的办法好像不错,能给出具体点的实现吗
随手写的,保不定有BUG哦
定义对象:
Type
TFileInfo=class(TObject)
FileName:String;
FilePathName:String;
FileSize:Integer;
end;
保存数据:
var
fi:TFileInfo;
begin
fi:=TFileInfo.Create;
fi.FilePathName := 'C:\A.TXT';
fi.FileName := 'A.TXT';
fi.FileSize := 10;
lw.Items.AddObject(fi.FileName,fi);
end;
取出数据:
ShowMessage(TFileInfo(lw.Items.Objects[lw.ItemIndex]).FilePathName);
我看到画画就头昏
To ysai(赛赛 )
你的方法比较直观,看得懂,决定试试,等下再结帐
你的方法是我最初所想的,但发觉不实用,因为要同时操作两个tstrings太麻烦了
你的办法不行啊,添加文件时出错!!代码如下:
procedure TfrmMain.ListBoxDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
MyCanvas:TCanvas;
begin//自已画出列表中的项目
MyCanvas.Create;
MyCanvas.Handle:=GetDC(Control.Handle );
MyCanvas.FillRect(Rect);
MyCanvas.TextOut(Rect.Left,Rect.Top ,ExtractFileName(Listbox.Items[Index]));
MyCanvas.Free;
end;是哪里错了???
2。在LISTBOX中DRAW一次
两者都可
老规矩,挂一夜,明天结帐