how to export listbox to notepad in vb? - vb.net

i have contains listbox. how to export listbox to notepad in vb? Please advise me

Notepad typically reads and writes plain old .txt files. These can be written with a standard StreamWriter. Just iterate over your ListBox and write each item to your file:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim txtFile As String = "C:\Users\mikes\Documents\Test.txt"
Using sw As New IO.StreamWriter(txtFile, False)
For Each item As String In ListBox1.Items
sw.WriteLine(item)
Next
End Using
End Sub
If you don't want to hard-code a filename, then use a SaveFileDialog to get a filename from the user.

Related

Is there a way to extract a variable from a subroutine in vb .net? or how do I declare custom event handlers and trigger them in a linked fashion?

I am trying to build this file copy utility in VB.Net, and I have this window:
The current folder button opens up a folder browser dialog, and I save the selected path from the dialog to a string variable which I then pass to a function. The function adds all files and directories present in that folder to the current folder listbox.
Now I need this filepath for the all files checkbox, which when triggered lists all the subdirectories and their contents in the currentfolder listbox.
Is there any way I can extract that filepath variable dynamically without hardcoding it? Or can I create custom event handlers and trigger them inside the current folder button handler?
Here is my code:
Public Class Form1
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FB As FolderBrowserDialog = New FolderBrowserDialog()
Dim srcpath As String
Dim flag As Integer = 1
FB.ShowDialog()
FB.ShowNewFolderButton = True
If (DialogResult.OK) Then
srcpath = FB.SelectedPath()
End If
listfiles(srcpath)
End Sub
Public Function listfiles(srcpath As String)
Dim dir As DirectoryInfo = New DirectoryInfo(srcpath)
Dim dirs As DirectoryInfo() = dir.GetDirectories()
Dim d As DirectoryInfo
Dim files As FileInfo() = dir.GetFiles()
Dim file As FileInfo
For Each file In files
CurrentFolderListBox.Items.Add(file.Name)
Next
For Each d In dirs
CurrentFolderListBox.Items.Add(d)
Next
'If CheckBox1.Checked = True Then
' CheckBox1_CheckedChanged(sender, New System.EventArgs())
'End If
End Function
Public Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChange
Dim item As DirectoryInfo
For Each i As DirectoryInfo In CurrentFolderListBox.Items
item = i
Next
End Sub
Any help would be most appreciated.
Well, for the list side lbox, and the right side lbox?
Why not fill each list box with a data source? You can have 1, or 2, or even 5 columns of data. The ListBox can display TWO of the columns.
So, VERY often a list box will have two values. the "value" based on what you select (often a PK database row value), and then you have the "text" value for display.
from FoxPro, ms-access, vb.net, and even asp.net?
A list box traditional has had the ability to "store" a set of values for your selection.
So, why not just put out the file list to a on the fly "data structure". You can quite much use a struct, a class or whatever.
However, might as well use a data table, since listbox supports "binding" to a table.
So, in the ListBox settings, you have these two settings:
so above is our "display"
And then set the "value" to the FULL file name like this:
So now we can say create a form like this:
So, our code to select the "from folder" can look like this:
Private Sub cmdSelFrom_Click(sender As Object, e As EventArgs) Handles cmdSelFrom.Click
Dim f As New FolderBrowserDialog
If f.ShowDialog = DialogResult.OK Then
txtFromFolder.Text = f.SelectedPath
ListBox1.DataSource = GetFileData(txtFromFolder.Text)
End If
End Sub
Public Function GetFileData(sFolder As String) As DataTable
Dim rstData As New DataTable
rstData.Columns.Add("FullFile", GetType(String))
rstData.Columns.Add("FileName", GetType(String))
' get all files from this folder
Dim folder As New DirectoryInfo(sFolder)
Dim fList() As FileInfo = folder.GetFiles
For Each MyFile As FileInfo In fList
Dim OneRow As DataRow = rstData.NewRow
OneRow("FullFile") = MyFile.FullName
OneRow("FileName") = MyFile.Name
rstData.Rows.Add(OneRow)
Next
Return rstData
End Function
so, we setup a two column "thing" (in this case a data table).
We fill it with our two values (FileName and FullFile).
So, we now have this:
I have selected two files on the left side, and thus we get this:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
For Each MySel As DataRowView In ListBox1.SelectedItems
Debug.Print(MySel.Item("FileName"))
Debug.Print(MySel.Item("FullFile"))
Next
End Sub
OutPut:
a.pdf
C:\Test2\a.pdf
b.pdf
C:\Test2\b.pdf
We could even include say file size in that table. But the "basic" concept here is that we can store + save data items into the list box, and that REALLY makes the code simple, since the list box now has JUST the file for display, but also enables us to have the full path name also.

