-->

TVirtualStringTree. How to check a node and its ch

2019-08-17 20:32发布

问题:

I have a component of type TVirtualStringTree. The option toCheckSupport is enabled. The options related to the propagation of checkstates are also enabled, because the propagation is needed. I want to implement checking a node with a confirmation ("Are you sure you want to check... ?"). Unfortunately, if the options for propagation are enabled, the events OnCheck and OnChecking are triggered including for the child nodes. Therefore, placing the message in the event procedures make it show repeatedly. Do you have any idea how to check a node and its child nodes with a single confirmation?

I think of manual checking, i.e. the user wants to check a node in the tree (and hence all the node's descendants) and is asked for confirmation only once. Checking with confirmation is simple if the user wants to check a leaf: it is sufficient to restore the node to the previous state.

procedure TMyForm.VSTChecked(Sender: TBaseVirtualTree; Node: PVirtualNode);
begin
  if Node.CheckState = csCheckedNormal then
  begin
    if not VST.HasChildren[Node] then
    begin
      if MessageDlg('Are you sure you want to check?', mtConfirmation,
        mbYesNo, 0) <> mrYes then
      begin
        Node.CheckState := csUncheckedNormal;
        PropagateCheckState(VST, Node); // From Node's parent to the root 
      end;
    end;
  end;
end;

I thought about using OnNodeClick in order to identify the exact node that became checked by the user, and (as in my application the significant data are in leaves) about remembering the previous check states of the leaves in order to restore them. Would it lead to a good solution?

回答1:

Use the OnMouseDown event to get notified about a mouse click. If it's TMouseButton.mbLeft call GetHitTestInfoAt() and check if THitPositions.ctCheckBoxis included in THitInfo.HitPositions. If so, THitInfo.HitNodecontains the corresponding node. You may then show the confirmation dialog and save the result in a member variable, that you can use in the OnChecking event to allow or disallow the change of the check state.