Home > Net >  In c # for listbox added right-click menu list items
In c # for listbox added right-click menu list items

Time:09-22

In c # to create good listbox, have shown good all processes, right click to select one of the process menu pop up close process, but I don't how fiddling is right the popup menu, can you give directions under the

CodePudding user response:

Context menu pops up in the show, the specified control and location, can be specified in the control system of position pop-up,
This problem is a lot of you, also very few ^ ^
Take the mouse to click the listbox item has several procedures, can use the API to send SENDMESSAGE LB_ITEMFROMPOINT=& amp; H1A9 news, it is important to note the mouse coordinates of coordinate system, also can be directly calculated, give you a little code, look at yourself:
Public Class Form1

Dim WithEvents prcListBox As ListBox
Dim popMenu As ContextMenuStrip
Dim WithEvents popMenuItem_Kill As ToolStripItem
Dim prcList As New List (Of the Process)
Dim pickIdx As Integer=1
Private Sub Form1_Load (sender As Object, As EventArgs e) Handles MyBase. Load
'list
PrcListBox=New ListBox
PrcListBox. The Dock=DockStyle. The Fill
Me. Controls. The Add (prcListBox)
'menu
PopMenu=New ContextMenuStrip
PopMenuItem_Kill=popMenu. Items. The Add (" end process ")
'
save processPrcList. AddRange (Process. GetProcesses)
'according to
For I As Integer=0 To prcList. Count - 1
PrcListBox. Items. The Add (prcList (I). The ProcessName & amp; "" & amp; PrcList (I). MainWindowTitle)
Next
End Sub
Private Sub prcListBox_MouseUp (sender As Object, e As MouseEventArgs) Handles prcListBox. MouseUp
If e.B utton=MouseButtons. Right Then
For I As Integer=0 To prcListBox. Items. The Count - 1
'in the item of the rect
If prcListBox. GetItemRectangle (I). The Contains (e.L ocation) Then
'record, click on the project, modify the listbox display, display menu
PickIdx=I
PrcListBox. SelectedIndex=I
PopMenu. Show (prcListBox, e.L ocation)
Return
End the If
Next
End the If
End Sub
Private Sub popMenuItem_Kill_Click (sender As Object, As EventArgs e) Handles popMenuItem_Kill. Click
If pickIdx & lt;> 1 Then
Try
'kill process and removed from the list
PrcList (pickIdx). The Kill ()
PrcList. RemoveAt (pickIdx)
Catch the ex As Exception
'to play... No change to the dry, general can elevate privileges may kill two more
MsgBox (ex. ToString)
The Finally
'no matter how to restore to sign
PickIdx=1
End the Try
End the If
End Sub
The End of the Class

CodePudding user response:

 Public Class Form1 

Dim WithEvents prcListBox As ListBox
Dim popMenu As ContextMenuStrip
Dim WithEvents popMenuItem_Kill As ToolStripItem
Dim prcList As New List (Of the Process)
Dim pickIdx As Integer=1
Private Sub Form1_Load (sender As Object, As EventArgs e) Handles MyBase. Load
'list
PrcListBox=New ListBox
PrcListBox. The Dock=DockStyle. The Fill
Me. Controls. The Add (prcListBox)
'menu
PopMenu=New ContextMenuStrip
PopMenuItem_Kill=popMenu. Items. The Add (" end process ")
'
save processPrcList. AddRange (Process. GetProcesses)
'according to
For I As Integer=0 To prcList. Count - 1
PrcListBox. Items. The Add (prcList (I). The ProcessName & amp; "" & amp; PrcList (I). MainWindowTitle)
Next
End Sub
Private Sub prcListBox_MouseUp (sender As Object, e As MouseEventArgs) Handles prcListBox. MouseUp
If e.B utton=MouseButtons. Right Then
For I As Integer=0 To prcListBox. Items. The Count - 1
'in the item of the rect
If prcListBox. GetItemRectangle (I). The Contains (e.L ocation) Then
'record, click on the project, modify the listbox display, display menu
PickIdx=I
PrcListBox. SelectedIndex=I
PopMenu. Show (prcListBox, e.L ocation)
Return
End the If
Next
End the If
End Sub
Private Sub popMenuItem_Kill_Click (sender As Object, As EventArgs e) Handles popMenuItem_Kill. Click
If pickIdx & lt;> 1 Then
Try
'kill process and removed from the list
PrcList (pickIdx). The Kill ()
PrcList. RemoveAt (pickIdx)
Catch the ex As Exception
'to play... No change to the dry, general can elevate privileges may kill two more
MsgBox (ex. ToString)
The Finally
'no matter how to restore to sign
PickIdx=1
End the Try
End the If
End Sub
The End of the Class

CodePudding user response:

Old and useless,,
'kill process and removed from the list
PrcList (pickIdx). The Kill ()
PrcList. RemoveAt (pickIdx)
PrcListBox. Items. RemoveAt (pickIdx)

Write to write go to forget who you are, where you, declare variables is doing,

CodePudding user response:

https://blog.csdn.net/qq_33712555/article/details/80051754
  •  Tags:  
  • C#
  • Related