Link to home
Start Free TrialLog in
Avatar of Steve Williams
Steve WilliamsFlag for United States of America

asked on

How do I get the up & down arrow keys to navigate in an Autocomplete textbox list.

I have an issue with the following code:

using System;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using static System.Math;

namespace CalculationTools
{
    public partial class frmNeedleCalc : Form
    {        
        public frmNeedleCalc()
        {
            InitializeComponent();
            txtAssmNum.TextChanged += txtAssmNum_TextChanged;
            Text = "Calculation Tools : Needle Calc " + Application.ProductVersion;
            txtAssmNum.Text = "Type Assembly No. Here";
        }

        private void frmNeedleCalc_Load(object sender, EventArgs e)
        {
            Show();
            BringToFront();
            AssmNumACList();
            this.ActiveControl = txtAssmNum;
        }

        private void txtAssmNum_TextChanged(object sender, EventArgs e)
        {
            AssmNumACList();
        }

        private void AssmNumACList()
        {
            string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
            SqlConnection con = new SqlConnection(ConString);


            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT [ANum] FROM [CalcTools].[dbo].[NeeBearCalcs]";
            cmd.Parameters.AddWithValue("@ANum", txtAssmNum.Text);
            cmd.Connection = con;

            con.Open();

            SqlDataReader reader;
            reader = cmd.ExecuteReader();
            AutoCompleteStringCollection MyCollection = new AutoCompleteStringCollection();
            while (reader.Read())
            {   
                MyCollection.Add(reader.GetString(0));
            }

            txtAssmNum.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtAssmNum.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtAssmNum.AutoCompleteCustomSource = MyCollection;
            con.Close();
        }
    }
}

Open in new window


I created a form that has an autocomplete textbox. The textbox gets a collections list from an SQL table. the list populates fine and changes as I change the text. The issue I'm having is the end users would like the ability to use the up/down arrows to move thru the list to the selected assembly number and then hit enter to select part number once they have navigated to the assembly number they are looking for.

Currently, the only way to select the assembly number from the list is to use a mouse or finish typing in the complete number. If I try the Tab key it just selects the top assembly number from the list and then goes to the next control instead of tabbing to the list.

I have googled this question for a while but have found no good answers yet. So here I 'am. Does anyone have a solid fix for this? I'm still new to C#, so any help is greatly appreciated.
ASKER CERTIFIED SOLUTION
Avatar of Don VonderBurg
Don VonderBurg
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Steve Williams

ASKER

@dvonderberg : Thanks that was what was wrong. VB requires the textbox.changed event in order for it to work properly. Guess I have a long way to go before I truly get how C# works. I also had to add this line of code just under the InitializeComponents(); for the load event to fire properly as well.

this.Load += new EventHandler(frmNeedleCalc_Load);

Open in new window

Awarding points to dvonderburg for solving my issue. Your time is greatly appreciated