2012-02-13 112 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

相關問題