Jumping to specific record in a Silverlight ComboBox

I had a ComboBox that contained a list of U.S. states. I wanted this ComboBox to have functionality that would enable a user to enter a letter or two and have the correct item highlighted. This simple change was very easy to make.

First, a bit of XAML to define my ComboBox:

<ComboBox Name=”State” KeyDown=”State_KeyDown” Height=”25″ Grid.Column=”1″ Grid.Row=”2″ />

Now for the KeyDown event.

        private void State_KeyDown(object sender, KeyEventArgs e)
        {
            foreach (string Item in State.Items)
            {
                if (Item.StartsWith(e.Key.ToString()))
                {
                    State.SelectedItem = Item;
                    break;  // Find after the first match
                }
            }
        }

If you are just interested highlighting the element based on the first character, this little snippet will work:

        string DDLValue = string.Empty;

        private void State_KeyDown(object sender, KeyEventArgs e)
        {
            DDLValue += e.Key.ToString();

            foreach (string Item in State.Items)
            {
                if (Item.StartsWith(DDLValue))
                {
                    State.SelectedItem = Item;
                    break;  // Find after the first match
                }
            }
        }

The string can be cleared out at another time. I did it when the Close button is clicked on the parent window.

The above works, but it will not work after the first time the text is entered – the value is persisted. To clear the string of previous values, so the user could type in a new state the next time they entered the window, or even if they tabbed off the ComboBox, I added a LostFocus event to the State ComboBox definition, and added this in the code behind:

private void FollowSearchState_LostFocus(object sender, RoutedEventArgs e)
{
            DDLValue = string.Empty;
}


So, the full working code is this:

XAML:

<ComboBox Name="State" KeyDown="State_KeyDown" LostFocus="State_LostFocus" />

C#:

private void FollowSearchState_KeyDown(object sender, KeyEventArgs e)
{
            if (DDLValue.Length <= 2)
                DDLValue += e.Key.ToString();
            foreach (string Item in FollowSearchState.Items)
            {
                if (Item.StartsWith(DDLValue))
                {
                    FollowSearchState.SelectedItem = Item;
                    break;  // Find after the first match
                }
            }
            FollowSearchState.Focus();
}
private void FollowSearchState_LostFocus(object sender, RoutedEventArgs e)
{
            DDLValue = string.Empty;
}
Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: