Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7606055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:18:56+00:00 2026-05-31T00:18:56+00:00

I am trying replicate auto-complete functionality that you find on the Google homepage. So,

  • 0

I am trying replicate auto-complete functionality that you find on the Google homepage. So, when you start typing in the text box you get a drop down list of suggestions and using the up or down arrow key you can cycle through this list. I am pretty much there with this functionality. The code that I have listed below demonstrates the problem.

When you start typing for the first time and a list of suggestions is displayed, pressing the up arrow key while the text box still has focus should cause the last item in list of suggestions to be focused and selected.

In my example below, the second to last item is selected and I cant’ seem to fathom why. I have hard coded the suggestions in my example so there is no need to type. Just pressing the up arrow key should demonstrate the issue.

<Window x:Class="MyTestApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="350"
        Width="525"
        Loaded="Window_Loaded">
    <StackPanel>
        <TextBox x:Name="nameTextBox"
                 Text="hello world!"
                 PreviewKeyDown="nameTextBox_PreviewKeyDown" />
        <ListBox x:Name="suggestionListBox"
                 DisplayMemberPath="Name"
                 SelectionChanged="suggestionListBox_SelectionChanged"
                 PreviewKeyDown="suggestionListBox_PreviewKeyDown" />
        <Label x:Name="output" />
    </StackPanel>
</Window>


using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace MyTestApplication
{
    public partial class MainWindow : Window
    {
        private const int NOT_SELECTED = -1;

        public MainWindow()
        {
            InitializeComponent();

            suggestionListBox.ItemsSource = new[] 
            {
                new { Name = "aaaaaa"},
                new { Name = "bbbbbbbb"},
                new { Name = "cccc"},
                new { Name = "ddddd"},
                new { Name = "eeeeee"},
                new { Name = "fffffffff"}
            };

            output.Content = suggestionListBox.SelectedIndex.ToString();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            nameTextBox.Focus();
        }

        private void nameTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Down)
            {
                suggestionListBox.Focus();
            }

            if (e.Key == Key.Up)
            {
                var listBoxItem = (ListBoxItem)suggestionListBox
                        .ItemContainerGenerator
                        .ContainerFromIndex(suggestionListBox.Items.Count - 1);

                var result = listBoxItem.Focus();
            }
        }

        private void suggestionListBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (    (e.Key == Key.Up && suggestionListBox.SelectedIndex == 0)
                ||  (e.Key == Key.Down && suggestionListBox.SelectedIndex == suggestionListBox.Items.Count - 1))
            {
                suggestionListBox.SelectedIndex = NOT_SELECTED;
                nameTextBox.Focus();
            }
        }

        private void suggestionListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            output.Content = suggestionListBox.SelectedIndex.ToString();
        }
    }
}

I have tried using selected index on the list box but when I call the focus method on the list box the selected index is set to zero. This is why I locate the last list box item in the list of suggestions and apply focus to that, but it highlights the one above. Hope this makes sense. To demonstrate the problem just create a new WPF project and copy and paste the above code in the MainWindow.xaml and MainWindow.Xaml.cs and run the application. All shall be revealed.

Any help or indication would be greatly received.

Best of regards

Mohammad.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:18:57+00:00Added an answer on May 31, 2026 at 12:18 am

    PreviewKeyDown event is a tunneling event i.e. it tunnels down from parent control to child control and in case you don’t want it to tunnel down further, you have to handled the event by setting its handled property of args to true like this e.Handled = true.

    So, this should work for you –

    private void nameTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
    {
       if (e.Key == Key.Down)
       {
          suggestionListBox.Focus();
       }
    
       if (e.Key == Key.Up)
       {
          var listBoxItem = (ListBoxItem)suggestionListBox
                            .ItemContainerGenerator
                            .ContainerFromIndex(suggestionListBox.Items.Count - 1);
    
          suggestionListBox.SelectedIndex = suggestionListBox.Items.Count - 1;
          listBoxItem.Focus();
          e.Handled = true;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to replicate the text overlay effect that occurs when you mouse
I'm trying to replicate this: http://webdesignerwall.com/tutorials/css-gradient-text-effect however I can't seem to get it to
I am trying to replicate TryParse for generic types and thought that TypeDescriptor might
Has anyone successfully done this? Trying to replicate functionality similar to what is found
I am trying to replicate the feature in voice memos app that uses a
I am trying to replicate the animated drop down menu here but I am
So I am trying to replicate Facebook's picture tagging functionality, and I have the
I'm trying to replicate Excel's Vertical Text feature in HTML and wondering if anyone
I am trying to replicate the exact functionality of this dialogue in Visual Studio
Basically I was trying to replicate one of the things that xajax gave me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.