请问:在delphi中的listview控件中画控件CheckBox,怎么全选画的控件CheckBox控件为true。或对每一个进行判断是否打勾。怎么才能对每一行CheckBox.Checked的值进行判断。注意,不是listview控件自带CheckBox,那个是默认第排。我是画的可以随便定义。
下边是画的控件代码。 知道快帮我写下代码。。谢谢了。
1.先定义结构:type
PItemCtrl = ^TItemCtrl;
TItemCtrl = record
viewCheckBox: TCheckBox;
CheckBox: TCheckBox;
guidCheckBox: TCheckBox;
end; 2.在listview的CustomDrawSubItem事件中写如下代码;var
Rect: TRect;
P: PItemCtrl;
begin
if SubItem in [1,2,3] then
begin
DefaultDraw:= False; // 不显示默认的文本.
Rect:= Item.DisplayRect(drBounds); // 获取Item显示的区域.
if Item.Data = nil then // 如果为空则创建CheckBox及Button.
begin
new(P); // 创建一个指针用于存储CheckBox及Button.
{ 创建并显示CheckBox }
P.viewCheckBox:= TCheckBox.Create(ListView1);
P.viewCheckBox.Parent:= ListView1;
P.viewCheckBox.Caption:= '';
P.viewCheckBox.Width:= 20;
P.viewCheckBox.Height:= 20;
P.viewCheckBox.Left:= Rect.Right - ListView1.Columns[2].Width
- ListView1.Columns[3].Width
- ((ListView1.Columns[1].Width + P.viewCheckBox.Width) div 2);
P.viewCheckBox.Top:= Rect.Top;
P.viewCheckBox.Visible:= True; // showmessage(inttostr(SubItem));
{ SubItems[2 -1].Caption为0和1,直接转换为Boolean型并给CheckBox赋值. }
P.CheckBox.Checked:= StrToBool(Item.SubItems[SubItem-1]);
//创建并显示Button
{P.Button:= TRadioButton.Create(ListView1);
P.Button.Parent:= ListView1;
P.Button.Caption:= '...';
P.Button.Width:= 20;
P.Button.Height:= 20;
P.Button.Left:= Rect.Right - ((ListView1.Columns[3].Width
+ P.Button.Width) div 2);
P.Button.Top:= Rect.Top;
P.Button.Visible:= True;}
P.CheckBox:= TCheckBox.Create(ListView1);
P.CheckBox.Parent:= ListView1;
P.CheckBox.Caption:= '';
P.CheckBox.Width:= 20;
P.CheckBox.Height:= 20;
P.CheckBox.Left:= Rect.Right - ListView1.Columns[3].Width - ((ListView1.Columns[2].Width + P.CheckBox.Width) div 2);
P.CheckBox.Top:= Rect.Top;
P.CheckBox.Visible:= True; ////
P.guidCheckBox:= TCheckBox.Create(ListView1);
P.guidCheckBox.Parent:= ListView1;
P.guidCheckBox.Caption:= '';
P.guidCheckBox.Width:= 20;
P.guidCheckBox.Height:= 20;
P.guidCheckBox.Left:= Rect.Right - ((ListView1.Columns[3].Width + P.guidCheckBox.Width) div 2);
P.guidCheckBox.Top:= Rect.Top;
P.guidCheckBox.Visible:= True;
Item.Data:= P; // 将CheckBox及Button的结构指针保存于Item.Data属性.
end; end;
end;
下边是画的控件代码。 知道快帮我写下代码。。谢谢了。
1.先定义结构:type
PItemCtrl = ^TItemCtrl;
TItemCtrl = record
viewCheckBox: TCheckBox;
CheckBox: TCheckBox;
guidCheckBox: TCheckBox;
end; 2.在listview的CustomDrawSubItem事件中写如下代码;var
Rect: TRect;
P: PItemCtrl;
begin
if SubItem in [1,2,3] then
begin
DefaultDraw:= False; // 不显示默认的文本.
Rect:= Item.DisplayRect(drBounds); // 获取Item显示的区域.
if Item.Data = nil then // 如果为空则创建CheckBox及Button.
begin
new(P); // 创建一个指针用于存储CheckBox及Button.
{ 创建并显示CheckBox }
P.viewCheckBox:= TCheckBox.Create(ListView1);
P.viewCheckBox.Parent:= ListView1;
P.viewCheckBox.Caption:= '';
P.viewCheckBox.Width:= 20;
P.viewCheckBox.Height:= 20;
P.viewCheckBox.Left:= Rect.Right - ListView1.Columns[2].Width
- ListView1.Columns[3].Width
- ((ListView1.Columns[1].Width + P.viewCheckBox.Width) div 2);
P.viewCheckBox.Top:= Rect.Top;
P.viewCheckBox.Visible:= True; // showmessage(inttostr(SubItem));
{ SubItems[2 -1].Caption为0和1,直接转换为Boolean型并给CheckBox赋值. }
P.CheckBox.Checked:= StrToBool(Item.SubItems[SubItem-1]);
//创建并显示Button
{P.Button:= TRadioButton.Create(ListView1);
P.Button.Parent:= ListView1;
P.Button.Caption:= '...';
P.Button.Width:= 20;
P.Button.Height:= 20;
P.Button.Left:= Rect.Right - ((ListView1.Columns[3].Width
+ P.Button.Width) div 2);
P.Button.Top:= Rect.Top;
P.Button.Visible:= True;}
P.CheckBox:= TCheckBox.Create(ListView1);
P.CheckBox.Parent:= ListView1;
P.CheckBox.Caption:= '';
P.CheckBox.Width:= 20;
P.CheckBox.Height:= 20;
P.CheckBox.Left:= Rect.Right - ListView1.Columns[3].Width - ((ListView1.Columns[2].Width + P.CheckBox.Width) div 2);
P.CheckBox.Top:= Rect.Top;
P.CheckBox.Visible:= True; ////
P.guidCheckBox:= TCheckBox.Create(ListView1);
P.guidCheckBox.Parent:= ListView1;
P.guidCheckBox.Caption:= '';
P.guidCheckBox.Width:= 20;
P.guidCheckBox.Height:= 20;
P.guidCheckBox.Left:= Rect.Right - ((ListView1.Columns[3].Width + P.guidCheckBox.Width) div 2);
P.guidCheckBox.Top:= Rect.Top;
P.guidCheckBox.Visible:= True;
Item.Data:= P; // 将CheckBox及Button的结构指针保存于Item.Data属性.
end; end;
end;
if ListView1.Items[i].Checked then
你的方法我知道我是要画在别的行。继续给力啊代码里已经将P传给ListItem的Data,根据Data就可以设置ListItem所对应的那几个CheckBox的Checked属性了、、要怎么判断,我试了下,没法子判断,请给出代码好么?谢谢
plvcol = ^lvcol
lvcol = record
col1: boolean;
col2: boolean;
...
end然后使用 plvcol 传给Data就可以操作了.数组 array of integer 也行啊!!重画的时候,根据Data读取,然后遍历数组或变体来确定是否为选上了.
begin
if PItemCtrl(self.ListView1.Items[i].Data).viewCheckBox.Checked then
showmessage(format('%d viewCheckBox',[i]));
if PItemCtrl(self.ListView1.Items[i].Data).CheckBox.Checked then
showmessage(format('%d CheckBox',[i]));
if PItemCtrl(self.ListView1.Items[i].Data).guidCheckBox.Checked then
showmessage(format('%d guidCheckBox',[i]));
end;