关于TListView的OnChanging消息(100分)

  • 主题发起人 主题发起人 晓鹤
  • 开始时间 开始时间

晓鹤

Unregistered / Unconfirmed
GUEST, unregistred user!
在TListView的OnChanging消息中如何区分以下操作:
1、无选择项目时,点击一项目选中;
2、有选择项目时,点击另一项目改变选择;
3、有选择项目时,点击原选择项目;
4、点击项目前的CheckBox,改变CheckBox的选择状态(包括当前选择的项目、其它项目);
 
1、2:
if ListView1.Selected = nil then
ShowMessage('无选择项目时,点击一项目选中')
else
ShowMessage('有选择项目时,点击另一项目改变选择')

3:
if ListView1.Selected <> nil then
AllowChange := False;
 
CheckBox1.Checked:=True;
注意,不要在它的Click事件中写:CheckBox1.Checked:=not CheckBox1.Checked;
 
我是要区分4种操作的不同,而不是要得到ItemIndex、ItemFocused、selected
的值。
 
这些状态好象是没办法区分的,因为listview的onchanging里只有三种状态,就是:
ctText, ctImage, ctState,具体的说明是:
ctText A change to the Caption property of the list item.
ctImage A change to the list item抯 ImageIndex property or the list view抯 image list.
ctState A change to the list item抯 Cut, Focused, or Selected property.

楼上所说的四种改变状态都是属于ctState的改变。
 
to chenxz:我就是想在OnChanging中区分ctState的几种不同情况,你能有什么办法吗?
 
你只需要判断Change为csState,然后再根据我上面写的判断就可以了。
难道不是吗?
 
to yzhshi:1、3如何区分。
我是要同时区分四种状态。
 
>>3、有选择项目时,点击原选择项目;
能详细解释一下你的需求吗?我上面给的单独对3的代码不可以吗?
 
我是要在选择项目时检查数据是否要保存,如用户要保存,则取消掉选择操作,
恢复原选择项目;如用户不要保存则正常选择新项目。同时在用户选择一个项目时,
显示一些项目的相关信息给用户修改。在用户改变CheckBox是要记录下来数据改变。
所以要同时区分四种状态。
 
procedure TForm1.ListView1Changing(Sender: TObject; Item: TListItem;
Change: TItemChange; var AllowChange: Boolean);
begin
if ctstate in [change] then
begin
if ListView1.Selected = nil then
Caption := '原来没有选择什么,让它改变吧'
else if CheckBox1.Checked then
begin
if Application.MessageBox('数据已经修改,是否继续?', '询问', 32 + mb_YesNo) = mrNo then
AllowChange := False
else
AllowChange := True
end;
end;
end;
 
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1 : TForm1;

implementation

var
vl_SelItem: Integer;
vl_Chk_Item: array of Boolean;

{$R *.DFM}

procedure TForm1.ListView1Changing(Sender: TObject; Item: TListItem;
Change: TItemChange; var AllowChange: Boolean);
begin
if (Item.Checked <> vl_Chk_Item[Item.Index]) then
begin
vl_Chk_Item[Item.Index] := not vl_Chk_Item[Item.Index];
ShowMessage('改变');
Exit;
end;

if vl_SelItem = -1 then
ShowMessage('原来没项目,点击一项目选中')
else
if vl_SelItem <> Item.Index then
ShowMessage('原来有项目,选中了另一项目')
else
ShowMessage('原来有项目,选中了原来同一项目');

vl_SelItem := Item.Index;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
I : Integer;
l_Item : TListItem;
begin
ListView1.OnChanging := nil;

for I := 0 to 20 do
with ListView1 do
begin
l_Item := Items.Add;
l_Item.Caption := IntToStr(I);
end;

SetLength(vl_Chk_Item, ListView1.Items.Count);
for I := 0 to ListView1.Items.Count - 1 do
vl_Chk_Item := ListView1.Items.Checked;

vl_SelItem := -1;

ListView1.OnChanging := ListView1Changing;
end;
 