opening a file from a path written in a text file?

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim objectreader As New System.IO.StreamReader(TextBox1.Text)
System.Diagnostics.Process.Start("TextBox2.Text = objectreader.ReadToEnd()")
objectreader.Close()
End Sub
End Class
I am trying to read from a text file where i have written a path to a file. I have been able to open a file using System.Diagnostics.Process.Start() and I have been able to read from a text file using StreamReader but I could not relate to it. How do I take the text from the text file and put it in the System.Diagnostics.Process.Start("here") to open the file. I am using vb.net visual studio 2013.
Remove the quotes. You aren't providing a string literal, you're providing the result of a method call:
TextBox2.Text = objectreader.ReadToEnd()
System.Diagnostics.Process.Start(TextBox2.Text)

Separate String Lines using StreamReader | VB.net

I am trying to make my own checklist app. I am using a ListView object to display each checklist item. I can add individual items to the object. I don't know how to save the ListView items on exit and then load them in on startup. (I tried using My.Settings, but it doesn't work.)
My solution was to make an import/export system using .txt files to store data. They are formatted like this:
Item1
Item2
Item3
When I import them, it all shows as one long item in ListView. I am using the code below.
Private Sub ChooseFileButton_Click(sender As Object, e As EventArgs) Handles ChooseFileButton.Click
If ImportFileDialog.ShowDialog = DialogResult.OK Then
Dim fileReader As String
fileReader =
My.Computer.FileSystem.ReadAllText(ImportFileDialog.FileName)
ImportFileDialog.RestoreDirectory = True
ChecklistObject.Items.Add(fileReader)
End If
End Sub
If anyone knows how to write individual items on their own line in a text file, that would be great too.
EDIT: Exporting doesn't work either. Using code below:
Private Sub ExportButton_Click(sender As Object, e As EventArgs) Handles ExportButton.Click
ExportFileDialog.Filter = "Keklist Save|*.kek"
If ExportFileDialog.ShowDialog = DialogResult.OK _
Then
ChecklistObject.Items.Item()
End If
End Sub
In your example code you are only adding one item to the list view so it will only show the one line.
You could use System.IO.File.ReadAllLines to read all the lines of the file into a string array.
Private Sub ChooseFileButton_Click(sender As Object, e As EventArgs) Handles ChooseFileButton.Click
If ImportFileDialog.ShowDialog = DialogResult.OK Then
Dim path As String = ImportFileDialog.FileName
Dim lines() As String = File.ReadAllLines(path)
ImportFileDialog.RestoreDirectory = True
For Each line in lines
ChecklistObject.Items.Add(line)
Next
End If
End Sub

Visual Basic Form. How to let users save text file where they want

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Dim CurrentDir As String = Environment.CurrentDirectory
Dim OutputFile2 As String = IO.Path.Combine(CurrentDir, "input.txt")
IO.File.WriteAllLines(OutputFile2, Result1.Lines)
End Sub
Right now, I have coding that saves a text file in the current directory. However, I want to have a browse button for users so that they can pick where this text file is saved. How do I proceed this?
I was trying it by my self and I'm having a trouble with using save file dialog. If you can teach me how to use a save file dialog or anyway to write save browse button, I would very appreciate it!
The documentation for the SaveFileDialog object contains an example.
Here is a tutorial on how to implement SaveFileDialog using Toolbox in Visual Studio like you mentioned. The code sample is in C# but it can be easily converted to VB.
Link: www.dotnetperls.com/savefiledialog
Private Sub button1_Click(sender As Object, e As EventArgs)
' When user clicks button, show the dialog.
saveFileDialog1.ShowDialog()
End Sub
Private Sub saveFileDialog1_FileOk(sender As Object, e As CancelEventArgs)
' Get file name.
Dim name As String = saveFileDialog1.FileName
' Write to the file name selected.
' ... You can write the text from a TextBox instead of a string literal.
File.WriteAllText(name, "test")
End Sub

Write and read a .ini file in VB.NET

Let's say I have a Button that, when clicked, will output text ("test text") to a .ini file in directory (C:/configdir) for example. How would I create this .ini file?
Then I have another Button that, when clicked, will show the content ("test text") of the .ini I just created. I am guessing I need to streamwrite the text.
Here's something that will work.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Using sw As New System.IO.StreamWriter("c:\myfile.ini", False)
sw.WriteLine("test text")
End Using
Using sr As New System.IO.StreamReader("c:\myfile.ini")
Dim Line As String = sr.ReadLine
Do While Line IsNot Nothing
MsgBox(Line)
Loop
End Using
End Sub