Drag select rows in TListview - delphi

I am trying to figure out a way to select rows in a TListview by dragging over them (like dragging a window over icons in Windows Explorer to select them). This is not about drag and drop, only dragging.
I don't think I need an action on MouseDown since that row is already selected when I click, but I think I need something like:
OnMouseMove check if X,Y is above a row
If so, change its IsSelected to true
If not, do nothing
I don't know if anything is needed on MouseUp
The question is, how can I check if the mouse x.y is within/above a Listview row?

Use GetItemAt method of TListView (and maybe also TListView.GetHitTestInfoAt).

Related

Detect a Dbgrid is in drag mode

How can I detect if a TDbGrid is currently in "drag mode" where a user is dragging a row (or rows) to another component.
I thought that:
if Screen.Cursor = DBGrid.DragCursor then
but that seems too brute force.
I could use the OnStartDrag and OnEndDrag events to keep track myself.
I haven't been able to find a property in TDbGrid that indicates the current mode.
Any suggestions?
You can use Dragging function of the control:
if DBGrid1.Dragging then

Show different popup menu depending on what column the mouse is over in a Delphi TListView control?

I have a Delphi 6 application that has a TJvListView control. I have a popup menu tied to that control via the control's PopupMenu property. What I would like to do is show a different popup menu based on which column the user had the mouse over when they right clicked, with the additional option to not show a popup menu at all if the current column does not need one. How can I do this?
Thanks to this detailed sample by Remy Lebeau on in-place editing in a TListView I know what row and column the mouse is over except for one wrinkle. The mouse down event where I determine the current row and column occurs after the popup menu is exited.
I now need to know two things. First, how can I get some event to fire before the popup menu shows after a right mouse click so I can record the current list view row and column and suppress the popup menu if I want to, and second, how I can show a different popup based on the current column. I am hoping to avoid having to write a bunch of mini-forms instead of using the TListView PopupMenu property and supporting code. Is there a simple solution, perhaps some fancy footwork in a sub-class I should create around TJvListView?
You could perform the detection in mousemove instead of mousedown/Click and change the popupmenu depending.
You also could remove any popupmenu and call the wished via p.pupup in mousedown as you desire.

How do I move multiple nodes at once in a TJvTreeView?

When you select multiple nodes of a JvTreeView, and then try to drag these nodes, the JvTreeView selects the node you clicked to drag, rather than initiating dragging all the selected nodes. You end up dragging only this single node.
Is it possible to drag multiple nodes in a JvTreeView? I am using Delphi 2007.
UPDATE: Oddly enough, if I hold down CTRL+SHIFT when dragging the items, I can successfully drag them all. Any ideas?
Suppose MultiSelectStyle is the default [msControlSelect], then the answer to your question is 'you don't release the control key when you're selecting the last item and beginning dragging'.
The culprit is in TJvTreeView.WMLButtonDown in JvComCtrls.pas. Code there tests if 'Ctrl' is pressed when the TreeView is MultiSelect, and clears all items and selects the clicked item if 'Ctrl' is not pressed. It should instead test if the clicked item is already selected and do nothing if it is.
You can see the broken behavior without dragging. Multi select a few items and then click a selected item with the mouse. The VCL TreeView do not select or de-select anything, while the JVTreeView, instead, de-selects all items and selects the clicked one.
Yes absolutely, I do it all the time.
Of course Multi-Select needs to be True and you may need to have a look at the TTreeView's MultiSelectStyle. That controls what type of nodes can be selected at the same time. For example if msSiblingOnly is set to true, you can only select sibblings.
Are you using dmAutomatic or dmManual. If the latter, it could be that you are doing something in the OnMouse* events that is negating the multi-selection you made earlier.

TStringGrid dirty hack - Restricting the selection to one single row

I have a heavily modified control based on TStringGrid. I want to allow the user to make selections in this grid but restrict the selection one single row (the current row).
Implementation:
When the user presses the left mouse button I am using a dirty hack: On OnMouseDown event I capture the mouse cursor and keep it on the current row. The user can move the mouse device up and down on its pad but the cursor will not go up or down. It will stay on the current row. When the user releases the button (OnMouseUp event), I release the capture.
However, this hack is as I said very dirty. There are several problems. For example, if the user presses the left mouse button (LMB) and then without releasing that button it presses the right button, the associated pop-up menu will pop but the mouse capture will never be released. So, the mouse will be locked in a screen region until the user will has the brilliant idea to click the LMB one more time to unlock the mouse. This may be a bug in D7. There is a separate post about it here: TStringGrid - OnMouseUp is not called!
There is a elegant way to do this?
Edit:
OnSelectCell is not working. OnSelectCell event is called only once when you click the cell. If you keep the button pressed and move the mouse to expand the selection, OnSelectCell will not be called again.
Indeed MoveCurrent appears only in TCustomGrid.MouseDown.
Use the OnSelectCell event and set the CanSelect var parameter depending on whether the ARow parameter is what you want.
A nice solution
Check the options property on TStringGrid
There is an Option called 'goRangeSelect'
Set this to false using the object inspector
Or programatically this can be done by
StringGrid1.Options - [goRangeSelect];
This answer is not elegant at all, but it works.
My solution is NOT to use the PopupMenu property of the StringGrid. Instead I implemented my own PopUpMenu property. Works almost perfect. There is on small problem, the bottom of the pop-up menu appears next to the cursor and not its top.

Cause 'hint' to refire on listview as I move over items

Sure I've seen this done before but off-hand I can't find any examples.
I've got a TListView, set in 'report' viewstyle. It has about half a dozen subitems, and one thing we'd like to do is have the 'hint' (tooltip) on the listview dynamically show another field of data. That is, each time you move the mouse over any given row, the 'hint' would show some text relevant to that particular row.
I'm partway there - I can do this using the OnInfoTip method, but unfortunately once a tip has appeared, Windows seems to decide that I don't need to see a hint for the listview again until I move the mouse away from the listview and then back 'over' it again. Simply moving the mouse down to the next row, all-the-time keeping the mouse over the control, doesn't persuade the program to display the new hint.
Just to be clear - I've got OnInfoTip working so that the program does display the right hint relevant to the item I first moved the mouse over. Changing the hint text isn't the issue. The problem is that moving the mouse to another item in the listview doesn't cause the software to show a new hint. (Hope that makes sense).
Is there some proper way of getting this behaviour to work, or am I going to end up doing something icky with mouseovers and then manually drawing a hintbox (etc)?
check the following link:
Display Custom Hints for TListView Sub Items
Edit:
I just checked it now on delphi7 it's showing the hint for every row dynamically after moving the mouse on the listview.
Offtopic: This is simple in Virtual Treeview component, it is build-in feature.
i was using the OnInfoTip event (i didn't need hints for the subitems). the hint was "flashing" (show/hide/show/hide/show/hide/show/hide). found the listview's ShowHint was false. set it to True and it worked as it should.

Resources