SuperNovaa41 SuperNovaa41 - 5 months ago 32 Question

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
End Select
End Sub

Public Sub loadFile(ByVal file As String)
fileName = file
Catch ex As Exception
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