Jump to content
Mike Torrettinni

How to reliably know if there is data selected in TVirtualStringTree

Recommended Posts

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 by Mike Torrettinni
details

Share this post


Link to post

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 by timfrost
correction

Share this post


Link to post
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 by Mike Torrettinni

Share this post


Link to post
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
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 by Fr0sT.Brutal

Share this post


Link to post

 

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×