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

single click calls ContextMenuItem event multiple times

4 Answers 100 Views
ContextMenu
This is a migrated thread and some comments may be shown as answers.
Gone2TheDogs
Top achievements
Rank 1
Veteran
Gone2TheDogs asked on 07 Dec 2018, 03:14 PM

I wrote some code to capture the user's selected values before changing them. Thus, allowing the changes to be restored to the previous values. 

The problem I ran into was that my click event from the contextmenu is called multiple times. This wipes out the true previous values. I could just add a flag at the beginning of the event, but I was wondering if there was a better way to approach this? why is this click event called multiple times on a single click?

 

01.Private Sub FF_MenuItem_Click(sender As Object, e As EventArgs) Handles FF_MenuItem.Click
02.'Populate selected cells with "FF"
03.Try
04.Dim cell As GridViewCellInfo
05. 
06.If dgvMain.SelectedCells.Count > 0 Then
07._lastGridChange.Clear
08.End If
09. 
10.For Each cell In dgvMain.SelectedCells
11._lastGridChange.Add(cell.RowInfo.Index & "," & cell.ColumnInfo.Index & "," & cell.Value)
12.cell.Value = "FF"
13.Next
14.Catch ex As Exception
15.ShowErrorBox("No cell selection detected. Be sure to select a cell first.")
16.End Try
17.End Sub

Line 11.  _lastGridChange saves the values before they are updated

 

 

 

01.Private Sub dgvMain_ContextMenuOpening(sender As Object, e As ContextMenuOpeningEventArgs) Handles dgvMain.ContextMenuOpening
02. 
03.    FF_MenuItem.Text = "FF"
04.    HH_MenuItem.Text = "HH"
05.    CC_MenuItem.Text = "CC"
06.    ClearColumn_MenuItem.Text = "Clear Column Values"
07. 
08.    Dim separator As RadMenuSeparatorItem = New RadMenuSeparatorItem()
09.    e.ContextMenu.Items.Add(separator)
10. 
11.    Try
12.        e.ContextMenu.Items.Add(FF_MenuItem)
13.        e.ContextMenu.Items.Add(HH_MenuItem)
14.        e.ContextMenu.Items.Add(CC_MenuItem)
15.        e.ContextMenu.Items.Add(ClearColumn_MenuItem)
16.    Catch ex As Exception
17.    End Try
18. 
19.    Try
20.        AddHandler FF_MenuItem.Click, AddressOf FF_MenuItem_Click
21.        AddHandler HH_MenuItem.Click, AddressOf HH_MenuItem_Click
22.        AddHandler CC_MenuItem.Click, AddressOf CC_MenuItem_Click
23.        AddHandler ClearColumn_MenuItem.Click, AddressOf ClearColumn_MenuItem_Click
24.    Catch ex As Exception
25.    End Try
26.End Sub

Setting up the ContextMenu Options

 

 

 

 

 

4 Answers, 1 is accepted

Sort by
0
Gone2TheDogs
Top achievements
Rank 1
Veteran
answered on 07 Dec 2018, 03:35 PM

From original post:  "...I could just add a flag at the beginning of the event,..."

> Actually, I'm stumped. Flag won't work here. I can't find a way to make this work. Any suggestions would be appreciated.

0
Gone2TheDogs
Top achievements
Rank 1
Veteran
answered on 07 Dec 2018, 04:49 PM

I found a way to get the flag to work. I was looking for a way to clear the flag at the appropriate time. The grid_click event did the job. 

I'm still curious why there a multiple calls to the MenuItem click event on a single click. 

Also, was wondering if there was a better way to undo the last change other than the way I came up with.

 

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 10 Dec 2018, 01:31 PM
Hello, Bob,    

According to the provided code snippet, it seems that you initialize the RadMenuItems outside the ContextMenuOpening event. However, you subscribe to the Click event of each item every time the ContextMenuOpening event is fired. In order to avoid multiple firing of the event, please ensure that you first unsubscribe from this event:

Dim FF_MenuItem As New RadMenuItem
Dim HH_MenuItem As New RadMenuItem
Dim CC_MenuItem As New RadMenuItem
Dim ClearColumn_MenuItem As New RadMenuItem
Private Sub dgvMain_ContextMenuOpening(sender As Object, e As ContextMenuOpeningEventArgs) Handles RadGridView1.ContextMenuOpening
 
    FF_MenuItem.Text = "FF"
    HH_MenuItem.Text = "HH"
    CC_MenuItem.Text = "CC"
    ClearColumn_MenuItem.Text = "Clear Column Values"
 
    Dim separator As RadMenuSeparatorItem = New RadMenuSeparatorItem()
    e.ContextMenu.Items.Add(separator)
 
    Try
        e.ContextMenu.Items.Add(FF_MenuItem)
        e.ContextMenu.Items.Add(HH_MenuItem)
        e.ContextMenu.Items.Add(CC_MenuItem)
        e.ContextMenu.Items.Add(ClearColumn_MenuItem)
    Catch ex As Exception
    End Try
 
    Try
        RemoveHandler FF_MenuItem.Click, AddressOf FF_MenuItem_Click
        RemoveHandler HH_MenuItem.Click, AddressOf HH_MenuItem_Click
        RemoveHandler CC_MenuItem.Click, AddressOf CC_MenuItem_Click
        RemoveHandler ClearColumn_MenuItem.Click, AddressOf ClearColumn_MenuItem_Click
 
        AddHandler FF_MenuItem.Click, AddressOf FF_MenuItem_Click
        AddHandler HH_MenuItem.Click, AddressOf HH_MenuItem_Click
        AddHandler CC_MenuItem.Click, AddressOf CC_MenuItem_Click
        AddHandler ClearColumn_MenuItem.Click, AddressOf ClearColumn_MenuItem_Click
    Catch ex As Exception
    End Try
End Sub

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Gone2TheDogs
Top achievements
Rank 1
Veteran
answered on 10 Dec 2018, 04:54 PM
I defined the menuItems in the ContextMenuOpening. That corrected it. Thank you for replying.
Tags
ContextMenu
Asked by
Gone2TheDogs
Top achievements
Rank 1
Veteran
Answers by
Gone2TheDogs
Top achievements
Rank 1
Veteran
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or