To find all the event handlers, and there are many, we are going to add showEvents to the On testButton Pressed
handler. Looking in the listener you can see all the available events, the one that we are interested in is the
on mouseDown event.
Code:
on testButton pressed do
(
clearListener() --Clear the listener.
format "Props\n" --Format a header so we know what we are looking at below.
showProperties lv --Show the properties of the listView control.
format "\nMethods\n" --Format a header so we know what we are looking at below.
showMethods lv --Show the properties of the listView control.
format "\mEvents\n" --Format a header so we know what we are looking at below.
showEvents lv --Show the properties of the listView control.
)
All the events are passed a MouseEventArgs that can be used to access information about the event.
Note that different events will return different MouseEventArgs so you might need to deal with them
differently.
Listener:
on MouseDown <System.Windows.Forms.MouseEventArgs>e do ( ... )
In this case if we do a showProperties on the mouseEventArgs will will see seven options. Click on
any one of the list items to display them in the listener.
Code:
on lv mouseDown arg do
(
clearListener()
showProperties arg
)
Listener:
.Button : <System.Windows.Forms.MouseButtons>, read-only
.Clicks : <System.Int32>, read-only
.Delta : <System.Int32>, read-only
.Location : <System.Drawing.Point>, read-only
.X : <System.Int32>, read-only
.Y : <System.Int32>, read-only
.Empty : <System.EventArgs>, read-only, static
It is interesting to note that none of the returned values are the actual listViewItem of the row
that we selected. Instead what we do get are the .X and .Y positions of the mouse in the listView. From the
position of the mouse we can get the row item and the sub items. This feels like a bit of a hack but
it is just the way it is.
In the code below I first showProperties on the mouseEventArgs and find there is a .HitTest property.
The hit test is looking for a "system.drawing.point" object to be passed to it. The point object needs
and X and Y property set and we can use the ones that we get from the mouseEventArgs. We will assign
the return value to a local variable called hit.
Next do a showProperties on the hitTest and there are many more properties, .item is the most obvious
as we are looking for the item that we have clicked on. If we then check the properties on hit.item we can
get the text that is shown in the listViews first column.
The .item property also have a .subItems property that holds an array of all the columns starting at the
first. Note that just about all other languages use arrays that start with the index 0, this is refereed to
as 0 based arrays. If we check the properties of .subItems we find a .item property that holds the array
and also a .count so if we are doing a search and we don't necessarily know how many columns there are we
can find out. Again, make sure your indexed for loop starts at 0 and goes to .count-1.
Some error checking will also be needed to ensure the user is not clicking on a part of the list that
doesn't have a an item under it. You can test this now and see that an error is thrown as there is no
.item property for undefined.
Code:
on lv mouseDown arg do
(
clearListener()
showProperties arg
hit=(lv.HitTest (dotNetObject "System.Drawing.Point" arg.x arg.y))
showProperties hit
showProperties hit.item
print hit.item.text
showProperties hit.item.subItems
print hit.item.subItems.count
print hit.item.subItems.item[1].text
)
|