Problem with combobox selecting the Index c #

2

Well the following happens I have 2 ComboBox with names of countries then let's say the following a trip from Spain to Germany worth 800000 vice versa is worth the same so I have it like this

if(combo1.Selectedindex==1 && combo2.SelectedIndex==2 ||
    combo1.Selectedindex==1 || combo2.SelectedIndex==2)
{
    //aca lo que va a hacer ,el problema es que solo me da el valor 
    //si selecciono el index 1 y el index 2 pero si pongo index 2 y luego 
    //index 1 me da un valor incorrecto pienso que es en la forma que pongo 
    //que capture los index
}

I only try to select the index 1 and 2 or 2 and 1 and send me the same value.

Thanks for your time.

    
asked by user90058 23.09.2018 в 05:33
source

2 answers

1

For the Or conditions to behave as expected, you should isolate the And conditions.

Here is an example:

if( (combo1.Selectedindex == 1 && combo2.Selectedindex == 2) || 
    (combo1.Selectedindex == 2 && combo2.Selectedindex == 1) )
{
    //do Something
}

This way you can make sure that one condition or another is met.

    
answered by 23.09.2018 / 11:09
source
0

Let me see if I understand what you want to do:

  

Combo1 -------- Combo2

     

Spain --- to ----- Germany="The trip costs 300"

     

Combo1 --------. Combo2

     

Germany --- a - Spain="The trip costs 300"

Then suppose that the content of your first ComboBox the index 1 is equal to Spain and the index 2 to Germany, your second ComboBox has the same order.

If so, then in your condition it should read like this:

if(combo1.Selectedindex == 1 && combo2.Selectedindex == 2 || 
combo1.Selectedindex == 2 && combo2.Selectedindex == 1){
    Console.Write("El viaje cuesta 300");
}

So if I'm not wrong in the condition the logic is as follows yes combobox1 the index is equal to 1 and in the combobox2 the index is equal to 2 or in the combobox1 the index is equal to 2 and the combobox2 index is equal to 1 then print the following message

  

Using & amp; (AND) in a condition if is for both conditions to be met and the || (OR) is that any of the written conditions can be met.

    
answered by 23.09.2018 в 07:20