我有一个包含完整路径的ListBox和一个选择打开文件或在文件夹中显示文件的ToolStripMenu。
问题是:在右键单击以选择需要在文件夹中显示的文件之前,我需要在ListBoxItem上左键单击。
如果我不这样做,我会得到NullReferenceException,因为没有选择任何项。
如何才能选择右键单击的项目?
下面是我的代码:
Private Sub ShowInFolderToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ShowInFolderToolStripMenuItem.Click
Try
If DirectCast(DirectCast(sender, ToolStripMenuItem).GetCurrentParent, _
ContextMenuStrip).SourceControl.GetType Is GetType(ListBox) Then
Shell("explorer /select, " & DirectCast(DirectCast(DirectCast(sender, _
ToolStripMenuItem).GetCurrentParent, ContextMenuStrip).SourceControl, _
ListBox).SelectedItem.ToString, AppWinStyle.NormalFocus)
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub还有一个问题..。为什么下面的代码不能工作,但不能给出错误并显示文档文件夹?
Process.Start("explorer.exe", "/select" & DirectCast(DirectCast(DirectCast(sender, _
ToolStripMenuItem).GetCurrentParent, ContextMenuStrip).SourceControl, _
ListBox).SelectedItem.ToString)发布于 2016-10-17 00:51:30
已解决添加MouseDown事件的问题:
Private Sub ListBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseDown
Try
DirectCast(sender, ListBox).SelectedIndex = DirectCast(sender, ListBox).IndexFromPoint(e.X, e.Y)
Catch ex As Exception
End Try
End Subhttps://stackoverflow.com/questions/40071735
复制相似问题