简体   繁体   中英

Opening File Rich Text Box (VB.NET)

I'm working on a Document Writer and I'm including the feature of tabs, and I'm having trouble opening the files into the multiple tabs I'm using RichTextboxes (I'm not sure if that affects anything)

Here is the code:

 Public Sub openFile()
    Dim ofd As New OpenFileDialog
    ofd.Filter = fileFilter
    ofd.FileName = ""
    Select Case ofd.ShowDialog()
        Case DialogResult.OK
        loadFile(Path.GetFileName(ofd.FileName))
    End Select
End Sub

Public Sub loadFile(ByVal file As String)
   Try
    fileName = file
    setText(IO.File.ReadAllText(file))
   Catch ex As Exception
            MsgBox(ex.Message)
   End Try
End Sub

Public Sub setText(ByVal value As String)
    Dim t As RichTextBox = tabH.SelectedTab.Controls.OfType(Of RichTextBox)().First()
    t.Text = value
End Sub

The try is catching the problem and is saying that it can't find the file. But it's searching for the file in the directory of the .exe

Does anybody know how to fix this?

This happens because you are only passing the file name (instead of the full path) to your loadFile() method.

The Path.GetFileName() method returns only the file name and extension part of a path. For example, if you would call:

Path.GetFileName("C:\Users\John\Hello World.txt")

the method would return:

Hello World.txt

So remove that call from your code and you should be good to go:

Case DialogResult.OK
    loadFile(ofd.FileName)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM