Visual basic code samples.
Visual basic code samples
I want to share some very useful code samples that have been a real help to me at times. So I am sharing the visual basic code samples with you so you don’t have to search msdn.com for every code thing that you need.
Alright this first one is how to read a comma delimited text file. This one is great and I use it in almost every single application I use. Also you don’t have to use commas. I like to use a | bar to delimit the text file so real commas aren’t mistaken. There is one flaw however and that is when the text reaches the end of the text file and starts a new line even when the comma section is still going it will treat the new line like a comma. This is because it reads each line. So beware.
As New Microsoft.VisualBasic.FileIO.TextFieldParser(“C:Storm-EngineeringAssoc.txt”)
myreader.TextFieldType = FileIO.FieldType.Delimited
Dim currentRow As String()
While Not myreader.EndOfData
currentRow = myreader.ReadFields()
Dim currentField As String
For Each currentField In currentRow
If currentField <> “” Then
‘currentField is the value you want
Catch ex As Microsoft.VisualBasic.
MessageBox.Show(“Sorry, there was a problem loading one text file, make sure it still exists or hasn’t been moved!”, “Can’t find text files”, MessageBoxButtons.OK, MessageBoxIcon.Warning)
Alright up next is how to make a web request. Now it took me a very long time to find out that such a thing existed. I would look at C# examples and see these web requests being made and I couldn’t find them using VB so I thought they didn’t exist. But they did, and thankfully they did because I was using a web browser and then looking at the source code for the current url and that was painstaking! But this is much easier. And where I got this code(I book marked it!) was from here. Then you can have a good look at everything. You also have to import System.Net and Stystem.IO FYI. That is also why I never found the web request until I searched for it! Visual basic code sample here:
wrGETURL = WebRequest.Create(url to get as string)
‘ set proxies
Dim myProxy As New WebProxy(“myproxy”, 80)
myProxy.BypassProxyOnLocal = True
‘get the html code and then iterate throught it
Dim objStream As Stream
objStream = wrGETURL.GetResponse.GetResponseStream()
Dim objReader As New StreamReader(objStream)
Dim sLine As String = “”
Do While Not sLine Is Nothing
sLine = objReader.ReadLine
If Not sLine Is Nothing Then
‘sline is the value of the current line in the soure code for this page
So that is all of the visual basic code samples for now. Hope you enjoyed them all you visual basic programmers!