All of the combobox values will change if I check a new checkbox

Duc Nguyen 101 Reputation points
2021-07-28T06:47:34.46+00:00

I used this loop below for the checkbox and also the combobox.

Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
     
         For i = 0 To 11  
             Dim MycbmQty As ComboBox = CType(Me.Controls("cbmQty" & i), ComboBox)  
             AddHandler MycbmQty.SelectedIndexChanged, AddressOf cbmQty_SelectedIndexChanged  
             Dim myCheckBox As CheckBox = CType(Me.Controls("chkName" & i), CheckBox)  
             AddHandler myCheckBox.CheckedChanged, AddressOf chkName_CheckedChanged  
         Next  
      
  Private Sub chkName_CheckedChanged(sender As Object, e As EventArgs)  
         For i = 0 To 11  
             Dim myLabel As Label = CType(Me.Controls("lblSumName" & i), Label)  
             Dim myButton As Button = CType(Me.Controls("btnSum" & i), Button)  
             Dim myCombobox As ComboBox = CType(Me.Controls("cbmQty" & i), ComboBox)  
             Dim myLabel2 As Label = CType(Me.Controls("lblSumPrice" & i), Label)  
             Dim myLabel3 As Label = CType(Me.Controls("lblPriceTit" & i), Label)  
             Dim myLabel4 As Label = CType(Me.Controls("lblQtyTit" & i), Label)  
             Dim myCheckBox As CheckBox = CType(Me.Controls("chkName" & i), CheckBox)  
             'Allow to use all the functions when clicked the checkbox  
If myCheckBox.Checked = False Then  
myLabel.Enabled = False  
myLabel2.Enabled = False  
myLabel3.Enabled = False  
myLabel4.Enabled = False  
myButton.Enabled = False  
myCombobox.SelectedIndex = 0  
Else 'do not allow to use all the funtion unless clicked the checkbox  
myLabel.Enabled = True  
myLabel2.Enabled = True  
myLabel3.Enabled = True  
myLabel4.Enabled = True  
myButton.Enabled = True  
If myCombobox.Items.Count > 0 Then  
myCombobox.SelectedIndex = 1 'Start the combobox at 1 pizza when clicked the checkbox  
myCombobox.Enabled = True  
End If  
End If  
Next  
End Sub  
  
     Private Sub cbmQty_SelectedIndexChanged(sender As Object, e As EventArgs)  
         For i = 0 To 11  
             Dim MycbmQty As ComboBox = CType(Me.Controls("cbmQty" & i), ComboBox)  
             Dim myLabel As Label = CType(Me.Controls("lblSumname" & i), Label)  
             Dim MybtnSum As Button = CType(Me.Controls("btnSum" & i), Button)  
             Dim myLabel2 As Label = CType(Me.Controls("lblSumPrice" & i), Label)  
             Dim myLabel3 As Label = CType(Me.Controls("lblPriceTit" & i), Label)  
             Dim myLabel4 As Label = CType(Me.Controls("lblQtyTit" & i), Label)  
             Dim mychkname As CheckBox = CType(Me.Controls("chkName" & i), CheckBox)  
      
             'do not allow to use all the funtion when the quantity is 0  
             If MycbmQty.SelectedIndex = 0 Then  
                 myLabel.Enabled = False  
                 MybtnSum.Enabled = False  
                 myLabel2.Enabled = False  
                 myLabel3.Enabled = False  
                 myLabel4.Enabled = False  
                 mychkname.Checked = False  
                 MycbmQty.Enabled = False  
             End If  
             'store the quantity of the pizza  
             ord(i, 1) = Val(MycbmQty.Text)  
             myLabel2.Text = (ord(i, 1) * ord(i, 2)).ToString("C") 'Display total price of each pizza in $  
         Next  
 End Sub  

However, when I choose a different checkbox, the values of all the pizzas will reset to 1. And I want to keep the values

(First time)

118516-image.png

(Choose another checkbox, Hawaiian Pizza one)
118582-image.png

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,727 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 117.3K Reputation points
    2021-07-28T11:31:09.547+00:00

    Maybe replace 'If myCombobox.Items.Count > 0' with 'If myCombobox.Items.Count > 1 AndAlso myCombobox.SelectedIndex = 0'.


  2. Xingyu Zhao-MSFT 5,366 Reputation points
    2021-07-29T05:41:08.66+00:00

    Hi @Duc Nguyen ,
    You can use an array to store the combobox data.
    Here's the code you can refer to.

        Private arr As Integer() = New Integer(10) {}  
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            For i = 1 To 11  
                ...  
                arr(i) = 1  
            Next  
        End Sub  
      
        Private Sub chkName_CheckedChanged(sender As Object, e As EventArgs)  
            For i = 1 To 11  
                ...  
                If myCheckBox.Checked = False Then  
                    ...  
                    RemoveHandler myCombobox.SelectedIndexChanged, AddressOf cbmQty_SelectedIndexChanged  
                    myCombobox.SelectedIndex = 0  
                    AddHandler myCombobox.SelectedIndexChanged, AddressOf cbmQty_SelectedIndexChanged  
                Else   
                    ...  
                    myCombobox.Enabled = True  
                    If myCombobox.Items.Count > 0 Then  
                        RemoveHandler myCombobox.SelectedIndexChanged, AddressOf cbmQty_SelectedIndexChanged  
                        myCombobox.SelectedIndex = 1   
                        myCombobox.Text = arr(i)  
                        AddHandler myCombobox.SelectedIndexChanged, AddressOf cbmQty_SelectedIndexChanged  
      
                    End If  
                End If  
            Next  
        End Sub  
      
        Private Sub cbmQty_SelectedIndexChanged(sender As Object, e As EventArgs)     
            For i = 1 To 11  
                ...  
                If Not (MycbmQty.Text.Equals("0") OrElse MycbmQty.Text.Equals("1")) Then  
                    arr(i) = MycbmQty.Text  
                End If  
            Next  
        End Sub  
    

    Hope it could be helpful.

    Best Regards,
    Xingyu Zha0
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.