Thursday, May 26, 2016

How to rd text files VB.net





Rd text files VB.net
Today I will tch you how to rd text files (.txt) using VB.Net. Let´sgo :D
How to crte a project in Visual Studio?
Exists many ways to crte a new project in VS. For example, when you open your VS program, in theStart Pageyou will see the optionNewProject ...If you can´t see the Start Page you can also click inFile > New Project > Select Visual Basic > Forms AppliionRename your project name if you want and clickOK.It´s sy!
Start in the Form
For this tutorial we will need to crte a Text file in our to test our appliion.It´s simple.Open yourNotepadewritesome text. Then save it in Desktop and rename it asRdtextfiles_VB.net.Now in the form, add aTextBox(orrichtextbox) to place the text from the file. The textbox should supportMultiLine. You can enable it in the Properties of the textbox or using like I will show in the example. Center it in the form. Also we need a Button.
The …
PressControl + Alt + 0to open the page.Click incomboboxthat is in the top, left side. SelectFormX(events)and in theComboboxin the top right side selectLoad. This will erate theLoad Event of the FormWrite the next lines of :
PrivateSubForm3_Load(senderAsObject, eAsEventArgs)HandlesMe.LoadButton1.Text ="Open file ..."TextBox1.Multiline =True
EndSub
Now do the same to the button. Select in the leftButton1and in the right sideClickor more simple do double-click in the button in the designer.Write this:
IfSystem.IO.File.Exists(My.Computer.FileSystem.SpecialDirectories.Desktop &"\Rdtextfiles_VB.net.txt")Then'check if file existsTextBox1.Text =System.IO.File.RdAllText(My.Computer.FileSystem.SpecialDirectories.Desktop &"\Rdtextfiles_VB.net.txt")'if true the load the text from the file and insert in the textboxElseMsgBox("File not found",vbCritical+vbOKOnly,"Error")'if false show amessageboxEndIf
UsingOpenFileDialog
If you want, you can also you the controlOpenFileDialog. Paste this on you Click event of the button.
DimopenfileAsNewOpenFileDialogopenfile.CheckFileExists=Trueopenfile.InitialDirectory=My.Computer.FileSystem.SpecialDirectories.MyDocumentsopenfile.Multiselect=Falseopenfile.Filter="Text Files (.txt)|*.txt| Configuration Files (.inf)|*.inf"openfile.FilterIndex= 1openfile.Title="Select the text file ..."openfile.ShowDialog()IfDialogResult.OKThenTextBox1.Clr()TextBox1.Text =System.IO.File.RdAllText(openfile.FileName)EndIf

No comments:

Post a Comment