File is not shown in OpenFileDialog - vb.net

I'm trying to do something like a text editor in VB 2012, so I have a MenuStrip with an "OpenFile" option. When it's clicked it fires an OpenFileDialog control and shows me just the files with the extension I want. The problem is that if I close the file with another option in the menu strip, when I want to open the same file I opened the first time using the same OpenFile option the OpenFileDialog is not showing me the file.
Do you know why is that?
Here is how I open the file:
Private Sub OpenFileToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenFileToolStripMenuItem.Click
'Open File
If OpenFile.ShowDialog() = Windows.Forms.DialogResult.OK Then
If Not (OpenFile.FileName = "") And OpenFile.CheckFileExists Then
NewFile = New StreamReader(OpenFile.FileName)
If NewFile IsNot Nothing Then
Me.TextBox_Main.Text = NewFile.ReadToEnd
Me.TabPage1.Text = OpenFile.FileName.Substring(OpenFile.FileName.LastIndexOf("\") + 1)
End If
End If
End If
End Sub
And here is how I close the file:
Private Sub CloseFileToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CloseFileToolStripMenuItem.Click
'Close Files
If NewFile IsNot Nothing Then
NewFile.Close()
NewFile = Nothing
End If
Me.TextBox_Main.Text = ""
Me.TabPage1.Text = "New Tab"
End Sub

Related

Rename file extension of selected file from dialog box vb.net

I am trying to rename the file extension of a file I selected from the dialog box. Right now, it is not changing format to ".txt". Where am I wrong?
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
'Sym File Open
Dim sym As OpenFileDialog = New OpenFileDialog()
''Defines the critieria of a Trace file type.
sym.Title = "Open File Dialog"
sym.InitialDirectory = "C:\My Computer\Documents"
sym.Filter = "Symbol files (*.sym*)|*.sym*"
sym.FilterIndex = 1
sym.RestoreDirectory = True
'Assigning path to textbox
If sym.ShowDialog() = DialogResult.OK Then
SymTextSelection.Text = sym.FileName
'Converting sym file into a text file
sym.FileName = sym.FileName.Replace(".sym", ".txt")
End If
End Sub
This worked for me
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
'Sym File Open
Dim sym As OpenFileDialog = New OpenFileDialog()
''Defines the critieria of a Trace file type.
sym.Title = "Open File Dialog"
sym.InitialDirectory = "C:\My Computer\Documents"
sym.Filter = "Symbol files (*.sym*)|*.sym*"
sym.FilterIndex = 1
sym.RestoreDirectory = True
'Assigning path to textbox
If sym.ShowDialog() = DialogResult.OK Then
SymTextSelection.Text = sym.FileName
'Converting sym file into a text file
File.Move(sym.FileName, Path.ChangeExtension(sym.FileName, ".xlsm"))
End If
End Sub

Issue with OpenFileDialog and InitialDirectory property

I have two Buttons and two OpenFileDialogs and I am facing an issue with InitialDirectory property. When I choose a file for my first OpenFileDialog and then click to choose a file for my second OpenFileDialog, I get the same InitialDirectory and not the ones I have set!!!
Here is an example of my code...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FileDialog As New OpenFileDialog
Dim Path As String = Nothing
If Label1.Text IsNot Nothing And My.Computer.FileSystem.FileExists(Label1.Text) Then
Path = Label1.Text
Else
Path = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
End If
FileDialog.Title = "Open File Dialog"
FileDialog.InitialDirectory = Path
FileDialog.Filter = "Executable (*.exe)|*.exe"
FileDialog.RestoreDirectory = True
If FileDialog.ShowDialog() = DialogResult.OK Then
Label1.Text = FileDialog.FileName
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim FileDialog As New OpenFileDialog
Dim Path As String = Nothing
If Label2.Text IsNot Nothing And My.Computer.FileSystem.FileExists(Label2.Text) Then
Path = Label2.Text
Else
Path = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
End If
FileDialog.Title = "Open File Dialog"
FileDialog.InitialDirectory = Path
FileDialog.Filter = "Executable (*.exe)|*.exe"
FileDialog.RestoreDirectory = True
If FileDialog.ShowDialog() = DialogResult.OK Then
Label2.Text = FileDialog.FileName
End If
End Sub
Well I found what was wrong...
I just had to set the Path variable like this Path = IO.Path.GetDirectoryName(Label1.Text) and not like this Path = Label1.Text. Because the first one gets file's directory path (which is required) and the second one gets file's path. And I was using the second one...

Unknown error wont open from computer. Basic

Sub ShowFileContents(ByVal strFileName As String)
Dim fs As New FileStream(strFileName, FileMode.Open, FileAccess.Read)
Dim TextFile As New StreamReader(fs)
Me.txtFileContents.Text = Nothing
Dim strLineOfText As String
Do While TextFile.Peek > -1
strLineOfText = TextFile.ReadLine()
Me.txtFileContents.Text = Me.txtFileContents.Text & strLineOfText & vbCrLf
Loop
TextFile.Close()
fs.Close()
End Sub
Why cant I open a file when I run program all it says is "Save" or "Cancel"
Why cant I open a file when I run program all it says is "Save" or "Cancel" What Im trying to do is open a file from my documents and display it in the textbox below. I have the save file working but not the open file
It looks like you could benefit from using the OpenFileDialog control rather than a textbox to get the file path you want to open.
Here's an example:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim ofp As New OpenFileDialog
If ofp.ShowDialog = Windows.Forms.DialogResult.OK Then
ShowFileContents(ofp.FileName)
End If
End Sub
Sub ShowFileContents(ByVal strFileName As String)
Dim TextFile As New StreamReader(strFileName)
txtFileContents.Text = Nothing
Do While Not TextFile.EndOfStream
txtFileContents.AppendText(TextFile.ReadLine() & vbCrLf)
Loop
TextFile.Close()
End Sub

Use SUB variable in other sub

I have a Open File button that opens an INI file.
Now I would like to use the path of the file so I can click a Save Button and the file is saved.
This is the code for the Open Button:
Private Sub OpenINIButton_Click(sender As Object, e As EventArgs) Handles OpenINIButton.Click
Dim OpenDLG As New OpenFileDialog
OpenDLG.Filter = "Configuration File (*.ini)|*.ini"
OpenDLG.Title = "Open INI File"
OpenDLG.InitialDirectory = "C:\"
OpenDLG.RestoreDirectory = True
DialogResult = OpenDLG.ShowDialog
If DialogResult = Windows.Forms.DialogResult.OK Then
Dim OpenFile = OpenDLG.FileName.ToString()
wValue.Text = ReadIni(OpenFile, Isolation, Value, "")
ElseIf DialogResult = Windows.Forms.DialogResult.Cancel Then
End If
End Sub
I would like the OpenFile variable in the Save Button, the code I want to use is:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles saveINI.Click
System.IO.File.WriteAllText(OpenFile, "")
writeIni(OpenFile, BuildOptions, Isolation, w.Value.Text)
End Sub
But the OpenFile variable is not available.
Is there any possibility to set the OpenFile variable Global?
I cannot move it outside the SUB because the Open File button doesn't work anymore.
Thanks!
The solution is very simple. Just declare OpenFile outside of sub
Private OpenFile as String
Private Sub OpenINIButton_Click( ...
Remove Dim statement Dim OpenFile = replace with just OpenFile =
Optionally test whether the variable was set at the beginning of Button2_Click
If OpenFile is Nothing then Exit Sub

How to op-en file and view in a rich text box using vb.net?

Hi I am trying to open and view a files text in a rich text box. Here is what I have please let me know what I am doing wrong?
Private Sub loadButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loadButton.Click
' Displays an OpenFileDialog so the user can select a Cursor.
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "Cursor Files|*.txt"
openFileDialog1.Title = "Select a Cursor File"
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
' Assign the cursor in the Stream to the Form's Cursor property.
Me.mainRTBox = New Text(openFileDialog1.OpenFile())
End If
End Sub
The problem you were having was that you weren't reading the file at all, and you weren't assigning the content of the file to the RichTextBox correctly.
Specifically, this code you have:
Me.mainRTBox = New Text(openFileDialog1.OpenFile())
.. should be:
Me.mainRTBox.Text = FileIO.FileSystem.ReadAllText(openFileDialog1.FileName)
This code will work:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Displays an OpenFileDialog so the user can select a Cursor.
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "Cursor Files|*.cur"
openFileDialog1.Title = "Select a Cursor File"
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
' Assign the cursor in the Stream to the Form's Cursor property.
Dim extension = openFileDialog1.FileName.Substring(openFileDialog1.FileName.LastIndexOf("."))
If extension Is "cur" Then
Me.mainRTBox.Text = FileIO.FileSystem.ReadAllText(openFileDialog1.FileName)
End If
End If
End Sub
End Class
Edit: I updated the code so that it checks if the user did actually open a Cur (cursor) file.
RichTextBoxes have a built in function for viewing RTF files and TXT files.
Code for a RTF file:
RichTextBox1.LoadFile("YOUR DIRECTORY", RichTextBoxStreamType.RichText)
Code for a TXT file:
RichTextBox1.LoadFile("YOUR DIRECTORY", RichTextBoxStreamType.PlainText)
Hope it helps
-nfell2009