Create A Project and Add A Combo Box To It.
Just Make Sure You Have Change The Following Properties In The Combo Box Properties.
I Have Add Following Sample Data To My Combo Box.
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ComboBoxSuggest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("Apple");
comboBox1.Items.Add("Ball");
comboBox1.Items.Add("Cat");
comboBox1.Items.Add("Dog");
comboBox1.Items.Add("Elephant");
comboBox1.Items.Add("Fish");
comboBox1.Items.Add("Goat");
comboBox1.Items.Add("Animal");
comboBox1.Items.Add("Amazon");
comboBox1.Items.Add("Avatar");
}
}
}
Now Type And See,You Will Get Suggestions
If You Click The Drop down You Can See Everything...
If You Want To Add Dynamic Data To Combo Box You Can Do It Inside The Form Load Method.Following Example Load Data To Combo Box From SQL Server Database.
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ComboBoxSuggest
{
public partial class MyForm : Form
{
public Form1()
{
InitializeComponent();
}
private void MyForm_Load(object sender, EventArgs e)
{
loadMyComboBox();
}
private void loadMyComboBox()
{
string query = "SELECT EmpName,Age FROM Employee";
DBConnect dbconnection = new DBConnect();
DataTable datatable = new DataTable();
dbconnection.connect();
datatable = dbconnection.ReadData(query);
dbconnection.connect();
datatable = dbconnection.ReadData(query);
MyComboBox.Items.Clear();//Clearing Existing Items
foreach (DataRow row in dt.Rows)
{
MyComboBox.ItemsAdd(row[0].ToString());
{
MyComboBox.ItemsAdd(row[0].ToString());
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace ComboBoxSuggest
{
class DBConnect
{
string strconnection= "Server=localhost;Database=dbname;Trusted_Connection=True;";
SqlConnection sqlcon = new SqlConnection();
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
public void connect()
{
sqlcon = new SqlConnection(strconnection);
sqlcon.Open();
}
public void disconnect()
{
if (sqlcon.State == ConnectionState.Open)
{
sqlcon.Close();
sqlcon.Dispose();
}
}
public DataTable ReadData(string query)
{
try
{
connect();
sqlcmd = new SqlCommand(query, sqlcon);
da = new SqlDataAdapter(sqlcmd);
dt = new DataTable();
da.Fill(dt);
}
catch (Exception ex)
{
throw ex;
}
finally
{
disconnect();
}
return dt;
}
public void QryCommand(string query)
{
try
{
connect();
sqlcmd = new SqlCommand(query, sqlcon);
sqlcmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
disconnect();
}
}
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace ComboBoxSuggest
{
class DBConnect
{
string strconnection= "Server=localhost;Database=dbname;Trusted_Connection=True;";
SqlConnection sqlcon = new SqlConnection();
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
public void connect()
{
sqlcon = new SqlConnection(strconnection);
sqlcon.Open();
}
public void disconnect()
{
if (sqlcon.State == ConnectionState.Open)
{
sqlcon.Close();
sqlcon.Dispose();
}
}
public DataTable ReadData(string query)
{
try
{
connect();
sqlcmd = new SqlCommand(query, sqlcon);
da = new SqlDataAdapter(sqlcmd);
dt = new DataTable();
da.Fill(dt);
}
catch (Exception ex)
{
throw ex;
}
finally
{
disconnect();
}
return dt;
}
public void QryCommand(string query)
{
try
{
connect();
sqlcmd = new SqlCommand(query, sqlcon);
sqlcmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
disconnect();
}
}
}
}
9 comments:
if you first click on the combobox's dropdown icon and then type..you will see a list overlapping the previous drop down contents..any suggestions?
reply guys
i am sorry your question is not clear enough ! could you please tell again
if I first click on the combobox dropdown arrow a list of contents appear(say 1 to 25)..now if I type 2..I can see a separate list (of 20 to 25) which overlaps the original contents..however, I will be able to see both the previous combobox contents and the new list of contents overlapping..but the control remains in the original list..I can't select from the new list...
I have an ComboBox and a large number of records to show in it. When user starts typing I want to load records from the database that starts with input text and offer the user for autocomplete. I am using MSAccess as backend for the records.How can this be achieved?
In This Combo Box if you click combobox dropdown arrow you will see the all the items in the combo box.
If you type anything you will get the append result for your typing...
however if you click the combo box drop down arrow and then if you type anything you will have the said situation.
because both the situation working together.
If you only want to get the append list you have to do it without clicking drop down arrow.
hope this solve your problem....
For You I Guess Best Option Is To Use A List Box Alone With A Text Box.
When You Type Anything In The Text Box You Can Load Records To List Box.Then You Can Select It from List Box.
First Of All To Do This Add A Text Field and A List Box To Your Form.Then Get The Key Down Event For Text Field.In The Event Body You Can Write Your Code To Get The Details And Add To List Box.Ill Try to Put A Complete Post Regarding This By Tomorrow Night.
check this...C# Autocomplete combobox
Ling
Post a Comment