added samples
[windows-sources.git] / sdk / samples / FrameworkSamples / NCL / PingClient / vb / pingclient / pingclient.vb
blob38890a69a2b154666422f7f4cfa8b13ea142b871
1 '---------------------------------------------------------------------
2 ' This file is part of the Microsoft .NET Framework SDK Code Samples.
3 '
4 ' Copyright (C) Microsoft Corporation. All rights reserved.
5 '
6 'This source code is intended only as a supplement to Microsoft
7 'Development Tools and/or on-line documentation. See these other
8 'materials for detailed information regarding Microsoft code samples.
9 '
10 'THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
11 'KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
12 'IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
13 'PARTICULAR PURPOSE.
14 '---------------------------------------------------------------------
16 Imports System.Net.NetworkInformation
17 Imports System.Globalization
19 Public Class PingClientForm
20 Dim WithEvents pingClient As New Ping()
22 Private Sub pingClient_PingCompleted( _
23 ByVal sender As Object, ByVal e As PingCompletedEventArgs) _
24 Handles pingClient.PingCompleted
25 ' Check to see if an error occurred. If no error, then display the
26 ' address used and the ping time in milliseconds
27 If e.Error Is Nothing Then
28 If e.Cancelled Then
29 pingDetailsTextBox.Text &= _
30 "Ping cancelled." & Environment.NewLine
31 Else
32 If e.Reply.Status = IPStatus.Success Then
33 pingDetailsTextBox.Text &= _
34 " " & e.Reply.Address.ToString() & " " & _
35 e.Reply.RoundtripTime.ToString( _
36 NumberFormatInfo.CurrentInfo) & "ms " & _
37 Environment.NewLine
38 Else
39 pingDetailsTextBox.Text &= _
40 " " & GetStatusString(e.Reply.Status) & _
41 Environment.NewLine
42 End If
43 End If
44 Else
45 ' Otherwise display the error
46 pingDetailsTextBox.Text &= _
47 " Ping error." & Environment.NewLine
48 MessageBox.Show( _
49 "An error occurred while sending this ping. " & _
50 e.Error.InnerException.Message.ToString())
51 End If
52 sendPingButton.Enabled = True
53 End Sub
55 Private Function GetStatusString(ByVal status As IPStatus) As String
56 Select Case status
57 Case IPStatus.Success
58 Return "Success."
59 Case IPStatus.DestinationHostUnreachable
60 Return "Destination host unreachable."
61 Case IPStatus.DestinationNetworkUnreachable
62 Return "Destination network unreachable."
63 Case IPStatus.DestinationPortUnreachable
64 Return "Destination port unreachable."
65 Case IPStatus.DestinationProtocolUnreachable
66 Return "Destination protocol unreachable."
67 Case IPStatus.PacketTooBig
68 Return "Packet too big."
69 Case IPStatus.TtlExpired
70 Return "TTL expired."
71 Case IPStatus.ParameterProblem
72 Return "Parameter problem."
73 Case IPStatus.SourceQuench
74 Return "Source quench."
75 Case IPStatus.TimedOut
76 Return "Timed out."
77 Case Else
78 Return "Ping failed."
79 End Select
80 End Function
83 Private Sub sendPingButton_Click( _
84 ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendPingButton.Click
86 ' Select all the text in the address box
87 addressTextBox.SelectAll()
89 If addressTextBox.Text.Length <> 0 Then
90 ' Disable the Send button.
91 sendPingButton.Enabled = False
93 pingDetailsTextBox.Text &= _
94 "Pinging " & addressTextBox.Text & _
95 " . . ." & Environment.NewLine
97 ' Send ping request
98 pingClient.SendAsync(addressTextBox.Text, Nothing)
99 Else
100 MessageBox.Show("Please enter an IP address or host name.")
101 End If
103 End Sub
105 Private Sub cancelPingButton_Click( _
106 ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelPingButton.Click
108 ' Cancel any pending pings.
109 pingClient.SendAsyncCancel()
110 End Sub
112 End Class