Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Problem setting ComboBox SelectedIndex RSS

18 replies

Last post Jun 15, 2011 06:12 AM by totaldotnet

(0)
  • hehrsson

    hehrsson

    Member

    26 Points

    86 Posts

    Problem setting ComboBox SelectedIndex

    Jan 23, 2009 11:51 AM | LINK

    Hi.

    In the SelectionChanged event of a ComboBox, I display a MessageBox where the user has to confirm the change.
    If I press OK, some actions are performed and everything is fine.
    If I press Cancel, I reset the SelectedIndex back to what it was before the change and then exit the method.
    Everything looks fine, the value displayed is changed back to its original value. But, when I open the dropdown, the value I selected is now displayed there, and I can't select it again since it's alredy selected.

    Wow, that explanation was a bit hairy, lets take an example:

    The ComboBox has three values "Row1", "Row2" and "Row3".
    By default "Row1" is selected. I open the dropdown with the mouse and select "Row2", the MessageBox is displayed and I press Cancel.
    The value in the text field goes back to "Row1" but "Row2" is still selected in the dropdown. (I also need to set focus to another control, otherwise the dropdown is not closed).

    Another strange thing, if I don't open the dropdown with the mouse, instead I use Arrow Down button to select "Row2", the result is opposite, now "Row2" is displayed in the text box, but in the dropdown "Row1" is still seletced.

    Seems there is some mismatch between the text box and the dropdown list in the ComboBox control.

    I also tried setting the SelectedItem instead of SelectedIndex with no luck.

    Anyone else having the same problem or maybe a solution?

    Regards,
    Håkan

     

  • FuryDiamond

    FuryDiamond

    All-Star

    24033 Points

    4110 Posts

    Re: Problem setting ComboBox SelectedIndex

    Jan 23, 2009 02:58 PM | LINK

    Can you post the code?

    Please "Mark as Answer" if this post answered your question. :)

    Silverlight 5 3D Tutorials: http://silverlight.bayprince.com
    Blog: http://blog.bayprince.com
    Twitter: http://twitter.com/bayprince
  • silverlight_kid

    silverlight_kid

    Member

    455 Points

    105 Posts

    Re: Problem setting ComboBox SelectedIndex

    Jan 23, 2009 05:45 PM | LINK

    Hi,

    better you can post the code.

    In RC0 Version also combo comes with some bugs you visit this link find whether it helps you

    http://stackoverflow.com/questions/432688/how-to-avoid-an-open-silverlight-combobox-dropdown-to-be-on-top-of-everything

    http://silverlight.net/forums/t/38901.aspx

    http://silverlight.net/forums/t/30705.aspx

    If it is Solved your problem then mark as answer bcoz other with same question can benefit.....

    S.Siva Sankar
    Software Engineer
    Protechsoft
  • hehrsson

    hehrsson

    Member

    26 Points

    86 Posts

    Re: Problem setting ComboBox SelectedIndex

    Jan 23, 2009 07:38 PM | LINK

    The code is simple:

    private void BillingType_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      if (MessageBox.Show("This will regenerat the invoice rows, continue?", "Warning", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
      {
        BillingType.SelectedIndex = prevBillingType;
        InvoiceNumber.Focus();
        return;
      }
      InvoiceNumber.Focus();

      // Regenerate invoice rows
      SetDefaultInvoiceRows();

      prevBillingType = BillingType.SelectedIndex;
    }

    I did try to use UpdateLayout(), although I called it after setting SelectedIndex, not before as a mentioned solution. I will try is and return.

    - Håkan

  • hehrsson

    hehrsson

    Member

    26 Points

    86 Posts

    Re: Problem setting ComboBox SelectedIndex

    Jan 26, 2009 09:26 AM | LINK

    The UpdateLayout() does not work for me, no matter where I put it.

    - Håkan

  • FuryDiamond

    FuryDiamond

    All-Star

    24033 Points

    4110 Posts

    Re: Re: Problem setting ComboBox SelectedIndex

    Jan 28, 2009 03:24 PM | LINK

    Try this version out. I also noticed that there was a bug that caused the SelectionChanged event to be called multiple times because you are changing the Selection value.

        public partial class Page : UserControl
        {
            int prevBillingType = 0;

            public Page()
            {
                InitializeComponent();

                BillingType.SelectedIndex = 0;
                BillingType.SelectionChanged += new SelectionChangedEventHandler(BillingType_SelectionChanged);
            }

            private void BillingType_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (MessageBox.Show("This will regenerate the invoice rows, continue?", "Warning", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                {
                    BillingType.SelectionChanged -= new SelectionChangedEventHandler(BillingType_SelectionChanged);
                    BillingType.SelectedIndex = prevBillingType;
                    BillingType.SelectionChanged += new SelectionChangedEventHandler(BillingType_SelectionChanged);

                    BillingType.IsDropDownOpen = false;
                    return;
                }

                prevBillingType = BillingType.SelectedIndex;
                BillingType.IsDropDownOpen = false;
            }
        }

     

    Please "Mark as Answer" if this post answered your question. :)

    Silverlight 5 3D Tutorials: http://silverlight.bayprince.com
    Blog: http://blog.bayprince.com
    Twitter: http://twitter.com/bayprince
  • hehrsson

    hehrsson

    Member

    26 Points

    86 Posts

    Re: Re: Problem setting ComboBox SelectedIndex

    Jan 29, 2009 06:15 AM | LINK

    Sorry, it didn't help [:(]
    When I press Cancel it looks fine, but still, when I open the dropdown again, the second item in the listbox is still selected, it will not reset back to the previous selected item.

    The IsDropDownOpen property helped me a bit though, now I don't need to tab away from the ComboBox to make it close the dropdown, thanks!

    Håkan

     

  • meykih

    meykih

    Participant

    1049 Points

    260 Posts

    Re: Re: Problem setting ComboBox SelectedIndex

    Jan 29, 2009 07:03 AM | LINK

    The problem is that you try to change the selected index back while the SelectionChanged-Event for the first changing still is not worked through. If you find a possibility to first finish the standard selection change and react on the message box afterwards I guess it would work. Otherwise try to create your own dropdown with no standard event for selection changed and react on something like mouseleftbuttondown.
    Regards,
    Maike Ohlig

    Please mark post as answer if it helped you
  • FuryDiamond

    FuryDiamond

    All-Star

    24033 Points

    4110 Posts

    Re: Re: Re: Problem setting ComboBox SelectedIndex

    Jan 29, 2009 08:25 PM | LINK

    The IsDropDownOpen is definitely a great property to force the focus.

    I tested the scenario and the second item isn't selected if you push cancel. Are you disabling the event handler to ensure that its not re-calling it?

    Please "Mark as Answer" if this post answered your question. :)

    Silverlight 5 3D Tutorials: http://silverlight.bayprince.com
    Blog: http://blog.bayprince.com
    Twitter: http://twitter.com/bayprince
  • hehrsson

    hehrsson

    Member

    26 Points

    86 Posts

    Re: Re: Re: Problem setting ComboBox SelectedIndex

    Jan 30, 2009 06:31 AM | LINK

    Yes, I'm sure. I even tried a new clean project with only one page containing just a ComboBox, still the same.
    Run the project, select the second item, press cancel.
    The TextBox part of the ComboBox is fine (the first item is displayed), but when you open the dropdown again, the second item is selected.
    Or maybe I should say both the first and the second item are kind of selected. The first item have a blue border around it but a white background, while the second item has a blue background but no border. If I try to click on the first or second item, nothing happens, only the third item can be clicked. If I press cancel again, all three items are selected in the dropdown and none of the items can be selected again.

    If use the arrow buttons after I have canceled once and the drop down is closed, I get a System.ExecutionEngineException.

    My new page looks like this:

    <UserControl x:Class="SilverlightApplication1.Page"
    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
    Width="200" Height="30">
    <Grid x:Name="LayoutRoot" Background="White">
    <ComboBox x:Name="BillingType"/>
    </Grid>
    </
    UserControl>

    using System.Collections.Generic;
    using System.Windows;
    using System.Windows.Controls;

    namespace SilverlightApplication1
    {
        public partial class Page : UserControl
        {
            private List<string> list = new List<string>();
            int prevBillingType = 0;

            public Page()
            {
                InitializeComponent();

                list.Add("First");
                list.Add("Second");
                list.Add("Third");
                BillingType.ItemsSource = list;
                BillingType.SelectedIndex = 0;
                BillingType.SelectionChanged += new SelectionChangedEventHandler(BillingType_SelectionChanged);
            }

            private void BillingType_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (MessageBox.Show("This will regenerate the invoice rows, continue?", "Warning", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                {
                    BillingType.SelectionChanged -= new SelectionChangedEventHandler(BillingType_SelectionChanged);
                    BillingType.SelectedIndex = prevBillingType;
                    BillingType.SelectionChanged += new SelectionChangedEventHandler(BillingType_SelectionChanged);

                    BillingType.IsDropDownOpen = false;
                    return;
                }

                prevBillingType = BillingType.SelectedIndex;
                BillingType.IsDropDownOpen = false;
            }
        }
    }