单击ListBox VB.net中的项目时打开另一个表单

时间:2016-01-22 03:02:18

标签: vb.net pdf listbox

我是一名网络管理员,负责完成无纸化会议系统。所以现在我正在将VB.net视为我的平台。我的目标是使用文件名填充包含文件夹内项目的列表框,它主要是PDF文件。所以我实现了这个目标,当我点击列表框中的项目时,文件夹中的文件就会打开。我想要的是,如果我单击列表框中的项目,将显示另一个表单。此表单具有嵌入式pdf阅读器和用于注释的文本框。文本框将保存在另一个文件夹中的.txt文件中。这是我的代码

Public Class Form2

Dim MyFolderPath As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "C:\Users\ICTCAdmin\Desktop\Board Meeting\Academic")

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try

        For Each fullpath As String In IO.Directory.GetFiles(MyFolderPath)


            ListBox1.Items.Add(IO.Path.GetFileName(fullpath))
        Next
    Catch ex As Exception
        MsgBox(ErrorToString)
    End Try
End Sub

Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Form1.Show()
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    Dim fullpath As String = IO.Path.Combine(MyFolderPath, ListBox1.SelectedItem.ToString)
    Process.Start(fullpath)
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub
End Class

提前谢谢。

1 个答案:

答案 0 :(得分:0)

在您的第二个表单中,在子表单的代码窗口顶部创建一个这样的Friend属性

Public Class frmWhateverYourNewFormIsCalled
    Friend Property filepath As String

在您的调用表单中,将listbox1_SelectedIndexChanged属性编辑为此

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    Dim fullpath As String = IO.Path.Combine(MyFolderPath, ListBox1.SelectedItem.ToString)
    dim childForm as new frmWhateverYourNextFormIsCalled
            childForm.filepath = ""
    childForm.Show()
End Sub

在您的子表单中添加代码以在嵌入式pdf阅读器中打开filePath。可能在Shown事件处理程序中。不是Load事件处理程序。

要查看程序中的pdf,这可能有所帮助 Displaying a PDF in a control in Visual Basic 2010