以下のサンプルコードは、コンポーネントのStateを使用してUIを更新する方法を示します。
private void myComponent_StateChanged(object sender, EventArgs e)
{
// 接続が確立されたとき、または閉じたとき(Stateプロパティが変更されたとき)に必ず発生します。
switch (myComponent.State)
{
case ConnectionState.Connected:
case ConnectionState.ConnectedAndSecure:
// インタフェースを更新して接続状態であることを示します。
textDisplay.ReadOnly = false;
textDisplay.BackColor = Color.WhiteSmoke;
Text = (myComponent.State == ConnectionState.Connected)
? "[Connected to " + myComponent.RemoteEndPoint.Address.ToString() + ":" +
myComponent.RemoteEndPoint.Port.ToString() + "]"
: "[Securely Connected to " + myComponent.RemoteEndPoint.Address.ToString() + ":" +
myComponent.RemoteEndPoint.Port.ToString() + "]";
break;
case ConnectionState.Closed:
// インタフェースを更新して接続されていないことを示します。
textDisplay.ReadOnly = true;
textDisplay.BackColor = Color.Silver;
this.Text = "[Not Connected]";
break;
}
}
Private Sub myComponent_StateChanged(ByVal sender As Object, ByVal e As EventArgs)
' 接続が確立されたとき、または閉じたとき(Stateプロパティが変更されたとき)に必ず発生します。
Select Case myComponent.State
Case ConnectionState.Connected, ConnectionState.ConnectedAndSecure
' インタフェースを更新して接続状態であることを示します。
textDisplay.ReadOnly = False
textDisplay.BackColor = Color.WhiteSmoke
Text = If((myComponent.State = ConnectionState.Connected), _
"[Connected to " & myComponent.RemoteEndPoint.Address.ToString() & ":" & _
myComponent.RemoteEndPoint.Port.ToString() & "]", "[Securely Connected to " & _
myComponent.RemoteEndPoint.Address.ToString() & ":" & _
myComponent.RemoteEndPoint.Port.ToString() & "]")
Case ConnectionState.Closed
' インタフェースを更新して接続されていないことを示します。
textDisplay.ReadOnly = True
textDisplay.BackColor = Color.Silver
Me.Text = "[Not Connected]"
End Select
End Sub