John Christophoros John Christophoros - 3 months ago 12
C# Question

Importing a string/int from a text file and matching it to an index value of a ComboBox

I am importing a string "0000",

string strNotepad;
strNotepad = strNotepad + objReader.ReadLine();
int dec = int.Parse(strNotepad, System.Globalization.NumberStyles.HexNumber);
string strNotepad2 = dec.ToString();
ListBox1.Text = strNotepad2;


XAML Code

<ComboBox x:Name="ComboBox" KeyboardNavigation.TabIndex="6" HorizontalAlignment="Left" VerticalAlignment="Top" Width="61" Margin="94,43,0,0" IsEditable="True" MouseMove="comboBox_MouseMove" IsReadOnly="True">
<TextBlock> <Run Text="Apple"/></TextBlock>
<TextBlock><Run Text="Pear"/></TextBlock>
<TextBlock><Run Text="Orange"/></TextBlock>
</ComboBox>


I need code that will match my string or an int to the index value 0,1 or 2 and then show that in the content of the combobox in the comboBox

Answer

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.