.net - 如何使上下文菜单出现在datagridview单元的右键单击上

标签 .net winforms powershell datagridview contextmenu

我有一个带有数据网格的表格。我想做的是,当我右键单击一个单元格时,在鼠标旁边显示一个下拉菜单。我需要能够从菜单中选择一个选项并运行一些东西。

我可以右键单击事件进行注册,但不会显示上下文菜单。
下面的代码中的菜单应显示带有复制单元格的选项。

[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(1040,518)
$form.KeyPreview = $true
$form.StartPosition = 'centerscreen'
$form.BackColor = 'MidnightBlue'
$form.Add_KeyDown({if($_.KeyCode -eq "Escape"){$form.Close()}})
$form.Text = "VIOC Toolkit 5.4" 
$form.Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell_ise.exe")
$form.MinimumSize = New-Object System.Drawing.Size(1040,525)

$DataGrid1 = New-Object System.Windows.Forms.DataGridView
$DataGrid1.Location = New-Object System.Drawing.Size(298,29)
$DataGrid1.Dock = "Fill"
$DataGrid1.BorderStyle = 'FixedSingle'    
$DataGrid1.DefaultCellStyle.Font = New-Object System.Drawing.Font($dgfont,$dgfontSize)
$DataGrid1.AlternatingRowsDefaultCellStyle.BackColor = 'LightGray'
$DataGrid1.AllowUserToAddRows = $false
$DataGrid1.RowHeadersVisible = $false
$DataGrid1.BackgroundColor = "White"
$DataGrid1.Name="DataGrid1"
$DataGrid1.Text="DataGrid1"
$DataGrid1.ColumnCount = 3
$DataGrid1.Columns[0].Name = 'one'
$DataGrid1.Columns[1].Name = 'two'
$DataGrid1.Columns[2].Name = 'three'
$DataGrid1.Rows.add(@('a', 'b', 'c'))
$DataGrid1.Rows.add(@('d', 'e', 'f'))
#*************************************************************#    
$DataGrid1.add_CellMouseClick({
    if($_.button -eq 'Right'){
        ContextMenu($DataGrid1)
    }
})

function ContextMenu($Grid){
    #create the context menu
    $menu = New-Object System.Windows.Forms.ContextMenu
    $CopyMenuItem = New-Object System.Windows.Forms.MenuItem
    $copymenuItem.text = 'copy'
    $copymenuItem.add_Click({write-host 'clicked copy'})
    $menu.MenuItems.AddRange(@($CopyMenuItem))

}

#***************************************************************#
$form.Controls.Add($DataGrid1)
$form.ShowDialog() | out-null

最佳答案

试试这个代码:

[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(1040,518)
$form.KeyPreview = $true
$form.StartPosition = 'centerscreen'
$form.BackColor = 'MidnightBlue'
$form.Add_KeyDown({if($_.KeyCode -eq "Escape"){$form.Close()}})
$form.Text = "VIOC Toolkit 5.4" 
$form.Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell_ise.exe")
$form.MinimumSize = New-Object System.Drawing.Size(1040,525)

[System.Windows.Forms.DataGridView] $DataGrid1 = New-Object System.Windows.Forms.DataGridView
$DataGrid1.Location = New-Object System.Drawing.Size(298,29)
$DataGrid1.Dock = "Fill"
$DataGrid1.BorderStyle = 'FixedSingle'    
#$DataGrid1.DefaultCellStyle.Font = New-Object System.Drawing.Font $dgfont,$dgfontSize
$DataGrid1.AlternatingRowsDefaultCellStyle.BackColor = 'LightGray'
$DataGrid1.AllowUserToAddRows = $false
$DataGrid1.RowHeadersVisible = $false
$DataGrid1.BackgroundColor = "White"
$DataGrid1.Name="DataGrid1"
$DataGrid1.Text="DataGrid1"
$DataGrid1.ColumnCount = 3
$DataGrid1.Columns[0].Name = 'one'
$DataGrid1.Columns[1].Name = 'two'
$DataGrid1.Columns[2].Name = 'three'
$DataGrid1.Rows.add(@('a', 'b', 'c'))
$DataGrid1.Rows.add(@('d', 'e', 'f'))

#Creation of content click event
$ClickElementMenu=
{
    [System.Windows.Forms.ToolStripItem]$sender = $args[0]
    [System.EventArgs]$e= $args[1]

    $Contentcell=$DataGrid1.Rows[$DataGrid1.CurrentCell.RowIndex].Cells[$DataGrid1.CurrentCell.ColumnIndex].Value
    $ElementMenuClicked=$sender.Text
    $RowIndex=$DataGrid1.CurrentCell.RowIndex
    $ColIndex=$DataGrid1.CurrentCell.ColumnIndex


    $result="Click on element menu : '{0}' , in rowindex : {1} , column : {2}, content cell : {3}" -f $ElementMenuClicked,  $RowIndex, $ColIndex, $Contentcell;
    Write-Host $result
}

#creation menu
$contextMenuStrip1=New-Object System.Windows.Forms.ContextMenuStrip

#creation element1 of menu
[System.Windows.Forms.ToolStripItem]$toolStripItem1 = New-Object System.Windows.Forms.ToolStripMenuItem
$toolStripItem1.Text = "Element 1";
$toolStripItem1.add_Click($ClickElementMenu)
$contextMenuStrip1.Items.Add($toolStripItem1);

#creation element2 of menu
[System.Windows.Forms.ToolStripItem]$toolStripItem2 = New-Object System.Windows.Forms.ToolStripMenuItem
$toolStripItem2.Text = "Element 2";
$toolStripItem2.add_Click($ClickElementMenu)
$contextMenuStrip1.Items.Add($toolStripItem2);

#creation event of mouse down on datagrid and show menu when click
$DataGrid1.add_MouseDown({
    $sender = $args[0]
    [System.Windows.Forms.MouseEventArgs]$e= $args[1]

    if ($e.Button -eq  [System.Windows.Forms.MouseButtons]::Right)
    {
        [System.Windows.Forms.DataGridView+HitTestInfo] $hit = $DataGrid1.HitTest($e.X, $e.Y);
        if ($hit.Type -eq [System.Windows.Forms.DataGridViewHitTestType]::Cell)
        {
            $DataGrid1.CurrentCell = $DataGrid1[$hit.ColumnIndex, $hit.RowIndex];
            $contextMenuStrip1.Show($DataGrid1, $e.X, $e.Y);
        }

    }
})




#***************************************************************#
$form.Controls.Add($DataGrid1)
$form.ShowDialog() | out-null

关于.net - 如何使上下文菜单出现在datagridview单元的右键单击上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45574948/

相关文章:

c# - 集合中接口(interface)与对象引用的优势

powershell - 从网络位置复制列表中定义的文件

powershell - 在 Powershell Substring(0, IndexOf) 中没有匹配项时返回整个字符串

.net - 如何在VS 2015中打开性能监视器?

c# - EF6如何映射这种零或一对一关系

c# - WinForms App + MS SQL Server DB 迁移到 Azure 平台

powershell - New-SelfSignedCertificate 不会在 Windows 7 上运行

.net - 如何从 Windows Server Core 上的 WPF 应用程序使用“浏览文件夹”对话框?

c# - Application.Idle事件意义

c# - 如何在 Windows.Forms.PictureBox 中正确显示 Kinect 视频流?