Private Sub Button1_Click (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles for. Click
Dim TXT As String=Me. GetTxt
SaveTxt (TXT)
End Sub
'get the stored text
Private Function getTxt () As String
'Dim x As Integer (0 To 10)
'For I As Integer=0 To x.L ength - 1
'x (I)=CInt (100 * Rnd ())
'Next
'Dim STR As String=""
'For j As Integer=0 To x.L ength - 1
'Str +=x (j). ToString & amp; VbCrLf
'Next
'Return STR
Dim x (1) As String
Dim Str As String=""
X (0)=TextBox1. Text
X (1)=TextBox2. Text
For j As Integer=0 To 1
Str +=x (j) & amp; VbCrLf
Next
Return the Str
End the Function
'the stored text
Private Sub SaveTxt (ByVal v As String)
Dim fp As String=GetSavePath ()
If the String. IsNullOrEmpty (fp)=False Then
'the StreamWriter
Dim Swriter As New StreamWriter (fp, False) 'cover or create a New
Swriter. WriteLine (v)
Swriter. Close ()
End the If
End Sub
'get store path
Private WithEvents sd As New SaveFileDialog
Private Function GetSavePath () As String
Sd. The FileName=""
Sd. AddExtension=True 'if there is no extension file, the default for TXT extension
Sd. DefaultExt="TXT"
Sd. Filter="TXT file (*.txt) | *. TXT"
Sd. ShowDialog ()
Return sd. The FileName
End the Function
Code is looking for on the Internet, modify,
I want to achieve the above the contents of the TXT file to read into the textbox,
'* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
'the Textbox1. Text=""
'Dim STR As IO. StreamReader.=New IO StreamReader (" C: \ 123. TXT ", System. Text. The Encoding. The Default)
'Do Until STR. EndOfStream
'the TextBox1. Text=TextBox1. Text & amp; Str. ReadLine ()
'Loop
'Str. The Close ()
'this code can read 123. TXT the value in the
According to this code can read the contents of the fixed fixed TXT file directory, and what I want is stored in the TXT file, is can choose TXT files in the directory,
CodePudding user response:
Use OpenFileDialog to get the file name,To "C: \ 123. TXT" can be replaced with the new file name
CodePudding user response:
How to get the directory?CodePudding user response:
Add a button to add a OpenFileDialog controlsPrivate Sub Button1_Click (sender As System. Object, e the As System. EventArgs) Handles for. Click
OpenFileDialog1. Multiselect=False 'can only select a file
OpenFileDialog1. Filter="text document | *. TXT" 'choose the type of file, if there is no this line is the default choice all file types
OpenFileDialog1. ShowDialog () 'opens the file form, this is the main other is about the form of option Settings
End Sub
CodePudding user response:
According to the problems, roughly divided into hair under several steps1, triggering the "save" (usually a click on the button)
2, call the dialog box, save the file path, (use OpenFileDialog instead of SaveFileDialog)
3, read the content TextBox, save to file, many methods, but I most like to use the ws,
Dim ws=CreateObject (" Wscript. Shell ")
Dim fso=CreateObject (" SCripting. FileSystemObject ")
Dim f=fso. CreateTextFile (path, True)
F.w rite TextBox Text
CodePudding user response:
Thank you, has been solvedCodePudding user response:
VS you some right click insert code is actually a reference code to read and write files to you, very simple,CodePudding user response:
Thank you for your warrior upstairs!