Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15083

VS 2019 ContextMenuStrip - click

$
0
0
I currently have a DatagridView and when I right click on a cell, i want a menu strip to appear with the option to Insert Row & Delete Row.

The code below achieves this (display of the menu strip items ,Insert Row & Delete Row)

however when you click on one of the items, i can not get the handler to work -
The error being in the handler
Code:

Private Sub contextMenuDataGrid_Click(sender As Object, e As EventArgs) Handles contextMenuDataGrid.Click
saying a handles clause required


Code:

ublic Class Form1
    Dim index As Integer = 0

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DbTestDataSet.Staff' table. You can move, or remove it, as needed.
        Me.StaffTableAdapter.Fill(Me.DbTestDataSet.Staff)

    End Sub

   
    Private Sub DataGridView1_CellMouseDown(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
        If e.Button = MouseButtons.Right Then
            DataGridView1.CurrentCell = DataGridView1(e.ColumnIndex, e.RowIndex)
            Dim contextMenuDataGrid As New ContextMenuStrip
            contextMenuDataGrid.Items.Add("Insert Row")
            contextMenuDataGrid.Items.Add("Delete Row")
            contextMenuDataGrid.Show(DataGridView1, e.Location)
            ' MsgBox("Hello")
        End If
    End Sub

    Private Sub contextMenuDataGrid_Click(sender As Object, e As EventArgs) Handles contextMenuDataGrid.Click                  -<------------ERROR ContextMenuDataGrid.Click -
        MsgBox("QQ")
        If Not DataGridView1.Rows(index).IsNewRow Then 'CHECK IF NEW ROW
            DataGridView1.Rows.RemoveAt(index)
        End If


Viewing all articles
Browse latest Browse all 15083

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>