类型的值'字符串'无法转换为' 1维数组的对象'在组合框上

时间:2014-10-18 11:20:22

标签: vb.net combobox

我有这样的代码

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim myClientMachineAddressList As IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())
    Dim myClientMachineIP As String = myClientMachineAddressList.AddressList(0).ToString()
    Me.Text = GetComputerName()
    Me.ComboBox1.Items.AddRange(myClientMachineIP)
End Sub

我试图获得一个活跃的Ipv4地址适配器并将其添加到组合框但我收到此错误类型'字符串'无法转换为' 1维数组的对象'

1 个答案:

答案 0 :(得分:1)

您可以选中AddressFamiliy来过滤您的地址, 要过滤所有IPv4地址,AddressFamiliy应等于AddressFamily.InterNetwork

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim myClientMachineAddressList As IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())
    Dim myClientMachineIP = myClientMachineAddressList.AddressList.Where(Function(a) a.AddressFamily = AddressFamily.InterNetwork).ToArray()
    Me.Text = GetComputerName()
    Me.ComboBox1.Items.AddRange(myClientMachineIP)

End Sub