这听起来可能是一个非常愚蠢的问题,但我不确定答案。我的VB项目的“OnLoad”中有以下代码,工作正常:
Dim ServName = (From line In IO.File.ReadAllLines("c:\Testing\Servers.csv") _
Where line.Length > 0 _ Let Items = line.Split(","c) _ Select New With _ {.ServerRef = Items(0), _ .ServerName = Items(1), _ .ServerIP = Items(2) _ } _ ).ToList
For Each Server In ServName
Console.WriteLine("[{0} [{1}] [{2}]", _
Server.ServerRef, _
Server.ServerName, _
Server.ServerIP _
)
Next
Dim h As String
h = 0
For Each Server In ServName
ComboBox1.Items.Add(Server(h).ServerRef)
ComboBox2.Items.Add(Server(h).ServerName)
ComboBox3.Items.Add(Server(h).ServerIP)
h = h + 1
Next
但是我现在想要在ComboBox_SelectedIndexChanged部分中做的是从ComboBox1中选择你所做的选择,然后在ComboBox3中选择相应的信息。
例如(在CSV文件中):
服务器1,Cluster1,192.168.0.1
服务器2,Cluster2,172.16.16.1
等
我想要做的是当您选择Server1时,第3列中的IP将填充到文本框(TextBox1)中。 就目前而言,ComboBox2& ComboBox3只是为了显示正确读取信息。这些实际上不会被使用。
答案 0 :(得分:0)
目前,您正在从ServName
列表中提取信息,然后丢弃该列表。结果是,无法分辨一个框中的哪个值属于任何其他框中的哪个值。
我希望你做的是这样的事情:
ComboBox1.DataSource = ServName
ComboBox1.DisplayMember = "ServerRef"
ComboBox2.DataSource = ServName
ComboBox2.DisplayMember = "ServerName"
ComboBox3.DataSource = ServName
ComboBox3.DisplayMember = "ServerIP"
现在你可以使用引用相等来操作box2和box3,具体取决于box1中选择的项目。
修改强>
如果你遵循我的方法,你甚至不需要其他盒子。您只需一个方框即可获得所需的所有信息:
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim box As ComboBox = DirectCast(sender, ComboBox)
Dim server As Object = box.SelectedItem
Dim serverRef As String = server.ServerRef
Dim serverName As String = server.ServerName
Dim serverIP As String = server.ServerIP
End Sub