How to save a content of a specific textbox to file using VB.net - vb.net

Good morning everyone,
I'm working on my finals project in visual basic and i want to implement a save file option.
Right now the application has 2 textboxes (not richtextbox) which are the input and output of information.
What i want to do is save only the content of the output textbox. I managed to get it to save something to file but when its opened it always turns out to be empty.
the code example of the save file button is below, i have a feeling its not saving the content because it is not specified, yet i have no idea how to specify to only save the content of the one textbox even with the many hours of forum / google searching ive done to try and figure out on my own.
Dim myStream As Stream
Dim nsavetxtoutput As New SaveFileDialog()
'|All files (*.*)|*.*
nsavetxtoutput.Filter = "txt files (*.txt)|*.text"
nsavetxtoutput.FilterIndex = 2
nsavetxtoutput.RestoreDirectory = True
If nsavetxtoutput.ShowDialog() = DialogResult.OK Then
myStream = nsavetxtoutput.OpenFile()
If (myStream IsNot Nothing) Then
' Code to write the stream goes here.
myStream.Close()
End If
End If
Any and all insight would be much appreciated!
Thank you guys!
The Program allowed to save only the contents of the textbox with this function - Thanks very much everyone who replied. it helped out allot!
Private Sub NOTEPAD_BUTTON(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTSave2Notepad.Click
Dim nsavetxtoutput As New SaveFileDialog()
nsavetxtoutput.Filter = "txt files (*.txt)|*.text"
nsavetxtoutput.FilterIndex = 2
nsavetxtoutput.RestoreDirectory = True
If nsavetxtoutput.ShowDialog() = DialogResult.OK Then
IO.File.WriteAllText(nsavetxtoutput.FileName, txtoutput.Text)
End If
End Sub

Try this:
If nsavetxtoutput.ShowDialog() = DialogResult.OK Then
IO.File.WriteAllText(nsavetxtoutput.FileName, TextBox2.Text)
End If
Where TextBox2 is your output TextBox.
More information at MSDN Documentation.

You can write to a file using File.WriteAllText method. It takes two parameters. The first is the path to the file, which you get from the SaveFileDialog. The second is the value you want to write to the file.
See the article on MSDN

Related

How can i open more than 1000 files using openfiledialog? Or is there any method i can use?

I am writing a small program in which user will select set of file (mostly .CSV) files and my program will search through them and find required data.
It's working for up to 300 files, but after that its not working. It's giving me an error:
InvalidOperationExceprion was Unhandaled
Too Many files are selected. Select Fewer files and try again.
What should i do?
Like Eric Walker said, open file dialog works with 1,000's and 1,000's of files, there is no reason why it would stop at 300.
The best way to loop though files/folders to get info is to use the .GetFiles() method
Dim OFD As New FolderBrowserDialog
If OFD.ShowDialog = DialogResult.OK Then
For Each f In Directory.GetFiles(OFD.SelectedPath)
If Path.GetExtension(f) = ".txt" Path.GetExtension(f) = ".csv" Then
Dim reader As String() = File.ReadAllLines(f)
For each line as String in reader
DoSomethingAwesome(line)
Next
End If
Next
End If
This will cycle through every file in a certain Directory.
Now if you would like to cycle through every file in a file dialog, then you would use this.
Dim OFD As New OpenFileDialog()
OFD.Multiselect = True
If OFD.ShowDialog = DialogResult.OK Then
For Each f In OFD.FileNames
If Path.GetExtension(f) = ".txt" Path.GetExtension(f) = ".csv" Then
Dim reader As String() = File.ReadAllLines(f)
For Each line As String In reader
DoSomethingAwesome(line)
Next
End If
Next
End If
Give one of these a try depending on your preference.
As a side note, for future posts - please post your attempted code or more details on what you are trying to accomplish and where you are having trouble. Frankly, Im surprised you weren't downvoted (very surprised, people on SO can be ruthless). Just a friendly tip.

How do I specify relative file path in VB.net

I have a Webbrowser control in a form which displays a pdf file. I have to specify the URL as the file location on my computer.
eg.
E:\Folder\Manual.pdf
Both the pdf file and the program are in the same folder.
How do I specify the URL so that when I move the folder onto another drive, it opens the same pdf file?
The location of your application is
Dim path as String = My.Application.Info.DirectoryPath
The you could use:
Dim pdffile as String = IO.Path.Combine(path, "pdffile.pdf")
WebBrowser1.Navigate(pdffile)
If I understand you correctly, then:
Dim myPdf As String =
IO.Path.Combine(IO.Directory.GetParent(Application.ExecutablePath).FullName, "myPdfFile.pdf")
Another way you could do it is by using something like the code below;
Private Sub FamilyLocateFile_Click(sender As Object, e As EventArgs) Handles FamilyLocateFile.Click
If LocateFamilyDialog.ShowDialog = DialogResult.OK Then
FamilyWMP.URL = LocateFamilyDialog.FileName
ElseIf LocateFamilyDialog.ShowDialog = DialogResult.Cancel Then
MsgBox(MsgBoxStyle.Critical, "Error!")
End If
End Sub
What this will do is play a file in a Windows Media Player ActiveX object. The file can be selected with an OpenFile Dialog, which in this case is called LocateFamilyDialog. You don't need the ElseIf part of the statement, but you will need to insert an open file dialog and a control that can display PDFs. I think it'll work with WebBrowsers, but I'm not sure.

Visual Basic Form Coding (how to set a directory)

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Dim Newline As String
Newline = System.Environment.NewLine
System.IO.File.WriteAllLines("C:\Users\Sang\Desktop\filename.txt", Result1.Lines)
System.IO.File.AppendAllLines("C:\Users\Sang\Desktop\filename.txt", Result2.Lines)
System.IO.File.AppendAllLines("C:\Users\Sang\Desktop\filename.txt", values.Lines)
End Sub
This is my coding for making a text file on my desktop. However, my friend can not run this code because this code is only for myself as you can see above. I would like to use a folderbroswerdialog to generalize this coding for everyone. To be specific, if a user pressed this button on the form, folder browser should ask him where he wants to save this text file and text file should be saved in the directed folder or desktop. I tried to do it on my own by looking at many youtube videos and resources but I failed. How should I proceed this?
You can use the Environment.SpecialFolder Enumeration which contains the ubications of the system's directories that you can retrieve using Environment.GetFolderPath method :
Dim DesktopDir As String =
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Then you can use the Path.Combine method to properlly combine a directory/file path:
Dim OutputFile As String =
IO.Path.Combine(DesktopDir, "filename.txt")
Then:
IO.File.WriteAllLines(OutputFile, "Text Here")
Take a look here for using the FolderBrowserDialog:
http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog(v=vs.110).aspx
PERFECTLY great examples there.

Can anyone please help explain why document loads sometimes but doesn't other times?

The code is supposed to grab one or more links, grab the contents of that link(s) and load them into pdf. For the most part, it works.
The issue we are having currently is that sometimes document or documents are loaded correctly into the PDF; sometimes, the same document that loaded correctly a minute ago will either load blank into pdf or the image portion of the document is not loaded.
I thought that by adding Response.Clear(), the original load will flush out and document is ready to load again. That hasn't been the case so far.
Protected Sub btnGetCheck_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim doc As New PdfDocument()
Dim baseLink As String = _
"http://default.html?mode=print&ino=pan&s=g"
For Each r As GridViewRow In dsParcels.Rows
If CType(r.Cells(0).FindControl("link"), CheckBox).Checked Then
Dim url As String = baseURL & "&gen=" & r.Cells(1).Text
HtmlToPdf.ConvertUrl(url, doc)
' Setup HttpResponse headers
Response.Clear()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
End If
Next
doc.Save(Response.OutputStream)
Response.Flush()
Response.End()
End Sub
Can anyone suggest what's going wrong?
Kenny, you add your documents one-by-one, but every time you call Response.Clear() which resets response buffer (removing the already generated PDFs).

How can I make a "browse for file.." button in VB.net?

I'm trying to build a simple FTP uploader. How can I make it so the user can select a file to upload? See, what I want is to have a button (which I do) that a user can click, and it shows the OpenFileDialog (which I have), but then when they select a file, I want its path to be shown in a text box. How can I do that?
Try the following code
Dim dialog As New OpenFileDialog()
If DialogResult.OK = dialog.ShowDialog Then
TextBox1.Text = dialog.FileName
End If
One way is to convert the filename to a FileInfo which contains all sorts of information about the file including the path. This opens the dialog and displays the path of the selected file.
If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Dim fi As New System.IO.FileInfo(OpenFileDialog1.FileName)
TextBox1.Text = fi.DirectoryName
End If
You want to get the OpenFileDialog's property Filename property. See OpenFileDialog's class members here on MSDN.
Hope this helps
Add a OpenFileDialog And Add This Code
If YourOpenFileDialogName.ShowDialog = YourOpenFileDialogName.OK Then
textBox1.Text = YourOpenFileDialogName.FileName
End If