vb.net如果文本框是一个字符串

时间:2012-08-30 22:59:45

标签: vb.net visual-studio-2010

很抱歉错误标题,因为我不知道该怎么称呼标题我想要保存文本框值,如果它是一个整数到一个名为IP的配置文件,如果它是一个字符串的名称DNS这是代码:

Public Class ServerSettings

Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
    If IPHostInfo.Text = "" Then ' here if it's a string save it to my.settings.dns
        My.Settings.DNS() = IPHostInfo.Text

    Else : IPHostInfo.Text = ""
        My.Settings.IP() = IPHostInfo.Text 'and if it's an integer
    End If
    My.Settings.PORT() = IPInfo.Text
    My.Settings.TOut() = TOut.Text
    IPHostInfo.Text = IPHostInfo.Text
    IPInfo.Text = IPInfo.Text
    TOut.Text = TOut.Text
    Me.DialogResult = System.Windows.Forms.DialogResult.OK
    Me.Close()
End Sub

Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
    Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
    Me.Close()
End Sub

结束班

1 个答案:

答案 0 :(得分:0)

如果您需要确定字符串是否是有效的IP地址,我认为最简单的方法是简单地调用IPAddress.TryParse,例如:

If IPAddress.TryParse(IPHostInfo.Text, Nothing) Then
    ' It's an IP Address
    My.Settings.IP() = IPHostInfo.Text
Else
    ' It's not an IP Address, so it must be a DNS name
    My.Settings.DNS() = IPHostInfo.Text
End If