Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

How to Autocomplete TextBox



From the version Visual Studio 2005, some of the controls support Autocomplete feature including the TextBox controls. The properties like AutoCompleteCustomSource, AutoCompleteMode and AutoCompleteSource to perform a TextBox that automatically completes user input strings by comparing the prefix letters being entered to the prefixes of all strings in a data source.

AutoCompleteCustomSource is helpful for TextBox controls in which string sources like URLs, addresses etc. will be frequently entered.

AutoCompleteCustomSource property is perform to set the custom list of values.

AutoCompleteMode property is perform to set how the auto-complete candidates are displayed.

C# Autocomplete TextBox

The following C# program add some data values to AutoCompleteStringCollection and perform as an Autocomplete TextBox

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");
        }
    }
}

Autocomplete TextBox working with Database values C#

The following C# program connect to the database and add Dataset values to AutoCompleteStringCollection and perform as an Autocomplete TextBox

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 ! ");
            }
        }
    }
}

VB.Net Autocomplete TextBox

The following VB.Net program add some string values to AutoCompleteStringCollection and display as Autocomplete TextBox while entering text

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

Autocomplete TextBox working with Database values VB.Net

The following VB.Net program connects to the database and add Dataset values to AutoCompleteStringCollection and display as an Autocomplete TextBox while entering values to TextBox control.

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

TextBox

A TextBox control is used to enter text in an application. It has additional capabilities that are not found in the standard Windows text box control, including multiline editing and password character masking. More about… C# TextBox 

Create a Numeric Text Box

In many situations, you need to enter only numeric values in the Textbox. Here you can see some useful techniques that accept only numbers in the Textbox. More about


Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More