01-02-2011, 12:58 PM
(This post was last modified: 01-11-2011, 09:36 PM by Resistance.)
Hey guys. I have not posted a tutorial in weeks. So I thought this thread would be very helpful; A lot of VB applications I make need an active internet connection in order to work, so we will use a function to detect whether theres an active internet connection at the moment or not. This can help prevent fatal errors while the application requires
internet when trying to run something when there is no internet connection.
Heres the code, first plug in the Function anywhere between 2 classes as in "Form1_Load and CheckBox1_CheckState."
Function We Are Using:
Now to manipulate the code a little bit:
Now obviously you may change it from Form1_Load to a BackroundWorker or Timer. Thats just me. Enjoy! Post replies if you have questions!
internet when trying to run something when there is no internet connection.
Heres the code, first plug in the Function anywhere between 2 classes as in "Form1_Load and CheckBox1_CheckState."
Function We Are Using:
Code:
Public Function IsConnectionAvailibe() As Boolean
Dim objURL As New System.Uri("http://192.168.1.1/")
Dim ObjWebRequest As System.Net.WebRequest
ObjWebRequest = System.Net.WebRequest.Create(objURL)
Dim objResp As System.Net.WebResponse
Try
objResp = ObjWebRequest.GetResponse
objResp.Close()
ObjWebRequest = Nothing
Return True
Catch ex As Exception
objResp = Nothing
ObjWebRequest = Nothing
Return False
End Try
End Function
Now to manipulate the code a little bit:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If IsConnectionAvailibe() = True Then 'If there was internet on at the moment the application opened, add statements to enable the application for internet usage.
Label1.Visible = False
TextBox2.Enabled = True
TextBox1.Enabled = True
Button1.Enabled = True
Button2.Enabled = True
Button3.Enabled = True
ElseIf IsConnectionAvailibe() = False Then 'If there wasn't internet when the application opened, add statements to disable internet using items to prevent errors.
Label1.Visible = True
TextBox2.Enabled = False
TextBox1.Enabled = False
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
End If
End Sub
End Class
Now obviously you may change it from Form1_Load to a BackroundWorker or Timer. Thats just me. Enjoy! Post replies if you have questions!