2012-02-13 117 views
1

我想在这里做的是,对于添加到ComboBox中的每个新项目,Label的文本属性将显示前一个数字的+1。如何在更改ComboBox控件的SelectedIndex属性时更新Label的文本?

我该如何写出来,假设我没有将项目分配给一个数字。我的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 
+0

想添加新项目时显示组合框的总项目数吗?请提供您的代码,将新项添加到组合框中,请.. – 2012-02-13 09:02:29

+0

我已经在代码中添加了关于 – CompleteNewb 2012-02-13 09:13:20

+0

问题的代码还是要将编号添加到组合框本身?但这取决于项目的排序顺序(或指示创建时间或PK列的日期时间列)。 http://stackoverflow.com/a/2023338/284240 – 2012-02-13 09:22:10

回答

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 
+0

非常感谢。有用! – CompleteNewb 2012-02-13 09:56:43

0

这个怎么样?

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

标签只显示我拥有的项目总数,我已经更新了我的问题。 – CompleteNewb 2012-02-13 09:25:11

相关问题