【问题标题】:Find index of items in combox visual basic在组合框visual basic中查找项目索引
【发布时间】:2014-11-14 16:41:08
【问题描述】:

我正在尝试在表单加载时设置客户详细信息,但在确保将组合框设置为正确的索引时遇到了问题。

For iIndex As Integer = 0 To (Me.combo.Items.Count - 1)

    If Me.combo.Items(iIndex).Key = customer.CustTypeId Then
        Me.combo.SelectedIndex = iIndex
        Exit For
    End If
Next

Items.Key 来自我试图将其转换为标准 ComboBox 的 Klik 控件。我曾尝试访问该项目的 RowId,但无济于事。

谁能建议如何完成上述操作?

【问题讨论】:

  • 什么是“问题”?除非找不到密钥、大小写不匹配等,否则应该可以工作
  • 找不到密钥。 Key 是 Klik 控件上的一个属性,但据我所知,标准 ComboBox 不存在。我对 VB 很陌生,如果这很明显,我深表歉意
  • 如果您只是将字符串放入 CBO,请在测试中使用 If Me.combo.Items(iIndex) = customer.CustTypeId...。钥匙显然在你要替换的另一件事上
  • Me.combo.Items(iIndex) 是一个 DataRowView,我无法直接将其与 int 进行比较
  • 您可能希望在您的问题中包含这些重要的细节。在这种情况下,将其转换并在比较中使用相关值

标签: vb.net visual-studio-2010 user-interface controls


【解决方案1】:

你不能使用 Collection.IndexOf 方法吗?

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim one As String = "one"
        Dim two As String = "two"
        Dim three As String = "three"
        Dim four As String = "four"
        Dim five As String = "five"

        ComboBox1.Items.Add(one)
        ComboBox1.Items.Add(two)
        ComboBox1.Items.Add(three)
        ComboBox1.Items.Add(four)
        ComboBox1.Items.Add(five)

        MsgBox(ComboBox1.Items.IndexOf("three"))

    End Sub
End Class

【讨论】:

  • DataRowView 遗憾地没有 IndexOf 方法:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多