Steve Williams
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:
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.
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();
}
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awarding points to dvonderburg for solving my issue. Your time is greatly appreciated
ASKER
Open in new window