在不丢失关联索引的情况下对组合框中的值进行排序
Sorting value in the combobox without losing the indexes which are associated
在我的应用程序中,我制作了一个从网页获取内容的代码。
从网络上获取的团队名称以顺序模式在组合框中排序,就像它们被读取一样。
我将读取的值存储在 Classifica_Table
中,因此我执行此代码以添加值:
For Each Team As Team_Data In Classifica_Table
ComboBox1.Items.Add(Team.Name) 'Populate Combo Boxes with team names
Next
现在我拥有像尤文图斯、罗马、国际米兰这样杂乱无章且不按字母顺序排列的东西。
我过去曾尝试用类似的方式订购它们:
For Each Team As Team_Data In Classifica_Table.OrderBy(Function(x) x.Name)
ComboBox1.Items.Add(Team.Name)
Next
这只对外观有效,因为 table 中的索引丢失了。
例如,如果我有关联:
尤文图斯 => 3
罗马 => 5
国际米兰 => 2
我发现自己喜欢:
国际米兰 => 3
尤文图斯 => 5
罗马 => 2
应该是:
国际米兰 => 2
尤文图斯 => 3
罗马 => 5
有没有一种方法可以在不丢失它们关联的索引的情况下对值进行排序?
更多详情:
Dim Classifica_Table() As Team_Data
各队索引与此相关:
Classifica_Table(ComboBox1.SelectedIndex).Partite_Giocate
和.Partite_Giocate
是在数据下载过程中增强的变量。
代码的完整结构:
1.在组合框中添加数据下载:
Classifica_Table = Fill_Table(Classifica_JS)
ComboBox1.Items.Clear()
For Each Team As Team_Data In Classifica_Table
ComboBox1.Items.Add(Team.Name) 'Populate Combo Boxes with team names
Next
2. 填充 table 对每个 "teams" 的变量进行赋值,所以我用来自互联网的数据对这个变量进行了赋值:
.Partite_Giocate..
.Vincite..
.Pareggi...
.Perdite...
.Goal_Segnati..
.Goal_Subiti...
这是一个整型变量
3.组合框中每个团队的索引:
Classifica_Table(ComboBox1.SelectedIndex).Partite_Giocate
等等..就是这样。
一种方法:
首先插入所有团队,这样您就有了一个包含所有团队的列表
For Each Team As Team_Data In Classifica_Table
ComboBox1.Items.Add(Team.Name)
Next
然后将它们替换到列表中的正确位置:
For Each Team As Team_Data In Classifica_Table
ComboBox1.Items.RemoveAt(Team.Index)
ComboBox1.Items.Insert(Team.Index, Team.Name)
Next
SelectedIndex 属性 跟踪组合框项目中所选项目的位置,它不能用作另一个集合中的索引。
如果您想保留与显示的项目(名称)关联的值,则应将向组合添加项目的方法替换为 DataBinding 方法;
Dim bs = new BindingSource()
bs.DataSource = Classifica_Table.OrderBy (Function(x) x.Name)
ComboBox1.DataSource = bs
ComboBox1.DisplayMember = "Name"
ComboBox1.ValueMember = "Partite_Giocate"
请记住,此方法现在需要重新评估您从组合框中读回值的每个点。现在您的 ComboBox 项是 Team class 的实例,您可以使用 SelectedItem 属性 和 DirectCast to Team 将它们取回。要获得项目的价值,您需要阅读 SelectedValue 属性 等等...
例如,如果您想跟踪组合框更改事件,您可以编写
Sub comboBox_SelectedIndexChanged(sender as Object, e as EventArgs)
Dim b = DirectCast(sender, ComboBox)
if b.SelectedItem IsNot Nothing Then
Dim t = DirectCast(b.SelectedItem, Team)
Console.WriteLine("Partite Giocate=" & t.Partite_Giocate)
End If
End Sub
编辑
查看您的项目,这是一个关于如何在 ComboBox1 的 SelectedIndexChanged 中更改您的代码的示例。在该方法中,您使用 SelectedIndex 值作为 Classifica_Table 结构数组的索引。这是错误的,当然你想重新排列组合中团队的显示。相反,您应该尝试使用名称查找当前选定的 Team_Data 值,因为这是您的组合中的唯一值。
Private Sub ComboBox1_SelectedIndexChanged(.....) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedIndex = ComboBox2.SelectedIndex Then
MsgBox(".....")
Exit Sub
End If
' Get the Team_Data corresponding to the Text currently selected
Dim td = Classifica_Table.Where(Function(x) x.Name = ComboBox1.Text).SingleOrDefault()
Dim Shield As Bitmap = Bitmap.FromStream(New MemoryStream(New WebClient().DownloadData(td.Shield)))
Shield_1.Image = Shield
Rating_1_1.Value = td.Partite_Giocate
Rating_1_2.Value = td.Vincite
Rating_1_3.Value = td.Pareggi
Rating_1_4.Value = td.Perdite
Rating_1_5.Value = td.Goal_Segnati
Rating_1_6.Value = td.Goal_Subiti
.....
当然,使用这种方法您不需要使用 DataBinding 功能,您可以对 Teams 进行排序,因为 Team_Data 的检索不再使用 SelectedIndex 属性
在我的应用程序中,我制作了一个从网页获取内容的代码。
从网络上获取的团队名称以顺序模式在组合框中排序,就像它们被读取一样。
我将读取的值存储在 Classifica_Table
中,因此我执行此代码以添加值:
For Each Team As Team_Data In Classifica_Table
ComboBox1.Items.Add(Team.Name) 'Populate Combo Boxes with team names
Next
现在我拥有像尤文图斯、罗马、国际米兰这样杂乱无章且不按字母顺序排列的东西。 我过去曾尝试用类似的方式订购它们:
For Each Team As Team_Data In Classifica_Table.OrderBy(Function(x) x.Name)
ComboBox1.Items.Add(Team.Name)
Next
这只对外观有效,因为 table 中的索引丢失了。 例如,如果我有关联:
尤文图斯 => 3
罗马 => 5
国际米兰 => 2
我发现自己喜欢:
国际米兰 => 3
尤文图斯 => 5
罗马 => 2
应该是:
国际米兰 => 2
尤文图斯 => 3
罗马 => 5
有没有一种方法可以在不丢失它们关联的索引的情况下对值进行排序?
更多详情:
Dim Classifica_Table() As Team_Data
各队索引与此相关:
Classifica_Table(ComboBox1.SelectedIndex).Partite_Giocate
和.Partite_Giocate
是在数据下载过程中增强的变量。
代码的完整结构:
1.在组合框中添加数据下载:
Classifica_Table = Fill_Table(Classifica_JS)
ComboBox1.Items.Clear()
For Each Team As Team_Data In Classifica_Table
ComboBox1.Items.Add(Team.Name) 'Populate Combo Boxes with team names
Next
2. 填充 table 对每个 "teams" 的变量进行赋值,所以我用来自互联网的数据对这个变量进行了赋值:
.Partite_Giocate..
.Vincite..
.Pareggi...
.Perdite...
.Goal_Segnati..
.Goal_Subiti...
这是一个整型变量
3.组合框中每个团队的索引:
Classifica_Table(ComboBox1.SelectedIndex).Partite_Giocate
等等..就是这样。
一种方法:
首先插入所有团队,这样您就有了一个包含所有团队的列表
For Each Team As Team_Data In Classifica_Table
ComboBox1.Items.Add(Team.Name)
Next
然后将它们替换到列表中的正确位置:
For Each Team As Team_Data In Classifica_Table
ComboBox1.Items.RemoveAt(Team.Index)
ComboBox1.Items.Insert(Team.Index, Team.Name)
Next
SelectedIndex 属性 跟踪组合框项目中所选项目的位置,它不能用作另一个集合中的索引。
如果您想保留与显示的项目(名称)关联的值,则应将向组合添加项目的方法替换为 DataBinding 方法;
Dim bs = new BindingSource()
bs.DataSource = Classifica_Table.OrderBy (Function(x) x.Name)
ComboBox1.DataSource = bs
ComboBox1.DisplayMember = "Name"
ComboBox1.ValueMember = "Partite_Giocate"
请记住,此方法现在需要重新评估您从组合框中读回值的每个点。现在您的 ComboBox 项是 Team class 的实例,您可以使用 SelectedItem 属性 和 DirectCast to Team 将它们取回。要获得项目的价值,您需要阅读 SelectedValue 属性 等等...
例如,如果您想跟踪组合框更改事件,您可以编写
Sub comboBox_SelectedIndexChanged(sender as Object, e as EventArgs)
Dim b = DirectCast(sender, ComboBox)
if b.SelectedItem IsNot Nothing Then
Dim t = DirectCast(b.SelectedItem, Team)
Console.WriteLine("Partite Giocate=" & t.Partite_Giocate)
End If
End Sub
编辑 查看您的项目,这是一个关于如何在 ComboBox1 的 SelectedIndexChanged 中更改您的代码的示例。在该方法中,您使用 SelectedIndex 值作为 Classifica_Table 结构数组的索引。这是错误的,当然你想重新排列组合中团队的显示。相反,您应该尝试使用名称查找当前选定的 Team_Data 值,因为这是您的组合中的唯一值。
Private Sub ComboBox1_SelectedIndexChanged(.....) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedIndex = ComboBox2.SelectedIndex Then
MsgBox(".....")
Exit Sub
End If
' Get the Team_Data corresponding to the Text currently selected
Dim td = Classifica_Table.Where(Function(x) x.Name = ComboBox1.Text).SingleOrDefault()
Dim Shield As Bitmap = Bitmap.FromStream(New MemoryStream(New WebClient().DownloadData(td.Shield)))
Shield_1.Image = Shield
Rating_1_1.Value = td.Partite_Giocate
Rating_1_2.Value = td.Vincite
Rating_1_3.Value = td.Pareggi
Rating_1_4.Value = td.Perdite
Rating_1_5.Value = td.Goal_Segnati
Rating_1_6.Value = td.Goal_Subiti
.....
当然,使用这种方法您不需要使用 DataBinding 功能,您可以对 Teams 进行排序,因为 Team_Data 的检索不再使用 SelectedIndex 属性