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
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)