tags:

views:

75

answers:

2

I've got a newbie question about the combo box. The problem I'm having is with the selection changed event I'm binding to it. Here is my Code:

        <ComboBox Height="23"
              HorizontalAlignment="Left"
              Margin="12,67,0,0"
              Name="comboBox1"
              VerticalAlignment="Top"
              Width="112"
              SelectionChanged="comboBox1_SelectionChanged">
        <ComboBoxItem Content="Pokey"
                      IsSelected="True" />
        <ComboBoxItem Content="Octo-ooze" />
        <ComboBoxItem Content="Bolt" />
        <ComboBoxItem Content="Fink" />
    </ComboBox>

And in the code behind:

    private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        switch (comboBox1.Text)
        {
        case "Pokey" :
        tbMinutes.Text = "0";
        tbSeconds.Text = "8";
        break;
        case "Octo-ooze":
        tbMinutes.Text = "0";
        tbSeconds.Text = "16";
        break;
        case "Bolt":
        tbMinutes.Text = "0";
        tbSeconds.Text = "23";
        break;
        case "Fink":
        tbMinutes.Text = "1";
        tbSeconds.Text = "40";
        break;
        }
    }

Whats happening is tbMinutes.Text and tbSeconds.Text uses the old values, so for example if the combo box is initially Pokey and I change it to Bolt, it will still use the Pokey values as if its lagging one step behind.

+1  A: 

The comboBox1.Text returns the value prior to the selection change, which is what is causing your problem. You can use ComboBox.SelectedItem to access the newly selected item.

The change would look something like this

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  ComboBoxItem item = comboBox1.SelectedItem as ComboBoxItem;
  if (item != null)
  {
    switch (item.Content.ToString())
    {
      // ... Rest of your code here
    }
  }
}

However, you potentially have a problem, because the SelectionChanged event is fired when the data is loaded because you have set IsSelected="True" in you Xaml, if the textboxes are not yet created this will cause problem when the textbox values are initially set. To get around this, remove the IsSelected setting from the Xaml and just set the selection in the Load event of the Window.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
  comboBox1.SelectedIndex = 0;
}  
Chris Taylor
+1  A: 

In this case you should use the ComboBox.SelectedItem like mentioned by Chris Taylor. But be careful with setting the IsSelected Property of the ComboBoxItem in the XAML, because if you do it like this:

<ComboBox Height="23"
          HorizontalAlignment="Left"
          Margin="12,67,0,0"
          Name="comboBox1"
          VerticalAlignment="Top"
          Width="112"
          SelectionChanged="comboBox1_SelectionChanged">
        <ComboBoxItem Content="Pokey"
                  IsSelected="True" />
        <ComboBoxItem Content="Octo-ooze" />
        <ComboBoxItem Content="Bolt" />
        <ComboBoxItem Content="Fink" />
</ComboBox>
    <TextBox Name="tbMinutes" />
    <TextBox Name="tbSeconds" />

You will get a NullReferenceException, because when InitializeComponents is called, the components are initalized in the order defined in the XAML. If your first ComboBoxItem is initialized, a event is fired. But the TextBoxes are not initialized and null leading to the Exception.

One approach to solve the problem would be if you put the TextBoxes before the ComboBox in the XAML, but a much better approach would be if you hook up the event of the ComboBox in the code like this:

public MainWindow()
{
    InitializeComponent();
    comboBox1.SelectionChanged += comboBox1_SelectionChanged;
}
mpistrich
@mpistrich, nice we saw the same thing. Minor variation on the solution, 6 of one half a dozen of the other.
Chris Taylor
@Chris Taylor: Seems like we both tested the original code ;). But your code still preserves the selection at startup, so I would pick your solution.
mpistrich
@mpistich, this is only a potential problem, if the textboxes are declared prior to the combobox in the Xaml then the problem does not exist, but this is brittle at best.
Chris Taylor
Thanks Chris and mpistich, I did/do have the text boxes before the combobox in XAML. Chris' solution does the trick. Thanks!
Kory