tags:

views:

182

answers:

3

Background

I have a Windows Form with the following items

  • ComboBox
  • TextBox
  • Two Buttons: Forward and Back
  • A class - Items which holds a string int and double members

    if (ComboBox1.SelectedIndex == 2 && Items[index].Price > 50.00 ) { txtManu.Text = Items[index].Manu; txtPrice.Text = Convert.ToString(Items[index].Price); }

When I click the forward button on the form I expect all prices over 50.00 to be displayed in the txtPrice.Text TextBox but it's displaying all prices instead.

Forward button code snippet:

else if (comboBox1.SelectedIndex == 2 && Items[index].Price > 50.00)
        {

            index += 1;
            if (index == Items.Length) index = 0;

            txtManu.Text = Items[index].Manu;
        }

The ComboBox has index[0] and index[1] items: ComboBox1.SelectedIndex == 0 and ComboBox1.SelectedIndex == 1.

The forward button has index 0 and index 1 items too: if (comboBox1.SelectedIndex == 0) and if (comboBox1.SelectedIndex == 1)

Why isn't the if statement not executing?

Update

Here is the improved code for the example:

Items[0] = new items("Car", 30.00); Items[1] = new itemss("Cat", 55.00); Items[2] = new items("Cookie", 59.00);

ComboBox Code Snippet

if (ComboBox1.SelectedIndex == 0 && Items[index].Price > 50.00 ) 
{ 
    txtPrice.Text = Convert.ToString(Items[index].Price); 
} 

###Forward Button 
//single combobox 
if (comboBox1.SelectedIndex == 2 && Items[index].Price > 50.00) 
{ 
    index += 1; 
}

if (index == Items.Length) 
{
    index = 0; 
}
txtPrice.Text = Convert.ToString(Items[index].Price);
A: 

It looks like you may be incrementing that index too early. You check the value of Items[index], but then you increase index before using the value in Items[index]. So you are getting in reality: Items[index+1]

But in general I'm pretty confused by your question. Can you possibly post some more code and a more detailed explanation?

Thanks!

sidewinderguy
Yes I am incrementing the index to be able to move to next item. OR is this a problem? hmmm?
Akibo
Okay so your saying it should be this instead:else if (comboBox1.SelectedIndex == 2 index += 1; txtManu.Text = Items[index].Manu; }
Akibo
Or you mean this way: else if (comboBox1.SelectedIndex == 2 txtPrice.Text = Convert.ToString(Items[index].Price);index += 1;if (index == Items.Length) index = 0;}
Akibo
Sample data:Test items:Items[0] = new items(”Car”, 30.00);Items[1] = new items(”Cat”, 55.00);Items[2] = new items(”Cookie”, 59.00);The combobox:if (ComboBox1.SelectedIndex == 0 }The forward button:if (comboBox1.SelectedIndex == 0 if (index == Items.Length)index = 0; txtPrice.Text = Convert.ToString(Items[index].Price); }txtPrice { //’single textbox’ }
Akibo
+3  A: 

Did you check step by step with the debugger? I think there is an IndexOutBounds exception happening before the if. I would probably put a try catch on that block and see if you are getting an exception there.

Freddy
Yes I have checked debugger no errors or anything! Weird! Although it doesnt go to next item when I click forward button?? :(
Akibo
The comment by Akibo and the "accepted answer" status sort of conflict with each other. Is this solved or not?
Greg
A: 

Akibo, you might be able to get better help if you explain what you are trying to do in your windows form. Your explanation is not clear. You keep updating the code but without knowing what you are trying to get the windows form to do it is difficult for people to give you answers.

Jared
Ook in simple terms.I have windows form with one combobox,one textbox,one forward and back button.Combobox holds 'Items' values which are double type.When you choose 'Items' from combobox it should display the double variable in the textbox if its value is higher than 50.00.This works :) . When you click forward button to view next item with higher value than 50.00 it works too.But I get this error after items are displayed and I click next:.....>>>>
Akibo