Add values of a combobox only if they are different MS Visual Basic?

0

Cordial greeting colleagues, it turns out that I have a form in MS Visual basic about activities that should be done, there is a combobox for each activity with values from 1 to 8, depending on what is selected in each combobox should be added to a variable a certain value, which in this case is valued, what I am trying to do is that when the activities have the same value in the combobox the variable takes the value only once, and does not add the 7 times, only that adds when each activity has different values selected in the combobox. Example: If the value 6 is selected in all the comboboxes, then the value is = 2951000 but if in 6 of the combobox the value 6 is selected and in the last combobox they selected the value 7, then the value of the variable would be the sum of the two values, valued = 2951000 + 2214000 (Value when selecting 7 in combobox). Previously, the exercise was done with a single combobox for all the activities and with this same calculation, using this code:

Code:

'Declaramos  condicion multiple de seleccion
        Select Case comboescalafon.SelectedIndex
            Case 0
                valoresca = 15493000


            Case 1
                valoresca = 12542000


            Case 2
                valoresca = 9591000

            Case 3

                valoresca = 7378000

            Case 4
                valoresca = 5902000


            Case 5
                valoresca = 4427000

            Case 6
                valoresca = 2951000


            Case 7
                valoresca = 2214000

            Case 8
                valoresca = 1476000
      End Select

This would be the example, in the majority of activities select the value 6 in the combobox therefore the sca value is 2951000, but in the report activity select the combobox 7 therefore sca value would be 2214000, so what I want to do is that if one is different as in this case, that all are 6 and the last one is 7 then values would have to be 2951000 + 2214000, this calculation is done by pressing the quote button.

    
asked by Kevin Burbano 02.02.2018 в 14:27
source

1 answer

1

As you did not put any kind of restriction then the best would be to take advantage of the tools of the language.

Let's use a Dictionary, with key and value of those that correspond to the selected combo.

We are going to make a function, that given the value of the combo, return the number that you want, for that we reuse your select. and then, we will go through the combos one by one, pass it to the function that returns the value, and try to put it in the dictionary, always, that deals with seeing collisions.

At the end of everything, we go through the dictionary and add the values that we have inserted.

Before the code, is this too much? no, because we are taking advantage of language tools in very small structures.

Sub Main()
    'array de valores, simula los combos
    Dim valores(7) As Int32
    valores(0) = 6
    valores(1) = 6
    valores(2) = 6
    valores(3) = 6
    valores(4) = 6
    valores(5) = 6
    valores(6) = 7
    'creo el diccionario
    Dim diccionario As New Dictionary(Of Integer, Integer)
    Dim vTemp As Int32
    'Ahora recorro el vector de valores 
    For i = 0 To 6
        vTemp = DevolverValores(valores(i))
        'ahora lo agrego al diccionario
        'me fijo si existe, si no existe lo agrego
        If Not diccionario.ContainsKey(vTemp) Then
            diccionario.Add(vTemp, vTemp)
        End If
    Next
    'Ahora recorro el diccionario y sumo los valores que tengan. 
    'rehuso vTemp como el total de la suma
    vTemp = 0

    For Each pair In diccionario
        vTemp = vTemp + pair.Value
    Next
    Console.WriteLine(vTemp)
    Console.ReadLine()

End Sub

Function DevolverValores(valor As Integer) As Integer
    Select Case valor
        Case 0
            DevolverValores = 15493000
        Case 1
            DevolverValores = 12542000
        Case 2
            DevolverValores = 9591000
        Case 3
            DevolverValores = 7378000
        Case 4
            DevolverValores = 5902000
        Case 5
            DevolverValores = 4427000
        Case 6
            DevolverValores = 2951000
        Case 7
            DevolverValores = 2214000
        Case 8
            DevolverValores = 1476000
        Case Else
            DevolverValores = 0
    End Select
End Function
    
answered by 02.02.2018 в 18:42