This is a migrated thread and some comments may be shown as answers.

Threat a mouse Right-Button Click in the same way as a Left-Button Click?

5 Answers 113 Views
ListControl
This is a migrated thread and some comments may be shown as answers.
Christian
Top achievements
Rank 1
Christian asked on 02 Oct 2014, 09:17 AM
Hi, my intention is to display a RadcontextMenu on a RadListControl when a mouse right-button is pressed or released (depending on the ClickMode configuration of the RadListControl).

I just would like to threat a right-button click in the same way as a left-button click is threated by default in a  RadListControl, but I don't know whether Telerik provides a way to do this just by setting an specific property or somethins similar, what I'm doing is iterating the RadListControl items and determine which item contains the current mouse location but maybe this is a heavy method for big list item collections.

Pleasse. could you check my approach to see if it can be improved/simplified in some way via Telerik?

Also, I have an unsolved problem, in the line 96 of the code below I would like to determine which radListControl called the RadContextMenu to retrieve its SelectedItem.Text property, but I don't know if its possibly.

Thanks in advance.

001.Option Strict On
002.Option Explicit On
003.Option Infer Off
004. 
005.Imports Telerik.WinControls
006.Imports Telerik.WinControls.Enumerations
007.Imports Telerik.WinControls.UI
008. 
009.Public Class RadContextMenu_TestForm
010. 
011.    Private Shadows Sub Load() Handles MyBase.Load
012. 
013.        ' Set the RadListControl behavior
014.        With Me.RadListControl1
015.            .SortStyle = SortStyle.Ascending
016.            .SelectionMode = SelectionMode.One
017.            .ListElement.ClickMode = ClickMode.Release
018.        End With
019. 
020.        ' Set the listRadListControl items
021.        Me.RadListControl1.DataSource =
022.            From p As Process In Process.GetProcesses
023.            Select p.ProcessName
024. 
025.    End Sub
026. 
027.    ''' <summary>
028.    ''' Shows a RAD context menu on the active <see cref="RadListControl"/> item.
029.    ''' </summary>
030.    ''' <param name="ctrl">The <see cref="RadListControl"/> control.</param>
031.    ''' <param name="ctxm">The <see cref="RadContextMenu"/> to show.</param>
032.    Private Sub ShowRadContextMenu(ByVal ctrl As RadListControl,
033.                                   ByVal ctxm As RadContextMenu)
034. 
035.        For Each Item As RadListDataItem In ctrl.Items
036. 
037.            If Item.VisualItem IsNot Nothing AndAlso Item.VisualItem.IsMouseOverElement Then
038.                Item.Selected = True
039.                ctxm.Show(MousePosition)
040.            End If
041. 
042.        Next Item
043. 
044.    End Sub
045. 
046.    ''' <summary>
047.    ''' Handles the MouseDown event of the RadListControl1 control.
048.    ''' </summary>
049.    ''' <param name="sender">The source of the event.</param>
050.    ''' <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
051.    Private Sub RadListControl1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) _
052.    Handles RadListControl1.MouseDown
053. 
054.        Dim ctrl As RadListControl = DirectCast(sender, RadListControl)
055. 
056.        If (e.Button = Windows.Forms.MouseButtons.Right) _
057.        AndAlso (ctrl.ListElement.ClickMode = ClickMode.Press) Then
058. 
059.            Me.ShowRadContextMenu(ctrl, Me.RadContextMenu1)
060. 
061.        End If
062. 
063.    End Sub
064. 
065.    ''' <summary>
066.    ''' Handles the MouseUp event of the RadListControl1 control.
067.    ''' </summary>
068.    ''' <param name="sender">The source of the event.</param>
069.    ''' <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
070.    Private Sub RadListControl1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) _
071.    Handles RadListControl1.MouseUp
072. 
073.        Dim ctrl As RadListControl = DirectCast(sender, RadListControl)
074. 
075.        If (e.Button = Windows.Forms.MouseButtons.Right) _
076.        AndAlso (ctrl.ListElement.ClickMode = ClickMode.Release _
077.                 OrElse
078.                 ctrl.ListElement.ClickMode = ClickMode.Hover) Then
079. 
080.            Me.ShowRadContextMenu(ctrl, Me.RadContextMenu1)
081. 
082.        End If
083. 
084.    End Sub
085. 
086.    ''' <summary>
087.    ''' Handles the Click event of the RadMenuItem1 control.
088.    ''' </summary>
089.    ''' <param name="sender">The source of the event.</param>
090.    ''' <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
091.    Private Sub RadMenuItem1_AddToExclusionList_Click(ByVal sender As Object, ByVal e As EventArgs) _
092.    Handles RadMenuItem1.Click
093. 
094.        If DirectCast(e, MouseEventArgs).Button = Windows.Forms.MouseButtons.Left Then
095. 
096.            MessageBox.Show(DirectCast(RadMenuItem1.OwnerControl, RadListControl).SelectedItem.Text)
097. 
098.        End If
099. 
100.    End Sub
101. 
102.End Class

