8 Replies - 8440 Views - Last Post: 14 March 2014 - 09:25 AM Rate Topic: -----

#1 dutchcoffee   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-January 14

Select next item in combobox dropdown

Posted 14 March 2014 - 08:13 AM

I'm having issues with selecting the next item in sequence in a combobox dropdown. The dropdown consists of letters A-Z and there is a timer that I have running to change the letter every x minutes. What I need to do is if, for example, letter A is selected, when the timer goes off, it will change to letter B, etc. If it's at letter Z, it needs to go back to the first item, letter A.

letterCode.SelectedIndex = letterCode.SelectedIndex + 1;
if (letterCode.SelectedIndex >= 26)
{
    letterCode.SelectedIndex += 0;
}


I have the above, but I keep getting an error:

InvalidArgument=Value of '26' is not valid for 'SelectedIndex'.


Does anyone have any ideas on how to get this working (I just need help with the letter changing logic block).

Is This A Good Question/Topic? 0
  • +

Replies To: Select next item in combobox dropdown

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15797
  • View blog
  • Posts: 63,297
  • Joined: 12-June 08

Re: Select next item in combobox dropdown

Posted 14 March 2014 - 08:21 AM

How many letters are there in the alphabet? 26.

Collections tend to start with 0 as their first index, right? So if you have 26 characters in a collection you would have index 0 to 25. Index 26 would not exist. Think of it like an 'out of bounds' error. Now if that means you need to check to see if your selected index is going to be 26 so you can set it to 0 then you should add that.
Was This Post Helpful? 0
  • +
  • -

#3 dutchcoffee   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-January 14

Re: Select next item in combobox dropdown

Posted 14 March 2014 - 08:38 AM

I changed '26' to read '25', but it still throws the same error.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15797
  • View blog
  • Posts: 63,297
  • Joined: 12-June 08

Re: Select next item in combobox dropdown

Posted 14 March 2014 - 08:42 AM

Okay - what I outlined required more changes than that.

Examples:

 letterCode.SelectedIndex += 0;

Are you certain you know what += does? That is equivolent to:

letterCode.SelectedIndex = letterCode.SelectedIndex + 0


... which is not the same as SETTING the selected index to 0.
Was This Post Helpful? 0
  • +
  • -

#5 dutchcoffee   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-January 14

Re: Select next item in combobox dropdown

Posted 14 March 2014 - 08:50 AM

Changing it to:

letterCode.SelectedIndex = 0;


Does not work, either.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15797
  • View blog
  • Posts: 63,297
  • Joined: 12-June 08

Re: Select next item in combobox dropdown

Posted 14 March 2014 - 08:51 AM

Let's see what you got.

I am gong to guess you are not checking to see if the next value is greater than the number of items in the collection before you attempt to set it...
Was This Post Helpful? 0
  • +
  • -

#7 dutchcoffee   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-January 14

Re: Select next item in combobox dropdown

Posted 14 March 2014 - 08:51 AM

letterCode.SelectedIndex = letterCode.SelectedIndex + 1;
if (letterCode.SelectedIndex > 25)
{
    letterCode.SelectedIndex = 0;
}

Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15797
  • View blog
  • Posts: 63,297
  • Joined: 12-June 08

Re: Select next item in combobox dropdown

Posted 14 March 2014 - 08:53 AM

Again.. why are you not doing your check BEFORE you assign values?
Was This Post Helpful? 0
  • +
  • -

#9 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Select next item in combobox dropdown

Posted 14 March 2014 - 09:25 AM

I'd do this totally differently
private void IncrementComboBoxIndex(ComboBox box) {
    int tempIndex = (box.SelectedIndex + 1 ) % box.Items.Count;
    box.SelectedIndex = tempIndex;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1