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

发布于 2025-01-04 19:02:28 字数 1136 浏览 2 评论 0原文

我在这里想要做的是,对于添加到 ComboBox 中的每个新项目,标签的文本属性将显示前一个数字的+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

What I want to do here is that for every new item added into the ComboBox, a Label's text property will display +1 from the previous number.

How do I write it out, assuming I didn't assign the items a number.

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

Edit: My ComboBox is binded to a data source.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隐诗 2025-01-11 19:02:28

尝试使用此代码。

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

Try using this code.

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
压抑⊿情绪 2025-01-11 19:02:28

这个怎么样?

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

How about this?

label1.Text = ComboBox1.Items.Count.ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文