How to Create a Random ID Generator in Visual Basic
Submitted by Yorkiebar on Tuesday, October 8, 2013 - 03:58.
Language
Introduction:
Welcome to a tutorial on how to create a Random ID generator.
Steps of Creation:
Step 1:
First lets import System.Net and System.Text so we can use the internet and a function get a string between two points within a given string. Let's import those now and create our getbetween function...
Step 2:
Now, on a button click event, lets get the response from fakenamegenerator.com (which will provide us with the details) and then extract the information. I have chosen to only extract;
Name
Address
Phone
Email
However, you can choose to add more.
Once we have extracted all the information we output it in a messagebox line by line. We also surround the whole thing in a Try and Catch statement just in case the website returns a bad response.
Project Complete!
That's it! Below is the full source code and download to the project files.
- Imports System.Net
- Imports System.Text.RegularExpressions
- Private Function GetBetween(ByVal Source As String, ByVal Str1 As String, ByVal Str2 As String, Optional ByVal Index As Integer = 0) As String
- Return Regex.Split(Regex.Split(Source, Str1)(Index + 1), Str2)(0)
- End Function
- Try
- Dim r As HttpWebRequest = HttpWebRequest.Create("http://www.fakenamegenerator.com/")
- r.KeepAlive = True
- r.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36"
- Dim re As HttpWebResponse = r.GetResponse()
- Dim src As String = New System.IO.StreamReader(re.GetResponseStream()).ReadToEnd()
- Dim nameSpare As String = GetBetween(src, "<div class=""address"">", "</div>")
- Dim name As String = GetBetween(nameSpare, "<h3>", "</h3>")
- Dim address As String = GetBetween(nameSpare, "<div class=""adr"">", "</div>")
- address = address.Replace(" ", "")
- address = address.Replace("<br/>", ", ")
- address = address.Replace("</br>", ", ")
- Dim extraSpace As String = GetBetween(src, "<div class=""extra"">", "</div>")
- Dim phone As String = GetBetween(extraSpace, "<li class=""tel""><span class=""value"">", "</span></li>")
- Dim email As String = GetBetween(extraSpace, "<li class=""email""><span class=""value"">", "</span>")
- MsgBox(name & vbNewLine & address & vbNewLine & phone & vbNewLine & email)
- Catch ex As Exception
- MsgBox(ex.ToString())
- End Try
- Imports System.Net
- Imports System.Text.RegularExpressions
- Public Class Form1
- Private Function GetBetween(ByVal Source As String, ByVal Str1 As String, ByVal Str2 As String, Optional ByVal Index As Integer = 0) As String
- Return Regex.Split(Regex.Split(Source, Str1)(Index + 1), Str2)(0)
- End Function
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Try
- Dim r As HttpWebRequest = HttpWebRequest.Create("http://www.fakenamegenerator.com/")
- r.KeepAlive = True
- r.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.69 Safari/537.36"
- Dim re As HttpWebResponse = r.GetResponse()
- Dim src As String = New System.IO.StreamReader(re.GetResponseStream()).ReadToEnd()
- Dim nameSpare As String = GetBetween(src, "<div class=""address"">", "</div>")
- Dim name As String = GetBetween(nameSpare, "<h3>", "</h3>")
- Dim address As String = GetBetween(nameSpare, "<div class=""adr"">", "</div>")
- address = address.Replace(" ", "")
- address = address.Replace("<br/>", ", ")
- address = address.Replace("</br>", ", ")
- Dim extraSpace As String = GetBetween(src, "<div class=""extra"">", "</div>")
- Dim phone As String = GetBetween(extraSpace, "<li class=""tel""><span class=""value"">", "</span></li>")
- Dim email As String = GetBetween(extraSpace, "<li class=""email""><span class=""value"">", "</span>")
- MsgBox(name & vbNewLine & address & vbNewLine & phone & vbNewLine & email)
- Catch ex As Exception
- MsgBox(ex.ToString())
- End Try
- End Sub
- End Class
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Add new comment
- 358 views