CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2009
    Posts
    10

    Question Combo value not getting changed

    Hi All,

    My aim is not to fire selection changed event when combo value is changed through code.I have written following code:

    XAML
    =====
    <Window x:Class="TestCombo.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
    <Grid.RowDefinitions>
    <RowDefinition>
    </RowDefinition>
    <RowDefinition>
    </RowDefinition>
    </Grid.RowDefinitions>

    <ComboBox Width="100" IsTextSearchEnabled="False" VerticalAlignment="Center" IsEditable="True" HorizontalAlignment="Center" Name="cmbTest" SelectionChanged="cmbTest_SelectionChanged"/>
    <Button Grid.Row="1" Click="Button_Click" Content="Set Combo Value"/>
    </Grid>
    </Window>

    Code Behind
    =========


    Public Window1()
    {
    InitializeComponent();
    cmbTest.Items.Add("50");
    cmbTest.Items.Add("100");
    cmbTest.Items.Add("200")
    }

    private void cmbTest_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    MessageBox.Show("Selection Changed");
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
    cmbTest.Text = "25";
    }

    Problem Scenario
    ==============
    1) Run the application select 100 from combo drop down and you will see "Selection Changed" messagebox coming up.
    2) Now click the button "Set Combo Value" see combo text now changed to 25.
    3) Now again try to set combo value to "100" from drop down and notice interestingly nothing will happen and combo would still remain at value 25.

    How can i avoid this strange behavior?

    Plese suggest.

    Thanks,
    Anurodh
    Last edited by anurodhora; June 3rd, 2009 at 05:18 AM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Combo value not getting changed

    There are two parts to a combobox of this style - the edit box portion and the droplist portion.

    You are changing the edit box portion, but not the droplist portion. The SelectionChanged event fires when the SelectedIndex property changes (which is part of the droplist portion). This doesn't occur because, although you can the visible text, the SelectedIndex remains set to the index of the "100" value.

    So what you need to do is add the value to the Items list and then set the SelectedIndex to the added item.

    Code:
    private void Button_Click( object sender, RoutedEventArgs e )
    {
    	cmbTest.SelectedIndex = cmbTest.Items.Add( "25" );
    }

  3. #3
    Join Date
    Mar 2009
    Posts
    10

    Re: Combo value not getting changed

    Well, it will then move away me from my original aim i.e.
    "My aim is not to fire selection changed event when combo value is changed through code"

    What should i do in this scenario, i want both selection changed should not fire when combo text is set and at the same time it should honour selection changed i.e. should get to value the user chooses from the combo drop down.

    Is there any other way?

    Thanks,
    Anurodh

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Combo value not getting changed

    Quote Originally Posted by anurodhora View Post
    Well, it will then move away me from my original aim i.e.
    "My aim is not to fire selection changed event when combo value is changed through code"
    As I mentioned, if you want to enter the value (e.g. 25) into the combobox, you'll need to add it to the Items list (as I've shown in the code above).

    Unfortunately, you can't prevent the control from firing the SelectedChanged event, so the next best this is to use a guard variable.

    Code:
    private bool _ignoreSelectedChangeEvent = false;
    
    private void Button_Click( object sender, RoutedEventArgs e )
    {
    	_ignoreSelectedChangeEvent = true;
    	cmbTest.SelectedIndex = cmbTest.Items.Add( "25" );
    }
    
    private void cmbTest_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    	if( true == _ignoreSelectedChangeEvent )
    	{
    		_ignoreSelectedChangeEvent = false;
    		return;
    	}
    
    	MessageBox.Show("Selection Changed");
    }

  5. #5
    Join Date
    Mar 2009
    Posts
    10

    Re: Combo value not getting changed

    Thanks for the reply but i always want my combo to have values - 50,100 & 200. Which as per your suggestion showing me 25 also in the drop down.

    Is there any way say to reset selected index or so.

  6. #6
    Join Date
    Mar 2009
    Posts
    10

    Re: Combo value not getting changed

    OK i got it working now.

    Here is the final solution.

    private bool _ignoreSelectedChangeEvent = false;

    private void Button_Click( object sender, RoutedEventArgs e )
    {
    if (!cmbTest.Items.Contains("25"))
    {
    _ignoreSelectedChangeEvent = true;
    cmbTest.SelectedIndex = -1;
    cmbTest.Text = "25";
    }
    }

    private void cmbTest_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    if( true == _ignoreSelectedChangeEvent )
    {
    _ignoreSelectedChangeEvent = false;
    return;
    }

    MessageBox.Show("Selection Changed");
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured