user3527883
user3527883

Reputation: 45

Is there a way to make a drop down menu like this?

Is there anyone that know how to make a drop down menu like this?

I would put it in this if it was me:

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    //in here
}

Upvotes: 0

Views: 103

Answers (2)

Agaspher
Agaspher

Reputation: 484

Looks like you need AutoCompleteBox from WPFToolkit. You can set it up from NuGet with following command:

PM> Install-Package WPFToolkit

Here is the code snippet for using this control:

XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <toolkit:AutoCompleteBox x:Name="InputBox" Margin="0,77,0,159"/>
</Grid>

C#:

using System.Collections.Generic;
namespace WpfApplication1
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            InputBox.ItemsSource = new List<string>
            { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" };
        }
    }
}

Upvotes: 0

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Yes , You can use ListBox control.

OR

You can use ComboBox Control by setting the DropDownStyle property to Simple.

EDIT:

If you want to search for a String from ListBox and Select the Item if it is matching with it

You need to have a TextBox to receive the Serach String as input.

enter image description here

You need to handle the TextBox Key_Down Event handler to start searching.

Note: In below code i have started searching when user enters ENTER key after entering the input search string.

Try This:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            var itemSearched = textBox1.Text;
            int itemCount = 0;
            foreach (var item in listBox1.Items)
            {
                if (item.ToString().IndexOf(itemSearched,StringComparison.InvariantCultureIgnoreCase)!=-1)
                {
                    listBox1.SelectedIndex = itemCount;
                    break;
                }

                itemCount++;
            }
        }
    }

Upvotes: 1

Related Questions