Mike Torrettinni 198 Posted August 7, 2019 (edited) I hope someone can help me clear up this part of my refactoring process: I have many VSTs (TVirtualStringTree) and many handlers OnClick, OnMouseup, OnPaintText and others. Usually shows detail info or executes actions based on selected line in this VST. One of the areas I want to refactor/clean up is the beginning of these methods, where I make sure there is actual selected data in VST, and I have many different implementations that are any one of these statements or combination of them. The purpose of all these conditions is to stop processing methods if there is no data selected - empty VST is clicked on; uses clicks on edge margins which don't actually select line;.. or to prevent any anomalies where line data is not there: // in some methods I only have this if TVirtualStringTree(Sender).FocusedNode = nil then Exit; // and in others I have any of these or combination of them: TVirtualStringTree(Sender).FocusedNode = nil not TVirtualStringTree(Sender).Visible TVirtualStringTree(Sender).GetFirst <> nil Sender = nil TVirtualStringTree(Sender) = nil if any of these are True = Exit; And after this I usually have this: Data := TVirtualStringTree(Sender).GetNodeData(TVirtualStringTree(Sender).FocusedNode); if Data = nil then Exit; in some cases I have this: Node := TBaseVirtualTree(Sender).GetFirstSelected; if Node = nil then Exit; Is there a better, simpler way than having all these conditions? Thank you! Edited August 7, 2019 by Mike Torrettinni details Share this post Link to post
timfrost 78 Posted August 7, 2019 (edited) It sounds as if in your VT1mousedown event you should first call vt1.GetHitTestInfoAt to find out whether the mouse has been clicked on a node or not. Once you have the node, it is up to you to work out what you have put in it and whether it is a selected/focused node, and what to do with it if so. Presumably you always ensure that there is a node type or tag in the nodedata. Edited August 7, 2019 by timfrost correction Share this post Link to post
Mike Torrettinni 198 Posted August 8, 2019 (edited) 4 hours ago, timfrost said: It sounds as if in your VT1mousedown event you should first call vt1.GetHitTestInfoAt to find out whether the mouse has been clicked on a node or not. Once you have the node, it is up to you to work out what you have put in it and whether it is a selected/focused node, and what to do with it if so. Presumably you always ensure that there is a node type or tag in the nodedata. Thanks, I do have this, not at the beginning, but later on in some cases for clicked Column #. Will try to put it as the first thing. Edited August 8, 2019 by Mike Torrettinni Share this post Link to post
David Heffernan 2345 Posted August 8, 2019 4 hours ago, timfrost said: It sounds as if in your VT1mousedown event you should first call vt1.GetHitTestInfoAt to find out whether the mouse has been clicked on a node or not. Once you have the node, it is up to you to work out what you have put in it and whether it is a selected/focused node, and what to do with it if so. Presumably you always ensure that there is a node type or tag in the nodedata. What if the user has clicked without using the mouse? Share this post Link to post
Fr0sT.Brutal 900 Posted August 8, 2019 (edited) 10 hours ago, Mike Torrettinni said: TVirtualStringTree(Sender).FocusedNode = nil not TVirtualStringTree(Sender).Visible TVirtualStringTree(Sender).GetFirst <> nil Sender = nil TVirtualStringTree(Sender) = nil Some of these checks are senseless. Sender could not be nil. And you unlikely need GetFirst check unless you really use the first node not a focused one. In most cases, you only need one check for FocusedNode. Edited August 8, 2019 by Fr0sT.Brutal Share this post Link to post
Mike Torrettinni 198 Posted August 8, 2019 3 hours ago, Fr0sT.Brutal said: Some of these checks are senseless. Sender could not be nil. And you unlikely need GetFirst check unless you really use the first node not a focused one. In most cases, you only need one check for FocusedNode. These are various checks throughout the methods and they go back many years, when I might have called the method without Sender, but you are right is not valid anymore. Thanks! Share this post Link to post