3

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
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • can you show the code for both the Click and the ItemChanged event..? why can't you comment out the RadDropDownButton Click or add some code that will check for a specific condition..? – MethodMan Aug 29 '14 at 17:35
  • @DJ KRAZE Sorry but I consider that the question does not require to provide a code 'cause it is self-explanatory, anyways I've added a code (I don't have to handle the ItemChanged event, I only have 1 item),what Ive tried to ask is: just imagine a situation where you want to launch a MsgBox when you click on the control, but you also want be able to open the menu when clicking on the arrow without launching that msgbox ofcourse 'cause if you opened the menu you will select an item that will perform other things (launch a different msgbox for example).Did u understood me? sorry for my english! – ElektroStudios Aug 29 '14 at 18:06
  • Can you check the mouse position against the boundary of the control? – djv Aug 29 '14 at 22:23
  • Use the SplitButton instead - it is pretty much intended for that purpose. – Ňɏssa Pøngjǣrdenlarp Aug 30 '14 at 12:51
  • @Plutonix Telerik says `If you want a button that does something when clicked as well as when a selection is made from the menu, you should use a RadSplitButton instead of a RadDropDownButton` but I face the same problem that I've described in the question (a `Click` event that fires clicking anywhere on the control), if you know how to use a `RadSplitButton` in that way then please illustrate me! thanks for comment – ElektroStudios Aug 30 '14 at 16:46

2 Answers2

2

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
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • is more easier than that, just I neeeded to set the 'DefaultItem' property of the RadSplitButton, then when you click in the "text-area" it perfoms a click on that default item and you are able to click on the arrow to select another item, anyways your answer is a very good alternative that I will test for curiosity! – ElektroStudios Aug 31 '14 at 16:04
  • I think yes :( so your way seems more perfect 'cause I could avoid generating that default item on the control, thanks again – ElektroStudios Aug 31 '14 at 16:06
  • your solution gives problems. after deeping inside the control properties I've found the way checking the property 'RadSplitButton1.DropDownButtonElement.ArrowButton.IsMouseOverElement' – ElektroStudios Sep 02 '14 at 23:23
0

The solution:

I called this "Distinguish an Arrow click without a Default Item set", this works for both RadDropDownButton and RadSplitButton.

Public Class RadSplitButton_TestForm

''' <summary>
''' Flag that determines whether the RadSplitButton menu-opening should be canceled.
''' </summary>
Private CancelOpening As Boolean = False

Private Sub RadSplitButton1_DropDownOpening(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
Handles RadSplitButton1.DropDownOpening

    e.Cancel = Me.CancelOpening

End Sub

Private Sub RadSplitButton1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) _
Handles RadSplitButton1.MouseMove

    Me.CancelOpening = Not sender.DropDownButtonElement.ArrowButton.IsMouseOverElement

End Sub

Private Sub RadSplitButton1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) _
Handles RadSplitButton1.Click

    If e.Button = Windows.Forms.MouseButtons.Left AndAlso Me.CancelOpening Then
        MsgBox("clicked out the arrow!")

    ElseIf Not Me.CancelOpening Then
        MsgBox("clicked over the arrow!")

    End If

End Sub

End Class

PS: Firstly I was tried to determine whether the mouseposition is over the ArrowButton.ClientRectangle but it doesn't give the expected results.

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417