C# - Simple Auto Complete List With SQLite
Submitted by razormist on Wednesday, April 25, 2018 - 22:02.
In this tutorial we will create a Simple Auto Complete List With SQLite using C#. C# is a general-purpose, object-oriented programming language. C# automatically manages inaccessible object memory using a garbage collector, which eliminates developer concerns and memory leaks. C# is faster than dynamically typed languages because things are more clearly defined. It contains several classes that support any C# platforms, like game development. It has a friendly environment for all new developers. So let's do the coding...
or also you create the layout by dragging the proper tools to the forms.
To make a Auto Complete TextBox, just click the textbox then go to the properties and change the settings of the autocomplete as shown below.
After that select the ... in the AutoCompleteCustomSource then put all the needed string here,
Try to run the application and see if it works.
There you go we successfully created a Simple Auto Complete List With SQLite using C#. I hope that this tutorial help you understand on how to develop an application using C#. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!
Getting Started
First you will have to download & install the Visual Studio. Visual Studios is an open source development feel free to create any application that you want. Here's the link for the Visual Studio https://www.visualstudio.com/. Here's the link for the SQLite Browser http://sqlitebrowser.org/.Setting up SQLite
SQLite is very carefully tested prior to every release and relevant to use in some way. SQLite is very usable in any environments especially in embedded devices. First all you need to do is to install the components of the SQLIte database, by right clicking in the Main project title in the solution explorer then selecting the Manage NuGet Packages. Then go to the browse and search sqlite, after that install it and wait until the process is completed. Next go to the Updates and update the needed framework to make sqlite work properly. Note: Update only the framework if there is an available new update.Application Design
We will now create the design for the application, first locate the designer file called form1.Designer.cs, this is the default name when you create a new windows form. Rename the form as Main.cs and then write these codes inside your designer file.- namespace Simple_AutoComplete_Text
- {
- partial class Main
- {
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.groupBox1.SuspendLayout();
- this.SuspendLayout();
- //
- // btn_submit
- //
- this.btn_submit.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.btn_submit.Name = "btn_submit";
- this.btn_submit.TabIndex = 0;
- this.btn_submit.Text = "Submit";
- this.btn_submit.UseVisualStyleBackColor = true;
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.label1.Name = "label1";
- this.label1.TabIndex = 1;
- this.label1.Text = "NAME:";
- //
- // groupBox1
- //
- this.groupBox1.Controls.Add(this.txt_country);
- this.groupBox1.Controls.Add(this.label2);
- this.groupBox1.Controls.Add(this.txt_name);
- this.groupBox1.Controls.Add(this.label1);
- this.groupBox1.Controls.Add(this.btn_submit);
- this.groupBox1.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.TabIndex = 2;
- this.groupBox1.TabStop = false;
- this.groupBox1.Text = "Member Information";
- //
- // txt_country
- //
- "Afghanistan",
- "Albania",
- "Algeria",
- "Andorra",
- "Angola",
- "Anguilla",
- "Antigua & Barbuda",
- "Argentina",
- "Armenia",
- "Australia",
- "Austria",
- "Azerbaijan",
- "Bahamas",
- "Bahrain",
- "Bangladesh",
- "Barbados",
- "Belarus",
- "Belgium",
- "Belize",
- "Benin",
- "Bermuda",
- "Bhutan",
- "Bolivia",
- "Bosnia & Herzegovina",
- "Botswana",
- "Brazil",
- "Brunei Darussalam",
- "Bulgaria",
- "Burkina Faso",
- "Myanmar/Burma",
- "Burundi",
- "Cambodia",
- "Cameroon",
- "Canada",
- "Cape Verde",
- "Cayman Islands",
- "Central African Republic",
- "Chad",
- "Chile",
- "China",
- "Colombia",
- "Comoros",
- "Congo",
- "Costa Rica",
- "Croatia",
- "Cuba",
- "Cyprus",
- "Czech Republic",
- "Democratic Republic of the Congo",
- "Denmark",
- "Djibouti",
- "Dominica",
- "Dominican Republic",
- "Ecuador",
- "Egypt",
- "El Salvador",
- "Equatorial Guinea",
- "Eritrea",
- "Estonia",
- "Ethiopia",
- "Fiji",
- "Finland",
- "France",
- "French Guiana",
- "Gabon",
- "Gambia",
- "Georgia",
- "Germany",
- "Ghana",
- "Great Britain",
- "Greece",
- "Grenada",
- "Guadeloupe",
- "Guatemala",
- "Guinea",
- "Guinea-Bissau",
- "Guyana",
- "Haiti",
- "Honduras",
- "Hungary",
- "Iceland",
- "India",
- "Indonesia",
- "Iran",
- "Iraq",
- "Israel and the Occupied Territories",
- "Italy",
- "Ivory Coast (Cote d\'Ivoire)",
- "Jamaica",
- "Japan",
- "Jordan",
- "Kazakhstan",
- "Kenya",
- "Kosovo",
- "Kuwait",
- "Kyrgyz Republic (Kyrgyzstan)",
- "Laos",
- "Latvia",
- "Lebanon",
- "Lesotho",
- "Liberia",
- "Libya",
- "Liechtenstein",
- "Lithuania",
- "Luxembourg",
- "Republic of Macedonia",
- "Madagascar",
- "Malawi",
- "Malaysia",
- "Maldives",
- "Mali",
- "Malta",
- "Martinique",
- "Mauritania",
- "Mauritius",
- "Mayotte",
- "Mexico",
- "Moldova, Republic of",
- "Monaco",
- "Mongolia",
- "Montenegro",
- "Montserrat",
- "Morocco",
- "Mozambique",
- "Namibia",
- "Nepal",
- "Netherlands",
- "New Zealand",
- "Nicaragua",
- "Niger",
- "Nigeria",
- "Korea, Democratic Republic of (North Korea)",
- "Norway",
- "Oman",
- "Pacific Islands",
- "Pakistan",
- "Panama",
- "Papua New Guinea",
- "Paraguay",
- "Peru",
- "Philippines",
- "Poland",
- "Portugal",
- "Puerto Rico",
- "Qatar",
- "Reunion",
- "Romania",
- "Russian Federation",
- "Rwanda",
- "Saint Kitts and Nevis",
- "Saint Lucia",
- "Saint Vincent\'s & Grenadines",
- "Samoa",
- "Sao Tome and Principe",
- "Saudi Arabia",
- "Senegal",
- "Serbia",
- "Seychelles",
- "Sierra Leone",
- "Singapore",
- "Slovak Republic (Slovakia)",
- "Slovenia",
- "Solomon Islands",
- "Somalia",
- "South Africa",
- "Korea, Republic of (South Korea)",
- "South Sudan",
- "Spain",
- "Sri Lanka",
- "Sudan",
- "Suriname",
- "Swaziland",
- "Sweden",
- "Switzerland",
- "Syria",
- "Tajikistan",
- "Tanzania",
- "Thailand",
- "Timor Leste",
- "Togo",
- "Trinidad & Tobago",
- "Tunisia",
- "Turkey",
- "Turkmenistan",
- "Turks & Caicos Islands",
- "Uganda",
- "Ukraine",
- "United Arab Emirates",
- "United States of America (USA)",
- "Uruguay",
- "Uzbekistan",
- "Venezuela",
- "Vietnam",
- "Virgin Islands (UK)",
- "Virgin Islands (US)",
- "Yemen",
- "Zambia",
- "Zimbabwe"});
- this.txt_country.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
- this.txt_country.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
- this.txt_country.Name = "txt_country";
- this.txt_country.TabIndex = 4;
- //
- // label2
- //
- this.label2.AutoSize = true;
- this.label2.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.label2.Name = "label2";
- this.label2.TabIndex = 3;
- this.label2.Text = "COUNTRY:";
- //
- // txt_name
- //
- this.txt_name.Name = "txt_name";
- this.txt_name.TabIndex = 2;
- //
- // Main
- //
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.Controls.Add(this.groupBox1);
- this.Name = "Main";
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
- this.Text = "Main";
- this.groupBox1.ResumeLayout(false);
- this.groupBox1.PerformLayout();
- this.ResumeLayout(false);
- }
- #endregion
- private System.Windows.Forms.Button btn_submit;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.GroupBox groupBox1;
- private System.Windows.Forms.TextBox txt_country;
- private System.Windows.Forms.Label label2;
- private System.Windows.Forms.TextBox txt_name;
- }
- }
Creating the Script
We will now create the script to make things work. To do that go to the csharp script called Main.cs then right click and select view code, this will force you to go to the text editor. Then write these block of codes inside the Class of the form.- 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;
- using System.Data.SQLite;
- using System.IO;
- namespace Simple_AutoComplete_Text
- {
- public partial class Main : Form
- {
- SQLiteConnection conn;
- SQLiteCommand cmd;
- String connectString;
- public Main()
- {
- InitializeComponent();
- connectString = @"Data Source=" + Application.StartupPath + @"\data.db;version=3";
- GenerateDatabase();
- }
- private void Submit(object sender, EventArgs e) {
- if (txt_name.Text != "" && txt_country.Text != "")
- {
- try {
- cmd.CommandText = @"INSERT INTO member (name, country) VALUES(@name, @country)";
- cmd.Connection = conn;
- conn.Open();
- int i = cmd.ExecuteNonQuery();
- if (i == 1)
- {
- MessageBox.Show("Successfully Created!");
- txt_name.Text = "";
- txt_country.Text = "";
- }
- conn.Close();
- }
- catch (Exception ex) {
- MessageBox.Show(ex.Message);
- }
- }
- else {
- MessageBox.Show("Required Fields!");
- }
- }
- private void GenerateDatabase() {
- String path = Application.StartupPath + @"\data.db";
- if (!File.Exists(path)) {
- conn.Open();
- string sql = "CREATE TABLE member (ID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, country TEXT)";
- cmd.ExecuteNonQuery();
- conn.Close();
- }
- }
- }
- }
Add new comment
- 341 views