Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How to AutoComplete TextBox or ComboBox in Winforms
WhatsApp
Pintoo Yadav
Jan 28
2015
1.4
k
0
0
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
Up Next
How to AutoComplete TextBox or ComboBox in Winforms