-3

This is an old question which I have come back to trying to make sense of it and improve its quality.

It seems the essence of the question was "Setting a comboxbox index based if an int is odd or even", and it came down to figuring out if an int was odd or even, which has been answered many times on stack, so i will just post a link to a good answer and mark this as a duplicate

Testing if a list of integer is odd or even

JohnChris
  • 1,360
  • 15
  • 29
  • John, there are lots and lots of issues with this question and the code provided (I think you want ```string strNotepad2 = dec.ToString()``` in the top block for starters). Could you an image/description of what you want the combobox to look like as it is extremely unusual to embed a listbox within a combobox. – ibebbs Sep 06 '16 at 10:33
  • Sorry for the confusion, it should have been a comboBox... thanks for the assistance though – JohnChris Sep 06 '16 at 10:53
  • Fine, exactly the same approach should work. Is it what you were looking for? – ibebbs Sep 06 '16 at 10:58

1 Answers1

1

Ok, try this:

<ListBox x:Name="ListBox1">
    <ListBoxItem Content="None"/>
    <ListBoxItem Content="Odd"/>
    <ListBoxItem Content="Even"/>
</ListBox>

string strNotepad = strNotepad + objReader.ReadLine();
int dec = int.Parse(strNotepad, System.Globalization.NumberStyles.HexNumber);
ListBox1.SelectedIndex = (dec % 2) == 1 ? 1 : 2;

Doesn't really seem to be any point to having "None" in the ListBox though as it'll never be selected.

ibebbs
  • 1,963
  • 2
  • 13
  • 20
  • Thats exactly what I needed, can you give some more info about the last line of code just curious – JohnChris Sep 06 '16 at 11:04
  • 1
    The '%' is the [modulus operator](https://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx) which 'computes the remainder after dividing its first operand by its second'. By performing a modulus of 2 if the remainder is 1 we know the input was odd, otherwise it's even. The ['?' operator](https://msdn.microsoft.com/en-us/library/ty67wk28.aspx)' returns one of two values depending on the value of a Boolean expression. Following is the syntax for the conditional operator'. – ibebbs Sep 06 '16 at 11:11
  • Yea, I just found the mistake, the None, Odd and Even arent related to the numbers, I should have put string such as Apple, Pear, Orange, the code above only returns 2 index values, I need 3 and in the future even more – JohnChris Sep 06 '16 at 11:21
  • The solution was simply Listbox1.SelectedIndex = dec; As the note pad then selects whichever index. Thanks again, simple solution but I couldn't find it:S – JohnChris Sep 06 '16 at 11:25