从visual studio中的列表框中打开文件 [英] Open a file from a listbox in visual studio
问题描述
我设法使用此代码在列表框中获取视频列表,但我希望在列表框中点击视频时打开该视频,我该怎么做?
私人小组OpenToolStripMenuItem_Click(发送者为对象,
              &NBSP ; e As EventArgs)处理OpenToolStripMenuItem.Click
     试试¥b $ b $
          openfile =新的OpenFileDialog
          ' openfile.Filter ="仅视频文件(* .avi,* .mp4,* .mkv)| * .avi,* .mp4,* .mkv"
          openfile.Title ="选择文件"
          openfile.CheckFileExists = True
          openfile.Multiselect = True
          openfile.RestoreDirectory = False
         如果openfile.ShowDialog = System.Windows.Forms.DialogResult.OK则为
              openfile.SafeFileName.Count
             对于i As Integer = 0 to openfile.SafeFileNames.Count() - 1
                  lbTV.Items.Add(openfile.SafeFileNames(i))
                  lbPath.Items.Add(openfile.FileNames(i))
             下一个
         结束如果
      C ex ex As Exception
          MessageBox.Show(ex.Message," Message",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
     结束尝试
 结束Sub
I managed to get a list of video in a listbox with this code but I want the video to open when I click on it in the listbox, how can I do that ?
Private Sub OpenToolStripMenuItem_Click(sender As Object,
e As EventArgs) Handles OpenToolStripMenuItem.Click
Try
openfile = New OpenFileDialog
' openfile.Filter = "Video files only (*.avi, *.mp4, *.mkv)|*.avi, *.mp4, *.mkv"
openfile.Title = "Select files"
openfile.CheckFileExists = True
openfile.Multiselect = True
openfile.RestoreDirectory = False
If openfile.ShowDialog = System.Windows.Forms.DialogResult.OK Then
openfile.SafeFileName.Count
For i As Integer = 0 To openfile.SafeFileNames.Count() - 1
lbTV.Items.Add(openfile.SafeFileNames(i))
lbPath.Items.Add(openfile.FileNames(i))
Next
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub
推荐答案
首先
ListBox类有一个属性
ListBox.SelectionMode 应设置为默认值"SelectionMode.One" (如果你改变了这一点),这样一次只能选择一个项目。
Well first the ListBox Class has a property ListBox.SelectionMode which should be set to the default of "SelectionMode.One" (in case you altered that) so that only one item can be selected at a time.
同样,ListBox包含Objects而不是字符串,即使String已添加到ListBox,它也是一个对象,应该转换为正确的类型以使用它。
Also a ListBox contains Objects and not Strings even if a String was added to the ListBox it is then an Object and should be converted to the proper type to use it.
与所有控件一样,ListBox也有事件。如果在设计器窗口中单击ListBox,它应该自动将代码添加到窗体代码窗口,以获得
ListBox.SelectedIndexChanged事件并在该子就可以 获得的
ListBox.SelectedItem属性(即ListBox1.SelectedItem.ToString)将是文件的路径/文件名。
The ListBox, like all controls, has events. If you click on the ListBox in the designer window it should auto add code to the Forms code window for a ListBox.SelectedIndexChanged Event and in that sub you can get the ListBox.SelectedItem Property (i.e. ListBox1.SelectedItem.ToString) which converted to String type will be the path/filename of the file.
如果您的Windows版本使用Windows Media Player作为默认媒体播放器文件类型 它可以播放所有这些文件类型(安装了用于播放所有这些文件类型的编解码器)然后你可以使用
流程类,特别是 Process.Start方法提供文件名然后才开始(不要使用没有窗口) 启动那些
启动Windows Media Player的文件(或任何与这些文件扩展名相关联的默认应用程序) 播放这些文件。
If your Windows version uses Windows Media Player as the default media player for those file types and it can play all those file types (has the codecs installed for playing all those file types) then you could use the Process Class and specifically Process.Start Method providing it a filename then just start (don't use no window) to launch those files which would launch Windows Media Player (or whatever the default app is associated to those file extensions) to play those files.
但是,您还可以使用ToolBox管理器将工具添加到ToolBox,从而将Windows Media Player活动x控件添加到Visual Studio工具箱中。我相信它可能是一个组件,但不记得了。然后,您可以将一个Windows Media Player
活动x控件从ToolBox放到您的表单上,并为其提供播放路径。
However you can also add a Windows Media Player active x control to your Visual Studios toolbox by using the ToolBox manager to add tools to the ToolBox. I believe it may be a com component but don't remember. Then you could place a Windows Media Player active x control from the ToolBox onto your Form and provide it with the path to play.
如果您没有所有的编解码器您的系统需要默认媒体播放器播放这些文件,然后您需要找到在您的系统上安装所需的编解码器。
If you don't have all the CODECs necessary on your system for the default media player to play those files then you will need to find CODECs necessary to install on your system.
还有一个 MediaPlayer类,可以播放我喜欢的视频从未使用过。
There is also a MediaPlayer Class that can play videos which I've never used.
如果您对VB.Net代码有疑问,您可能应该在VB.Net论坛中询问
视觉基本。
Also if you have questions with VB.Net code you should probably ask in the VB.Net Forum Visual Basic.
这篇关于从visual studio中的列表框中打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!