How to op-en file and view in a rich text box using vb.net? - 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

Related

File is not shown in OpenFileDialog

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

Open only files with *.bin

It is possible to add open only files with *.bin extension in openfile dialog ?
Here is my code. Maybe somebody can fix it.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim OFD As New OpenFileDialog
Dim fullFile() As Byte
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
fullFile = File.ReadAllBytes(OFD.FileName)
TextBox1.AppendText(fullFile(&H1E).ToString("X2") & " ")
TextBox1.AppendText(fullFile(&H1F).ToString("X2"))
End If
If file have another extension msg,box : Wrong file
You need to use the Filter property: MSDN
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim OFD As New OpenFileDialog
OFD.Filter = "BIN Files (*.bin)|*.bin"
Dim fullFile() As Byte
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
fullFile = File.ReadAllBytes(OFD.FileName)
TextBox1.AppendText(fullFile(&H1E).ToString("X2") & " ")
TextBox1.AppendText(fullFile(&H1F).ToString("X2"))
End If
End Sub
The pipe | character is used in the filter string to separate it into chunks: the first is what the user sees in the dropdown, and the second is the actual filter that is run on the files. You can have multiple filters available, too. Here's another example of a filter string: Text files (*.txt)|*.txt|All files (*.*)|*.*
You need to use javax.swing.JFileChooser.
Use this:
FileNameExtensionFilter filter = new FileNameExtensionFilter("Binary Files", "bin");
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(filter);

How would I use an Open File Dialog to select an Image and then put that Image in a Picturebox on another Form?

How would I use an Open File Dialog to select an Image and then put that Image in a Picturebox Control on another Form?
Private Sub btnLogo_Click(sender As Object, e As EventArgs) Handles btnLogo.Click
OpenFileDialog1.Title = "Please Select a File"
OpenFileDialog1.InitialDirectory = "C:"
OpenFileDialog1.ShowDialog()
photo = OpenFileDialog1.FileName.ToString
I'm guessing this is wrong but I'm lost to what to do here.
Then once I have selected an Image; what would be the appropriate Code to put that Image into a Picturebox Control on the other Form ?
If I understood you correctly then it is pretty easy:
Sub OpenAnImageInPicturebox(ByRef pb As PictureBox)
Dim ofd As New OpenFileDialog
ofd.Filter = "Bitmap|*.bmp|JPEG|*.jpg" 'If you like file type filters you can add them here
'any other modifications to the dialog
If ofd.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
Try
Dim bmp As New Bitmap(ofd.FileName)
If Not IsNothing(pb.Image) Then pb.Image.Dispose() 'Optional if you want to destroy the previously loaded image
pb.Image = bmp
Catch
MsgBox("Not a valid image file.")
End Try
End Sub
try this:
photo = image.Fromfile( OpenFileDialog1.FileName)
Hope it helps
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.Title = "Please select a file"
OpenFileDialog1.InitialDirectory = "c:"
OpenFileDialog1.ShowDialog()
PictureBox1.ImageLocation = OpenFileDialog1.FileName.ToString
PictureBox1.Visible = True
End Sub
I came across this question when I was looking for Filter, as an update to previous answers.
You want to use OpenFileDialog to select an Image to put in a PictureBox Control in another Form. I suggest :-
Use Module : to use the Image on any Form
Use Default Image : to display in PictureBox Control whenever Error occurred. Use Project Resources to add Existing Resource (I.E. PNG Image file : noPhotoUsr).
Code for Module1
Module Module1
Public Function _GetImgOFD(Frm As Form, PicBx As PictureBox) As Bitmap
Dim _ErrBitmap As Bitmap = My.Resources.noPhotoUsr
Dim ChosenBitmap As Bitmap
Using OFD As OpenFileDialog = New OpenFileDialog
With OFD
.Filter = ("Image File (*.ico;*.jpg;*.bmp;*.gif;*.png)|*.jpg;*.bmp;*.gif;*.png;*.ico")
.RestoreDirectory = True
.Multiselect = False
.CheckFileExists = True
If .ShowDialog(Frm) = DialogResult.OK Then
ChosenBitmap = Bitmap.FromFile(.FileName)
Else
ChosenBitmap = _ErrBitmap
End If
End With
End Using
Return ChosenBitmap
End Function
End Module
Code to use in any Form, PictureBox Click Event
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
PictureBox1.Image = Module1._GetImgOFD(Me, PictureBox1)
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

adding a file into a listbox in vb.net / vb2008

Hello i store proxys in a notpad.txt file and im trying to grab all proxys in the notpad and put them into listbox1
i am using
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Using Ofd As New OpenFileDialog
Ofd.Filter = "All files (*.*)|*.*"
If Ofd.ShowDialog = 1 Then ListBox1.Items.AddRange(IO.File.ReadAllLines(Ofd.FileName))
End Using
End Sub
I click it the button it lets me pick a file but does not import the stuff in the file to listbox1
Please help
I tested your code and it works for me, so I assume the problem is with the format of your file. How was the file created? Could you provide a link to it so I can take a look?
One thing to note, you should use the DialogResult Enumeration instead of the magic number 1 for the OK result to improve the readability and maintenance of your code.
If Ofd.ShowDialog = DialogResult.OK Then ListBox1.Items.AddRange(IO.File.ReadAllLines(Ofd.FileName))
try this example add it if you click on the OK button
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Using Ofd As New OpenFileDialog
Ofd.Filter = "All files (*.*)|*.*"
If Ofd.ShowDialog = 1 Then
'Pass the file path and file name to the StreamReader constructor
Dim sr As New StreamReader(Ofd.FileName)
Dim line As String = String.Empty
Try
'Read the first line of text
line = sr.ReadLine()
'Continue to read until you reach end of file
While line IsNot Nothing
Me.listBox1.Items.Add(line)
'Read the next line
line = sr.ReadLine()
End While
'close the file
sr.Close()
Catch e As Exception
MessageBox.Show(e.Message.ToString())
Finally
'close the file
sr.Close()
End Try
End If
End Using
End Sub
Regards