5 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 06 Oct 2014, 10:16 AM
Hi Christian,

Thank you for writing.

First you can retrieve the the clicked element by using the GetElementAtPoint method. Please note that it is not necessary the clicked item to be the selected one. Nevertheless you can use this in the context menu item to either display the current item text or the clicked one's:
Private clickedElement As RadListVisualItem
 
Private Sub radListControl1_MouseDown(sender As Object, e As MouseEventArgs)
    If e.Button = System.Windows.Forms.MouseButtons.Right Then
        Dim listControl As RadListControl = DirectCast(sender, RadListControl)
 
        clickedElement = TryCast(listControl.ListElement.ElementTree.GetElementAtPoint(e.Location), RadListVisualItem)
 
        If clickedElement IsNot Nothing Then
            menu1.Show(listControl, e.Location)
        End If
    End If
 
End Sub
 
Private Sub item_Click(sender As Object, e As EventArgs)
    RadMessageBox.Show(clickedElement.Text)
 
    Dim selctedItemText As String = TryCast(clickedElement.Parent.Parent, RadListElement).SelectedItem.Text
End Sub

Let me know if you have additional questions.
 
Regards,
Dimitar
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Christian
Top achievements
Rank 1
answered on 07 Oct 2014, 04:11 AM
Thankyou for your time and support.

I have two problems:

1) This instruction of your code  throws an "Object reference not set to an instance of an object" exception, I didn't know how to solve it. also, please could you specify what should handle that event-handler?, is for handle a RadMenuItem.Click event?

2) When doing a left-button click the clicked item is selected in the RadListControl, the RadListControl give focus to it and changes its color, as I explained I would like to threat a right-button click with the same things that are performed for a left-click, well, not all things, but selection is one of the things that I need.

To fix the selection issue I just tried to call 'clickedElement.Select()' method exactlly after you are setting the clickedelement, but after that the clickedElement.Selected property indicates that the item is not selected.
0
Accepted
Dimitar
Telerik team
answered on 09 Oct 2014, 08:49 AM
Hi Christian,

Thank you for writing back.

1. I am not sure why you are getting an exception. This is why I have created a small sample to demonstrate you how this works on my side.

2. You can select the item when it is right clicked with the following code:
Dim listControl As RadListControl = DirectCast(sender, RadListControl)
 
clickedElement = TryCast(listControl.ListElement.ElementTree.GetElementAtPoint(e.Location), RadListVisualItem)
 
If clickedElement IsNot Nothing Then
    radListControl1.SelectedItem = clickedElement.Data 
End If

I hope this helps.

Regards,
Dimitar
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Christian
Top achievements
Rank 1
answered on 19 Oct 2014, 01:40 PM
The exception occurs at this instruction:

Dim selctedItemText As String = TryCast(clickedElement.Parent.Parent, RadListElement).SelectedItem.Text

But with your new modification to select the clicked item now the exception dissapeared.

Thanks so much
0
Dimitar
Telerik team
answered on 21 Oct 2014, 02:49 PM
Hello Christian,

I am glad I could be of help. Let us know if you have any other questions.

Regards,
Dimitar
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
ListControl
Asked by
Christian
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Christian
Top achievements
Rank 1
Share this question
or