Windows Forms: How to make a TextBox AutoComplete in C#
By FoxLearn 7/18/2017 9:35:50 PM 7.69K
AutoComplete TextBox in C# using sql database
Step 1: Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "AutoCompleteTextBox" and then click OK
Step 2: Design your form as below
We use the Northwind database to play demo. If you haven't got Northwind database, you can view How to download and restore Northwind database in SQL Server
Add an EF model to your project, then select Product table
Step 3: Add code to handle your form as below
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace AutoCompleteTextBox { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Retrieve data from product table, then add to textbox using (NorthwindEntities db = new NorthwindEntities()) { productBindingSource.DataSource = db.Products.ToList(); AutoCompleteStringCollection ac = new AutoCompleteStringCollection(); foreach (Product p in productBindingSource.DataSource as List<Product>) ac.Add(p.ProductName); txtProductName.AutoCompleteCustomSource = ac; } } private void txtProductName_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { //Search data from textbox using (NorthwindEntities db = new NorthwindEntities()) { productBindingSource.DataSource = db.Products.Where(p => p.ProductName.Contains(txtProductName.Text)).ToList(); } } } } }
VIDEO TUTORIALS
- Windows Forms: Text to speech in C#
- Windows Forms: Print Picture Box in C#
- Windows Forms: Advanced Filter DataGridView in C#
- Windows Forms: Progress Bar in C#
- How to create a Lucky Draw games in C#
- Windows Forms: Youtube Search with Paging in C#
- Windows Forms: How to Create a Random Password Generator in C#
- Windows Forms: Tag Control in C#
Categories
Popular Posts
Visual Studio: Auto increment version
04/01/2024
What is ARM architecture?
04/01/2024