How can I get my text file data to display in listbox? - vb.net

I have an issue that I would like some help with.
I am trying to open a file dialog, select text file, and then display text in listbox.
I have the following code. It opens dialog, but won't display text in listbox.
Any suggestions?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImportKeywordList.Click
Dim oReader As StreamReader
OpenFileDialog1.CheckFileExists = True
OpenFileDialog1.CheckPathExists = True
OpenFileDialog1.DefaultExt = "txt"
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
OpenFileDialog1.Multiselect = False
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
oReader = New StreamReader(OpenFileDialog1.FileName, True)
ListBox1.Text = oReader.ReadToEnd()
End If
End Sub

Listbox display text through the Items collection not through the Text property. In a ListBox the Text property represent the text of the currently selected item
An example could be written in this way
....
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Using oReader = New StreamReader(OpenFileDialog1.FileName, True)
While oReader.Peek <> -1
ListBox1.Items.Add(oReader.ReadLine())
End While
End Using
End If
....

Related

RTF code saved when I save a txt file

I'm working on a VB.NET project.
I have a richtextbox and a button to save what I write.
But when I open the file I see this:
I want to show just the text "ilyasscj isjdivs", not all the RTF code.
This is my code:
Private Sub EnsregistrerSousToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
EnsregistrerSousToolStripMenuItem.Click
SaveFileDialog1.InitialDirectory = "c:\"
SaveFileDialog1.Filter = "Texte|*.txt|RTF|*.rtf|Tous|*.*"
SaveFileDialog1.Title = "enregistrer un fichier"
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
fich = SaveFileDialog1.FileName
RichTextBox1.SaveFile(fich)
End If
End Sub
Private Sub EnregistrerToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
EnregistrerToolStripMenuItem.Click
If fich = "" Then
SaveFileDialog1.InitialDirectory = "c:\"
SaveFileDialog1.Filter = "Texte|*.txt|RTF|*.rtf|Tous|*.*"
SaveFileDialog1.Title = "enregistrer un fichier"
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
fich = SaveFileDialog1.FileName
Try
RichTextBox1.SaveFile(fich)
Catch ex As Exception
' MsgBox(ex.Message)
End Try
End If
Else
Try
RichTextBox1.SaveFile(fich)
Catch ex2 As Exception
MsgBox(ex2.Message)
End Try
End If
End Sub
I will appreciate any help.
The RichTextBox.SaveFile(String) pre-defined behaviour is described in the documentation:
Saves the contents of the RichTextBox to a rich text format (RTF)
file.
If you define more format options in the SaveFileDialog, you also need to verify what format the User chose and instruct the SaveFile method to use that format when streaming the output to disk.
As an example, use a Select Case switch to select the RichTextBoxStreamType corresponding to the User choice (see the documentation for the meaning of other possible fomat options):
Dim sfd As SaveFileDialog = New SaveFileDialog()
sfd.Filter = "Texte (*.txt)|*.txt|RTF (*.rtf)|*.rtf|Tous (*.*)|*.*"
sfd.Title = "Enregistrer un fichier"
sfd.DefaultExt = "txt"
If sfd.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim Filter As Integer = sfd.FilterIndex
Dim FileFormat As RichTextBoxStreamType
Select Case Filter
Case 1
FileFormat = RichTextBoxStreamType.PlainText
Case 2
FileFormat = RichTextBoxStreamType.RichText
Case Else
FileFormat = RichTextBoxStreamType.UnicodePlainText
End Select
Dim FileName As String = sfd.FileName
RichTextBox1.SaveFile(FileName, FileFormat)
End If

Easy and fast load datagrid from textfile with 100k rows using vb.net

I have a created a program with VB.net, where text file is used to load datagrid, but its very slow, there is a lot of code, when there are many line imports from text file.
Is there any simpler way to code this, so datagrid loading isn't so slow?
Private Sub Load_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Load.Click
OpenFileDialog1.Filter = "Text Files|*.txt|All Files|*.*"
OpenFileDialog1.Title = "Select Text File for Path"
OpenFileDialog1.FileName = "Select File"
If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.Ok Then
txttblnm.Text = OpenFileDialog1.FileName
txtfilenm.Text = System.IO.Path.GetFileName(txttblnm.Text)
txtfilenm.Text = txtfilenm.Text.Substring(0, txtfilenm.Text.Length - 4)
Label7.Text = System.IO.Path.GetDirectoryName(txttblnm.Text)
End If
If Len(txtfilenm.Text) = 8 And CheckBox1.CheckState = CheckState.Checked Then
Dim textfiles() = Directory.GetFiles(Label7.Text, "*.txt")
For Each file As String In textfiles
Call dgv1_load()
Call Access_export()
Call clear_item()
Next
ElseIf Len(txtfilenm.Text) >= 9 Then
MsgBox("Please Load DATA", MsgBoxStyle.Information, "Load Data Error")
Exit Sub
End If
End Sub
You're interacting far too much with the user interface. Every interaction with the gridview + textboxes is expensive.
What you should do is start by creating a DataTable and fill this with the row data.
Then after it's all loaded set the datagridview.DataSource to this table and everything will be drawn only once.
Private Function ParseFile(filename As String) As DataTable
Dim result As New DataTable()
result.Columns.Add("colname 1 ....")
result.Columns.Add("colname 2 ....")
Using inputFile As New StreamReader(filename) 'Open text file
Do Until inputFile.EndOfStream
Dim row = result.Rows.Add()
row(0) = "blah"
Loop
End Using
return result
End Function
Private Sub ShowResult()
dgv1.DataSource = ParseFile(filename)
End Sub

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

trying to output more than one filenam into textbox vb.net

Apologies if this is really simple but I'm fairly new to programming. I've created a program that uses an open dialog box and outputs the names of the file to a textbox.
Where I'm having issues is trying to get the textbox to display more than one line as all it seems to be doing is writing one line in the textbox.
The code I'm using is below, could someone please advise what I need to change so that I can get this to work.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim strFileName As String
OpenFD.Multiselect = True
OpenFD.InitialDirectory = "\\server\filename\"
OpenFD.Title = "Open a Text File"
OpenFD.Filter = "Text Files(.txt)|*.txt"
Dim DidWork As Integer = OpenFD.ShowDialog()
strFileName = OpenFD.FileName
If DidWork = DialogResult.Cancel Then
MsgBox("Cancel Button Clicked")
Else
strFileName = OpenFD.FileName
TextBox1.Text = strFileName += 1
End If
End Sub
I've managed to get everything else to work correctly but it's just this one thing.
Dim strFileName() As String
'...
Dim DidWork As Integer = OpenFD.ShowDialog()
If DidWork = DialogResult.Cancel Then
MsgBox("Cancel Button Clicked")
Else
strFileName = OpenFD.FileNames
TextBox1.Multiline = True
TextBox1.Text = ""
For Each sFile as String in strFileName
TextBox1.Text &= sFile & System.Enviroment.NewLine()
Next
End If
Set TextBox.Multiline property to True

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