消息序列:
无选择项目,选择一个新项目。
=================================================================
OnChanging Item
Caption = 1
Focused = False
Selected = False
-----------------------------------------------------------------
ListView.Selected
Selected = nil
=================================================================
=================================================================
OnChange Item
Caption = 1
Focused = True
Selected = True
-----------------------------------------------------------------
ListView.Selected
Caption = 1
Focused = True
Selected = True
=================================================================
=================================================================
SelectItem Item
Caption = 1
Focused = True
Selected = True
-----------------------------------------------------------------
ListView.Selected
Caption = 1
Focused = True
Selected = True
=================================================================
已有选择项目,选择新项目
=================================================================
OnChanging Item
Caption = 1
Focused = True
Selected = True
-----------------------------------------------------------------
ListView.Selected
Caption = 1
Focused = True
Selected = True
=================================================================
=================================================================
OnChange Item
Caption = 1
Focused = False
Selected = True
-----------------------------------------------------------------
ListView.Selected
Caption = 1
Focused = False
Selected = True
=================================================================
=================================================================
OnChanging Item
Caption = 1
Focused = False
Selected = True
-----------------------------------------------------------------
ListView.Selected
Caption = 1
Focused = False
Selected = True
=================================================================
=================================================================
OnChange Item
Caption = 1
Focused = False
Selected = False
-----------------------------------------------------------------
ListView.Selected
Selected = nil
=================================================================
=================================================================
SelectItem Item
Caption = 1
Focused = False
Selected = False
-----------------------------------------------------------------
ListView.Selected
Selected = nil
=================================================================
=================================================================
OnChanging Item
Caption = 4
Focused = False
Selected = False
-----------------------------------------------------------------
ListView.Selected
Selected = nil
=================================================================
=================================================================
OnChange Item
Caption = 4
Focused = True
Selected = True
-----------------------------------------------------------------
ListView.Selected
Caption = 4
Focused = True
Selected = True
=================================================================
=================================================================
SelectItem Item
Caption = 4
Focused = True
Selected = True
-----------------------------------------------------------------
ListView.Selected
Caption = 4
Focused = True
Selected = True
=================================================================
选择自己
=================================================================
OnChanging Item
Caption = 1
Focused = True
Selected = True
-----------------------------------------------------------------
ListView.Selected
Caption = 1
Focused = True
Selected = True
=================================================================
=================================================================
OnChanging Item
Caption = 1
Focused = True
Selected = True
-----------------------------------------------------------------
ListView.Selected
Caption = 1
Focused = True
Selected = True
=================================================================
已有选择项目,选择无
=================================================================
OnChanging Item
Caption = 1
Focused = True
Selected = True
-----------------------------------------------------------------
ListView.Selected
Caption = 1
Focused = True
Selected = True
=================================================================
=================================================================
OnChange Item
Caption = 1
Focused = True
Selected = False
-----------------------------------------------------------------
ListView.Selected
Selected = nil
=================================================================
=================================================================
SelectItem Item
Caption = 1
Focused = True
Selected = False
-----------------------------------------------------------------
ListView.Selected
Selected = nil
=================================================================
有选择项目,选择无后,在点击空白
=================================================================
OnChanging Item
Caption = 4
Focused = True
Selected = False
-----------------------------------------------------------------
ListView.Selected
Selected = nil
=================================================================s

Check其它项目
=================================================================
OnChanging Item
Caption = 4
Focused = False
Selected = False
-----------------------------------------------------------------
ListView.Selected
Caption = 1
Focused = True
Selected = True
=================================================================
=================================================================
OnChange Item
Caption = 4
Focused = False
Selected = False
-----------------------------------------------------------------
ListView.Selected
Caption = 1
Focused = True
Selected = True
=================================================================
Check自己
=================================================================
OnChanging Item
Caption = 1
Focused = True
Selected = True
-----------------------------------------------------------------
ListView.Selected
Caption = 1
Focused = True
Selected = True
=================================================================
=================================================================
OnChange Item
Caption = 1
Focused = True
Selected = True
-----------------------------------------------------------------
ListView.Selected
Caption = 1
Focused = True
Selected = True
=================================================================
 
改在Click事件中自己判断解决。
 

Similar threads

后退
顶部