- [C#] Hướng dẫn giải nén file *.rar với tiến trình progress bar winform
- [C#] Chia sẻ source code make Crazy Error Message Effect Bomb Windows
- [C#] Lập trình ứng dụng theo mô hình MVP Model-View-Presenter Pattern Winform
- [C#] Giới thiệu và những thứ hay ho từ thư viện System.Reactive của Microsoft
- [C#] Hướng dẫn tạo ứng dụng Chat với GPT sử dụng Open AI API
- [DEVEXPRESS] Tạo month picker trên DateEdit Winform C#
- [DATABASE] Cách sử dụng và lưu ý khi sử dụng khóa ngoại (Foreign Key) trong Sqlserver
- [C#] Garbage Collector (GC) là gì? Cách giải phóng bộ nhớ trên ứng dụng Winform khi các đối tượng không còn sử dụng
- [C#] Cách tính độ tương phản màu sắc Contrast Color mà con người có thể nhìn thấy được
- [C#] Hướng dẫn mã hóa mật khẩu tài khoản ứng dụng đúng chuẩn Men
- [C#] Sử dụng Open AI Chat GPT viết ứng dụng Count down timer có hiệu ứng trên winform
- [DATABASE] Chia sẻ dữ liệu Pantone Color sql và json api
- [SQLSERVER] Tạo mã sản phẩm tự động như: SP0001, SP0002, SP0003... sử dụng Trigger
- [C#] Hướng dẫn kiểm tra phiên bản NET Framework cài đặt ở máy tính
- [C#] Hướng dẫn đọc file excel đơn giản sử dụng thư viện Epplus
- [C#] ConcurrentBag là gì và cách sử dụng nó trong lập trình bất đồng bộ
- [C#] AutoResetEvent là gì và cách sử dụng
- [DEVEXPRESS] Chia sẻ source code cách tạo biểu đồ sơ đồ tổ chức công ty Org Chart trên Winform C#
- [C#] Hướng dẫn tạo Auto Number trên Datagridview winform
- [DATABASE] Hướng dẫn tạo Procedure String Split in Mysql
[VB.NET] Autocomplete Textbox load dữ liệu từ database
Bài viết này, mình hướng dẫn các bạn cách sử dụng Autocomplete trong Textbox của lập trình VB.NET. Với việc sử dụng Autocomplete đề nghị nội dung cho chúng ta nhập vào chính xác hơn.
Trong bài này, mình sẽ hướng dẫn các bạn code bằng 2 ngôn ngữ VB.NET và C#.
Vậy Autocomplete là gì?
Autocomplete là giúp chúng ta tìm dữ liệu nhập vào theo nội dung chúng ta mong muốn, các bạn có thể thấy ứng dụng của nó khi các bạn tìm kiếm từ khóa trên google. Google sử dụng Autocomplete cho input Textbox suggest từ khóa của mình.
Autocomplete TextBox working with Database values C#
Các bạn nhìn thấy hình ảnh demo ứng dụng dưới đây:
+ Đầu tiê mình sẽ hướng dẫn các bạn sử dụng nó bằng dữ liệu tự nhập vào:
Code Autocomple Textbox VB.NET
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest
TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
Dim DataCollection As New AutoCompleteStringCollection()
addItems(DataCollection)
TextBox1.AutoCompleteCustomSource = DataCollection
End Sub
Public Sub addItems(ByVal col As AutoCompleteStringCollection)
col.Add("Abel")
col.Add("Bing")
col.Add("Catherine")
col.Add("Varghese")
col.Add("John")
col.Add("Kerry")
End Sub
End Class
Và đây là Source code bằng ngôn ngữ Csharp.
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
addItems(DataCollection);
textBox1.AutoCompleteCustomSource = DataCollection;
}
public void addItems(AutoCompleteStringCollection col)
{
col.Add("Abel");
col.Add("Bing");
col.Add("Catherine");
col.Add("Varghese");
col.Add("John");
col.Add("Kerry");
}
}
}
+ Tiếp tục đoạn code dưới này, mình sẽ load dữ liệu từ database vào Autocomplete TextBox C# và VB.NET
Source VB.NET
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest
TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
Dim DataCollection As New AutoCompleteStringCollection()
getData(DataCollection)
TextBox1.AutoCompleteCustomSource = DataCollection
End Sub
Private Sub getData(ByVal dataCollection As AutoCompleteStringCollection)
Dim connetionString As String = Nothing
Dim connection As SqlConnection
Dim command As SqlCommand
Dim adapter As New SqlDataAdapter()
Dim ds As New DataSet()
connetionString = "Data Source=.;Initial Catalog=pubs;User ID=sa;password=zen412"
Dim sql As String = "SELECT DISTINCT [fname] FROM [employee]"
connection = New SqlConnection(connetionString)
Try
connection.Open()
command = New SqlCommand(sql, connection)
adapter.SelectCommand = command
adapter.Fill(ds)
adapter.Dispose()
command.Dispose()
connection.Close()
For Each row As DataRow In ds.Tables(0).Rows
dataCollection.Add(row(0).ToString())
Next
Catch ex As Exception
MessageBox.Show("Can not open connection ! ")
End Try
End Sub
End Class
Source code C#
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
getData(DataCollection);
textBox1.AutoCompleteCustomSource = DataCollection;
}
private void getData(AutoCompleteStringCollection dataCollection)
{
string connetionString = null;
SqlConnection connection ;
SqlCommand command ;
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
connetionString = "Data Source=.;Initial Catalog=pubs;User ID=sa;password=zen412";
string sql = "SELECT DISTINCT [fname] FROM [employee]";
connection = new SqlConnection(connetionString);
try
{
connection.Open();
command = new SqlCommand(sql, connection);
adapter.SelectCommand = command;
adapter.Fill(ds);
adapter.Dispose();
command.Dispose();
connection.Close();
foreach (DataRow row in ds.Tables[0].Rows)
{
dataCollection.Add(row[0].ToString());
}
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
}
}
}
CHÚC CÁC BẠN THÀNH CÔNG! HY VỌNG BÀI VIẾT NÀY SẼ GIÚP ÍCH ĐƯỢC CHO CÁC BẠN.