Search code examples
c#vb.netwinformstelerikraddropdownbutton

Telerik, handle the clicking on RadDropDownButton text zone


I have a RadDropDownButton with predefined text and with one RadMenuItem:

enter image description here

My intention is to perform an action when I click on the text zone (NOT on the arrow):

enter image description here

And then perform other action when I click on the selectable item:

enter image description here

Handling the RadMenuItem.Click is done, no problem with that, but the RadDropDownButton.Click event fires when I click everywhere on the control and not only in the text zone.

How I can fix this to let the control be working as I wish?

Private sub MyRadDropDownButton_click() handles MyRadDropDownButton.click

    ' this instruction should be launched only when clicking on the "Analyze" word.
    ' this means everywhere on the control but not on the arrow.
    msgbox("you've clicked on the "Analyze" word")

end sub

Solution

  • Their SplitButton is a bit braindead, IMO. Most SplitButtons treat the arrow area as a virtual button and either skip issuing the Button CLick event or Show the associated drop down menu instead (or both). Most use a new SplitClicked event when that area is clicked so you can fiddle with the menu as needed:

    Protected Overrides Sub OnMouseDown(ByVal mevent As MouseEventArgs)
        ...
    
        ' they clicked in the arrow.split rect
        If (SplitRect.Contains(mevent.Location)) Then
    
            ' notify them 
            RaiseEvent SplitClick(Me, New EventArgs)
    
            ' open the menu if there is one
            If ShowContextMenuStrip() = False Then
                skipNextClick = True       ' fixup for the menu
            End If         
    
        Else
            ' let the normal event get raised
            State = PushButtonState.Pressed
            MyBase.OnMouseDown(mevent)
        End If
    
    End Sub 
    

    They have no similar event, but as a workaround, you can use the DropDownOpening event to "cancel" the button click event like so (this works because the DropDownOpening event always fires first):

    ' workaround flag
    Private IgnoreClickBecauseMenuIsOpening As Boolean
    Private Sub RadSplitButton1_DropDownOpening(sender As Object, 
              e As EventArgs) Handles RadSplitButton1.DropDownOpening
    
        IgnoreClickBecauseMenuIsOpening = True
        ' code to modify menu (or not)
    
    End Sub
    
    Private Sub RadSplitButton1_Click(sender As Object, 
            e As EventArgs) Handles RadSplitButton1.Click
    
        ' ignore click if menu is opening
        If IgnoreClickBecauseMenuIsOpening Then
            ' reset flag
            IgnoreClickBecauseMenuIsOpening = False
            Exit Sub                    ' all done here
        End If
        ' normal code to execute for a click
    End Sub