TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to AutoComplete TextBox or ComboBox in Winforms
Pintoo Yadav
Jan 28
2015
Code
1.3
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
namespace
AutoCompleteTextBox
{
public
partial
class
frmAuto : Form
{
public
string
strConnection = ConfigurationManager.AppSettings[
"ConnString"
];
AutoCompleteStringCollection namesCollection =
new
AutoCompleteStringCollection();
public
frmAuto()
{
InitializeComponent();
}
private
void
frmAuto_Load(
object
sender, EventArgs e)
{
SqlDataReader dReader;
SqlConnection conn =
new
SqlConnection();
conn.ConnectionString = strConnection;
SqlCommand cmd =
new
SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText =
"Select distinct [Name] from [Names] order by [Name] asc"
;
conn.Open();
dReader = cmd.ExecuteReader();
if
(dReader.HasRows ==
true
)
{
while
(dReader.Read())
namesCollection.Add(dReader[
"Name"
].ToString());
}
else
{
MessageBox.Show(
"Data not found"
);
}
dReader.Close();
txtName.AutoCompleteMode = AutoCompleteMode.Suggest;
txtName.AutoCompleteSource = AutoCompleteSource.CustomSource;
txtName.AutoCompleteCustomSource = namesCollection;
}
private
void
btnCancel_Click(
object
sender, EventArgs e)
{
Application.Exit();
}
private
void
btnOk_Click(
object
sender, EventArgs e)
{
MessageBox.Show(
"Hope you like this example"
);
}
}
}
.Net 4.5
Framework 4.5
Winforms