When checking or unchecking the checkbox in the vst, I want to ask for confirmation in some cases.
(Un)checking works fine until I open a MessageBox
from the OnChecking
event handler.
When I have shown the MessageBox
(and set Allowed
to true) the checkbox state does not change and I have to click a second time in order to toggle the checkbox.
For any reason I haven't figured out yet, the second time the OnChecking
event handler isn't called.
It seems to be related with the focus: if I click on another node before the second click to the checkbox it doesn't work a all. I'm using Delphi XE2 and Vitual Treeview 5.3.
Can somebody confirm this behavior and think of a fix/workaround?
This MCVE shows the behavior. Just add a button and a vst to a form and assign the event handlers:
type
TMyData = class
public
value: String;
constructor Create(str: String);
end;
constructor TMyData.Create(str: String);
begin
value := str;
end;
procedure TForm3.btnInitTreeClick(Sender: TObject);
begin
VirtualStringTree1.NodeDataSize := Sizeof(TObject);
VirtualStringTree1.TreeOptions.MiscOptions := VirtualStringTree1.TreeOptions.MiscOptions + [toCheckSupport];
VirtualStringTree1.CheckImageKind := ckSystemDefault;
with VirtualStringTree1.Header.Columns.Add do
begin
Text := 'Colum header';
Width := 150;
end;
VirtualStringTree1.AddChild(nil, TMyData.Create('1')).CheckType := ctCheckBox;
VirtualStringTree1.AddChild(nil, TMyData.Create('2')).CheckType := ctCheckBox;
VirtualStringTree1.AddChild(nil, TMyData.Create('A')).CheckType := ctCheckBox;
VirtualStringTree1.AddChild(nil, TMyData.Create('B')).CheckType := ctCheckBox;
end;
procedure TForm3.VirtualStringTree1Checking(Sender: TBaseVirtualTree;
Node: PVirtualNode; var NewState: TCheckState; var Allowed: Boolean);
var
data: TObject;
begin
data := TObject(Sender.GetNodeData(Node)^);
if assigned(data) and (data is TMyData) and (TMyData(data).value = 'A') then
Allowed := Application.MessageBox('Are you sure?', 'Confirmation', MB_YESNO or MB_ICONQUESTION) = ID_YES
else
Allowed := true;
end;
procedure TForm3.VirtualStringTree1GetText(Sender: TBaseVirtualTree;
Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
var CellText: string);
var
data: TObject;
begin
data := TObject(Sender.GetNodeData(Node)^);
if assigned(data) and (data is TMyData) then
CellText := TMyData(data).value
end;
Edit: The problem also can be reproduced with version 5.5.2