如何在更改ComboBox控件的SelectedIndex属性时更新Label的文本?

时间:2012-02-13 08:56:37

标签: vb.net combobox label selectedindexchanged

我想在这里做的是,对于添加到ComboBox的每个新项目,Label的文本属性将显示前一个数字的+1。

如果我没有为项目分配数字,我该如何写出来。

Items              Label
Tom                 1
Jane                2
Mary                3
John                4
etc..               etc..

修改:我的ComboBox绑定到数据源。

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

    Dim studentcheck = StudentTableAdapter.checkstudent(StudentNameTextBox.Text, StudentAddressTextBox.Text)

    If StudentNameTextBox.Text.Length = 0 Then

        MsgBox("Name is Empty")

    ElseIf StudentAddressTextBox.Text.Length = 0 Then

        MsgBox("Address is empty")

    ElseIf studentcheck Is Nothing Then

        Me.Validate()
        Me.StudentBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.LibraryDataSet)
        frmAddLoan.DisplayLoanTableAdapter.Fill(frmAddLoan.LibraryDataSet.DisplayLoan)
        frmAddLoan.ComboBox1.Update()
        MsgBox("Student Info Added")

    Else

        MsgBox("Student Name and Address have been used.")

    End If

End Sub

2 个答案:

答案 0 :(得分:2)

尝试使用此代码。

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    label1.Text = (ComboBox1.selectedIndex+1).ToString()

End Sub

答案 1 :(得分:0)

这个怎么样?

label1.Text = ComboBox1.Items.Count.ToString();