【问题标题】:how to get a Combobox selection to display related information in labels?如何获得组合框选择以在标签中显示相关信息?
【发布时间】:2016-03-16 16:22:58
【问题描述】:

我试图让我的标签根据我的 cbo 选择显示信息。 (即 cbo = Class Name 然后标签将显示类标题和单位。)

Dim l As Integer

'Connect to Database and get the registration information
Using cnnOLEDB = New OleDbConnection(strConnectionString)

    ' Query the classschedule table for start of semester datas
    Using cmdOleDB = New OleDbCommand("SELECT [CourseTitle], [ShortTitle],[Units] FROM [Course]", cnnOLEDB)
        cnnOLEDB.Open()
        Using rdrOLEDB = cmdOleDB.ExecuteReader
            While rdrOLEDB.Read

                CboCourse.Items.Add(rdrOLEDB.Item(0).ToString)



                CourseArray(l, 0) = rdrOLEDB.Item(0).ToString
                CourseArray(l, 1) = rdrOLEDB.Item(1).ToString
                CourseArray(l, 2) = rdrOLEDB.Item(2).ToString
                l = l + 1

                lblCourseTitle.Text = (rdrOLEDB.Item(1).ToString)
                lblUnits.Text = (rdrOLEDB.Item(2).ToString)

                ' CboFormat.Items.Add()
                ' CboDayTime.Items.Add()
                ' CboLocation.Items.Add()
                ' CboName.Items.Add()
            End While
        End Using
    End Using
End Using

我知道这可能很容易做到,但我想不通。
我的信息位于从 MS-access 数据库中读取的数组中

【问题讨论】:

  • 一分钱可能要掉了...你在 cbo 中只有 rdrOLEDB.Item(0)(标题?),所以在 SelectedIndexChanged 事件中,你必须在该数组中找到它才能发布(x,1) 和 (x,2) 到标签。如果 cbo 绑定到 Course 对象列表,则可以使用 cbo.SelectedItem.ShortTitle 等。由于您必须将它们穿梭到该数组中,因此您应该硬着头皮使用列表并节省大量时间并在其他地方编码恐惧

标签: vb.net winforms ms-access combobox


【解决方案1】:

您可以使用数据绑定让处理数据变得轻松愉快。

在下面的代码中,我们设置标签以使用数据绑定并使用组合框作为索引。所以当你从组合框中选择一门课程时,标签会自动显示相关数据:

'Setup connection and command
Dim Connection As String = "Your connection string here"
Dim Command As String = "SELECT [CourseTitle], [ShortTitle],[Units] FROM [Course]"
Dim DataAdapter As New OleDbDataAdapter(Command, Connection)
Dim Table As New DataTable()

'Load data
DataAdapter.Fill(Table)

'Set data bindings
Me.ShortTitleLabel.DataBindings.Add(New Binding("Text", Table, "ShortTitle"))
Me.UnitsLabel.DataBindings.Add(New Binding("Text", Table, "Units"))

'Use a data bound combo box as index
Me.CourseComboBox.DataSource = Table
Me.CourseComboBox.DisplayMember = "CourseTitle"

别忘了导入Imports System.Data.OleDb

【讨论】:

  • 你先生太棒了,这简化了很多事情。我还是这个编码方面的新手,但非常感谢。
  • 有了这个,我应该能够稍微修改一下以添加到其他 cbo 选择中,对吗?只需修改 cmd str 并添加数据绑定
  • 您应该只使用一个组合框作为索引,例如使用列表框或网格作为索引,然后使用其他控件显示其他数据。
  • 顺便说一句,虽然您只能接受一个答案,但您可以投票给您认为有帮助的尽可能多的答案,包括通过单击答案附近的向上箭头来接受的答案。您也可以为好的问题投票。这根本不是强制性的,但可以让问题和答案对未来的读者更有用:)
  • 对不起,我的意思是类似的问题。不完全是这个,我也检查了你的,也对其他人投了赞成票。你是我的第一个赞成票哈哈
【解决方案2】:

我真的建议你避免使用 ArrayList 类。这是 NET Framework 中最早可用的集合类之一,但现在我们有多种选择来将对象实例存储在集合中。对于您的情况, List(Of T) 可能绰绰有余。

让我们开始定义我们的 T(A Course 类)

Public Class Course
    Public Title As String
    Public ShortTitle As String
    Public Units As Integer
End Class

有了这个类,你可以声明一个 List(Of Course),而不是一个无类型的 ArrayList

Dim courseList = New List(Of Course)

从你的数据库中读取数据的代码变成了

.....
Using rdrOLEDB = cmdOleDB.ExecuteReader
    While rdrOLEDB.Read
        Dim cc = New Course()
        cc.Title = rdrOLEDB.Item(0).ToString
        cc.ShortTitle = rdrOLEDB.Item(1).ToString
        cc.Units = Convert.ToInt32(rdrOLEDB.Item(2))
        courseList.Add(cc)
    End While
End Using

在循环结束并关闭所有 using 语句后,将组合的 DataSource 设置为 courseList

cboCourse.DataSource = courseList
cboCourse.DisplayMember = "Title"
cboCourse.ValueMember = "Title"

此时,在 SelectedIndexChanged 事件处理程序中,您可以通过这种方式检索您选择的项目

Sub cboCourse_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim cbo = CType(sender, ComboBox )
    Dim cc = CType(cbo.SelectedItem, Course)
    If cc IsNot Nothing
        lblCourseTitle.Text = cc.ShortTitle
        lblUnits.Text = cc.Units.ToString()
    End If

End Sub

【讨论】:

  • 你设置ComboBox的DataSource如上图了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-22
  • 1970-01-01
  • 2019-04-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多