question

DucNguyen-0389 avatar image
0 Votes"
DucNguyen-0389 asked XingyuZhao-MSFT commented

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

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






dotnet-visual-basic
image.png (48.0 KiB)
image.png (48.2 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @DucNguyen-0389 ,
Do you still meet this issue? Please let me know if you need further help.

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered DucNguyen-0389 commented

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


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks, but I tried and for the first time the combobox will be like the image below
118766-image.png


0 Votes 0 ·
image.png (10.3 KiB)
XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered

Hi @DucNguyen-0389 ,
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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.