在Visual Basic中創建右鍵彈出式菜單可以通過以下步驟實現:
在Form的屬性中設置Form的ContextMenu屬性為一個新的ContextMenu組件。
在Form的Load事件中添加代碼來創建菜單項并將其添加到ContextMenu組件中。例如:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim menuItem As New MenuItem("菜單項1")
AddHandler menuItem.Click, AddressOf MenuItem_Click
Me.ContextMenu1.MenuItems.Add(menuItem)
Dim menuItem2 As New MenuItem("菜單項2")
AddHandler menuItem2.Click, AddressOf MenuItem_Click
Me.ContextMenu1.MenuItems.Add(menuItem2)
End Sub
Private Sub MenuItem_Click(sender As Object, e As EventArgs)
Dim menuItem As MenuItem = DirectCast(sender, MenuItem)
MessageBox.Show("你點擊了" & menuItem.Text)
End Sub
Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
If e.Button = MouseButtons.Right Then
Me.ContextMenu1.Show(Me, e.Location)
End If
End Sub
以上就是在Visual Basic中創建右鍵彈出式菜單的簡單方法。您可以根據需要自定義菜單項和菜單項的點擊事件。