Does anyone know how to prevent users from selecting a row in the DesktopTable? The “Select Type” options are ‘Single’ or “Multi,” but “None” isn’t available.
With the DesktopListbox, you could simply set me.SelectedRowIndex = -1 in the SelectionChanged event, but that causes a stack overflow in the DesktopTable.
Incidentally, it’s not that easy to switch from the DesktopListbox to the DesktopTable, since the events for drawing the content are called differently here.
This in the SelectionChanged works:
If Me.SelectedRowIndex<>-1 Then
Me.RowSelectedAt(Me.SelectedRowIndex)=False
End If
Thanks for the quick reply. It works this way.
But the DesktopTable triggers the SelectionChanged event twice when the mouse is clicked and released.
That can’t be right—even when you click the same row.
You can Return True from MouseDown, but that’s very brute force.
The problem is the SelectionChanged event and not the MouseDown event.
Right, but returning true from MouseDown allows you to also enforce no selection in the DesktopTable. You can do the the same with KeyDown. It all depends on what you’re trying to achieve, which we don’t know.
An alternative is to set a property to track the source of the change. IE:
Private Property isSettingValue As Boolean
Then, in your Table’s SelectionChanged event:
if isSettingValue then Return
isSettingValue = True
if me.SelectedRowIndex >= 0 then me.SelectedRowIndex = -1
isSettingValue = False
Use the CellPressed Event.
You can also solve the problem with a SubClass, I am of the opinion that the events should do what the name promises.
The event does do what the name promises. It’s notifying you that the selection changed, one is happening as a result of user interaction, then you’re triggering it again programmatically. That’s the behavior that you’re not expecting.
This was a big debate some years ago. How should it behave? One camp believed that the event should not be raised as a result of code changing the selection (because of issues like this one), the other believed it should always be raised. Xojo chose the latter and have been implementing it that way since. At least that’s how I remember it, and this instance appears to bear that out.
But what is new is that the SelectionChanged event is called at MouseDown and MouseUp. But actually the DesktopTable should behave like the DesktopListBox.
DesktopTable is new. It’s not DesktopListBox, so the concessions that were made in the past to not break existing projects don’t apply. It’s implemented using Xojo’s new standard.
For most controls, I subclass them and add an additional boolean parameter to the “changed” events called UserChanged that is True if the user changed it, False if it was changed programmatically. It’s a bit of extra work but I keep them in my template project and just use them in everything.
Yeah, totally an option, I was just providing the code for the idea. They then said “I could create a subclass”.
I have conducted some tests to determine the differences between DesktopListBox and DesktopTable.
When you click on a row (press the mouse button), DesktopListBox triggers the “CellPressed” event; upon releasing the mouse button, the “SelectionChanged” event follows.
In the case of DesktopTable, however, clicking on a row triggers both the “CellPressed” and “SelectionChanged” events. When the mouse button is released, the “SelectionChanged” event is triggered again—unless you click on a different row.
What is the logic behind this?
I have created a subclass that solves the problem for me.
#tag Class
Protected Class RPTable
Inherits DesktopTable
#tag Event
Sub Opening()
if Stripes then
AlternatingRowColor = New ColorGroup(&cF2F4F7, &c2B2F33)
HasAlternatingRowColors = True
end if
RaiseEvent Opening
End Sub
#tag EndEvent
#tag Event
Sub SelectionChanged()
if not KeinChangeEvent and vLastRowPos <> SelectedRowIndex then
RaiseEvent SelectionChanged
vLastRowPos = SelectedRowIndex
end if
End Sub
#tag EndEvent
#tag Method, Flags = &h0
Sub DeselectAllRows()
if SelectedRowIndex <> -1 then
KeinChangeEvent = true
SelectedRowIndex = DesktopTable.NoSelection
KeinChangeEvent = false
end if
End Sub
#tag EndMethod
#tag Method, Flags = &h0
Sub SetSelectedRowIndex(Assigns x As Integer)
KeinChangeEvent = true
SelectedRowIndex = x
KeinChangeEvent = false
End Sub
#tag EndMethod
#tag Hook, Flags = &h0
Event Opening()
#tag EndHook
#tag Hook, Flags = &h0
Event SelectionChanged()
#tag EndHook
#tag Property, Flags = &h21
Private KeinChangeEvent As Boolean
#tag EndProperty
#tag Property, Flags = &h0
Stripes As Boolean
#tag EndProperty
#tag Property, Flags = &h21
Private vLastRowPos As Integer = -2
#tag EndProperty
End Class
#tag EndClass