CodingCool VB.NET Simple ping app with VB.NET

Simple ping app with VB.NET

We are going to create an app that will ping a certain IP address. Let’s get started.

UI

Assuming you have already created your project. Add the following components:

  • a textbox, named txtIp
  • a button, named btnPing, set text to Ping
  • a textbox, named txtResult, multiline true

Arrange the components as in the image:

Code behind

Add the following code and compile.

Imports System.Net

Public Class Form1
	Private Sub btnping_Click(sender As Object, e As EventArgs) Handles btnping.Click
		If txtip.Text.Trim.Length = 0 Then
			txtresult.Text &= "Enter IP" & vbNewLine : Exit Sub
		End If
		If My.Computer.Network.IsAvailable = False Then
			txtresult.Text &= "Connect to a network." & vbNewLine : Exit Sub
		End If
		Try
			If My.Computer.Network.Ping(txtip.Text, 1000) Then
				Dim hostname As IPHostEntry = Dns.GetHostByName(txtip.Text)
				Dim ip As IPAddress() = hostname.AddressList
				txtip.Text = ip(0).ToString()
				txtresult.Text &= "Reply from " & txtip.Text & vbNewLine
			Else
				txtresult.Text &= "Request timed out." & vbNewLine
			End If
		Catch ex As Exception
			If ex.InnerException.Message = "No such host is known" Then
				txtresult.Text &= "Could not find host " & txtip.Text & vbNewLine
			End If
		End Try
	End Sub

	Private Sub txtip_KeyDown(sender As Object, e As KeyEventArgs) Handles txtip.KeyDown
		If e.KeyCode = Keys.Enter Then
			btnping.PerformClick()
		End If
	End Sub
End Class

Enter an IP or hostname and it will ping it.

Leave a Reply

Your email address will not be published. Required fields are marked *