• We just launched and are currently in beta. Join us as we build and grow the community.

C# - Simple Auto Complete List With SQLite

Thesoundofaruba

Earnings Trend Analyst
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
79
Likes
79
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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...

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.
2018-04-25_13_13_25-.png


Then go to the browse and search sqlite, after that install it and wait until the process is completed.
2018-04-25_13_20_11-simple_autocomplete_text_-_microsoft_visual_studio.png


Next go to the Updates and update the needed framework to make sqlite work properly.
2018-04-25_13_23_25-simple_autocomplete_text_-_microsoft_visual_studio.png

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.
  1. namespace

    Simple_AutoComplete_Text
  2. {
  3. partial

    class

    Main
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. private

    System.ComponentModel

    .

    IContainer

    components =

    null

    ;

  9. /// <summary>
  10. /// Clean up any resources being used.
  11. /// </summary>
  12. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  13. protected

    override

    void

    Dispose(

    bool

    disposing)
  14. {
  15. if

    (

    disposing &&

    (

    components !=

    null

    )

    )
  16. {
  17. components.

    Dispose

    (

    )

    ;
  18. }
  19. base

    .

    Dispose

    (

    disposing)

    ;
  20. }

  21. #region Windows Form Designer generated code

  22. /// <summary>
  23. /// Required method for Designer support - do not modify
  24. /// the contents of this method with the code editor.
  25. /// </summary>
  26. private

    void

    InitializeComponent(

    )
  27. {
  28. this

    .

    btn_submit

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    Button

    (

    )

    ;
  29. this

    .

    label1

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    Label

    (

    )

    ;
  30. this

    .

    groupBox1

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    GroupBox

    (

    )

    ;
  31. this

    .

    txt_country

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    TextBox

    (

    )

    ;
  32. this

    .

    label2

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    Label

    (

    )

    ;
  33. this

    .

    txt_name

    =

    new

    System

    .

    Windows

    .

    Forms

    .

    TextBox

    (

    )

    ;
  34. this

    .

    groupBox1

    .

    SuspendLayout

    (

    )

    ;
  35. this

    .

    SuspendLayout

    (

    )

    ;
  36. //
  37. // btn_submit
  38. //
  39. this

    .

    btn_submit

    .

    Font

    =

    new

    System.Drawing

    .

    Font

    (

    "Arial"

    , 12F, System.Drawing

    .

    FontStyle

    .

    Regular

    , System.Drawing

    .

    GraphicsUnit

    .

    Point

    , (

    (

    byte

    )

    (

    0

    )

    )

    )

    ;
  40. this

    .

    btn_submit

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    177

    , 297

    )

    ;
  41. this

    .

    btn_submit

    .

    Name

    =

    "btn_submit"

    ;
  42. this

    .

    btn_submit

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    131

    , 40

    )

    ;
  43. this

    .

    btn_submit

    .

    TabIndex

    =

    0

    ;
  44. this

    .

    btn_submit

    .

    Text

    =

    "Submit"

    ;
  45. this

    .

    btn_submit

    .

    UseVisualStyleBackColor

    =

    true

    ;
  46. this

    .

    btn_submit

    .

    Click

    +=

    new

    System

    .

    EventHandler

    (

    this

    .

    Submit

    )

    ;
  47. //
  48. // label1
  49. //
  50. this

    .

    label1

    .

    AutoSize

    =

    true

    ;
  51. this

    .

    label1

    .

    Font

    =

    new

    System.Drawing

    .

    Font

    (

    "Arial"

    , 12F, System.Drawing

    .

    FontStyle

    .

    Regular

    , System.Drawing

    .

    GraphicsUnit

    .

    Point

    , (

    (

    byte

    )

    (

    0

    )

    )

    )

    ;
  52. this

    .

    label1

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    76

    , 100

    )

    ;
  53. this

    .

    label1

    .

    Name

    =

    "label1"

    ;
  54. this

    .

    label1

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    86

    , 27

    )

    ;
  55. this

    .

    label1

    .

    TabIndex

    =

    1

    ;
  56. this

    .

    label1

    .

    Text

    =

    "NAME:"

    ;
  57. //
  58. // groupBox1
  59. //
  60. this

    .

    groupBox1

    .

    Controls

    .

    Add

    (

    this

    .

    txt_country

    )

    ;
  61. this

    .

    groupBox1

    .

    Controls

    .

    Add

    (

    this

    .

    label2

    )

    ;
  62. this

    .

    groupBox1

    .

    Controls

    .

    Add

    (

    this

    .

    txt_name

    )

    ;
  63. this

    .

    groupBox1

    .

    Controls

    .

    Add

    (

    this

    .

    label1

    )

    ;
  64. this

    .

    groupBox1

    .

    Controls

    .

    Add

    (

    this

    .

    btn_submit

    )

    ;
  65. this

    .

    groupBox1

    .

    Font

    =

    new

    System.Drawing

    .

    Font

    (

    "Arial"

    , 12F, System.Drawing

    .

    FontStyle

    .

    Regular

    , System.Drawing

    .

    GraphicsUnit

    .

    Point

    , (

    (

    byte

    )

    (

    0

    )

    )

    )

    ;
  66. this

    .

    groupBox1

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    12

    , 12

    )

    ;
  67. this

    .

    groupBox1

    .

    Name

    =

    "groupBox1"

    ;
  68. this

    .

    groupBox1

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    489

    , 354

    )

    ;
  69. this

    .

    groupBox1

    .

    TabIndex

    =

    2

    ;
  70. this

    .

    groupBox1

    .

    TabStop

    =

    false

    ;
  71. this

    .

    groupBox1

    .

    Text

    =

    "Member Information"

    ;
  72. //
  73. // txt_country
  74. //
  75. this

    .

    txt_country

    .

    AutoCompleteCustomSource

    .

    AddRange

    (

    new

    string

    [

    ]

    {
  76. "Afghanistan"

    ,
  77. "Albania"

    ,
  78. "Algeria"

    ,
  79. "Andorra"

    ,
  80. "Angola"

    ,
  81. "Anguilla"

    ,
  82. "Antigua & Barbuda"

    ,
  83. "Argentina"

    ,
  84. "Armenia"

    ,
  85. "Australia"

    ,
  86. "Austria"

    ,
  87. "Azerbaijan"

    ,
  88. "Bahamas"

    ,
  89. "Bahrain"

    ,
  90. "Bangladesh"

    ,
  91. "Barbados"

    ,
  92. "Belarus"

    ,
  93. "Belgium"

    ,
  94. "Belize"

    ,
  95. "Benin"

    ,
  96. "Bermuda"

    ,
  97. "Bhutan"

    ,
  98. "Bolivia"

    ,
  99. "Bosnia & Herzegovina"

    ,
  100. "Botswana"

    ,
  101. "Brazil"

    ,
  102. "Brunei Darussalam"

    ,
  103. "Bulgaria"

    ,
  104. "Burkina Faso"

    ,
  105. "Myanmar/Burma"

    ,
  106. "Burundi"

    ,
  107. "Cambodia"

    ,
  108. "Cameroon"

    ,
  109. "Canada"

    ,
  110. "Cape Verde"

    ,
  111. "Cayman Islands"

    ,
  112. "Central African Republic"

    ,
  113. "Chad"

    ,
  114. "Chile"

    ,
  115. "China"

    ,
  116. "Colombia"

    ,
  117. "Comoros"

    ,
  118. "Congo"

    ,
  119. "Costa Rica"

    ,
  120. "Croatia"

    ,
  121. "Cuba"

    ,
  122. "Cyprus"

    ,
  123. "Czech Republic"

    ,
  124. "Democratic Republic of the Congo"

    ,
  125. "Denmark"

    ,
  126. "Djibouti"

    ,
  127. "Dominica"

    ,
  128. "Dominican Republic"

    ,
  129. "Ecuador"

    ,
  130. "Egypt"

    ,
  131. "El Salvador"

    ,
  132. "Equatorial Guinea"

    ,
  133. "Eritrea"

    ,
  134. "Estonia"

    ,
  135. "Ethiopia"

    ,
  136. "Fiji"

    ,
  137. "Finland"

    ,
  138. "France"

    ,
  139. "French Guiana"

    ,
  140. "Gabon"

    ,
  141. "Gambia"

    ,
  142. "Georgia"

    ,
  143. "Germany"

    ,
  144. "Ghana"

    ,
  145. "Great Britain"

    ,
  146. "Greece"

    ,
  147. "Grenada"

    ,
  148. "Guadeloupe"

    ,
  149. "Guatemala"

    ,
  150. "Guinea"

    ,
  151. "Guinea-Bissau"

    ,
  152. "Guyana"

    ,
  153. "Haiti"

    ,
  154. "Honduras"

    ,
  155. "Hungary"

    ,
  156. "Iceland"

    ,
  157. "India"

    ,
  158. "Indonesia"

    ,
  159. "Iran"

    ,
  160. "Iraq"

    ,
  161. "Israel and the Occupied Territories"

    ,
  162. "Italy"

    ,
  163. "Ivory Coast (Cote d\'

    Ivoire)"

    ,
  164. "Jamaica"

    ,
  165. "Japan"

    ,
  166. "Jordan"

    ,
  167. "Kazakhstan"

    ,
  168. "Kenya"

    ,
  169. "Kosovo"

    ,
  170. "Kuwait"

    ,
  171. "Kyrgyz Republic (Kyrgyzstan)"

    ,
  172. "Laos"

    ,
  173. "Latvia"

    ,
  174. "Lebanon"

    ,
  175. "Lesotho"

    ,
  176. "Liberia"

    ,
  177. "Libya"

    ,
  178. "Liechtenstein"

    ,
  179. "Lithuania"

    ,
  180. "Luxembourg"

    ,
  181. "Republic of Macedonia"

    ,
  182. "Madagascar"

    ,
  183. "Malawi"

    ,
  184. "Malaysia"

    ,
  185. "Maldives"

    ,
  186. "Mali"

    ,
  187. "Malta"

    ,
  188. "Martinique"

    ,
  189. "Mauritania"

    ,
  190. "Mauritius"

    ,
  191. "Mayotte"

    ,
  192. "Mexico"

    ,
  193. "Moldova, Republic of"

    ,
  194. "Monaco"

    ,
  195. "Mongolia"

    ,
  196. "Montenegro"

    ,
  197. "Montserrat"

    ,
  198. "Morocco"

    ,
  199. "Mozambique"

    ,
  200. "Namibia"

    ,
  201. "Nepal"

    ,
  202. "Netherlands"

    ,
  203. "New Zealand"

    ,
  204. "Nicaragua"

    ,
  205. "Niger"

    ,
  206. "Nigeria"

    ,
  207. "Korea, Democratic Republic of (North Korea)"

    ,
  208. "Norway"

    ,
  209. "Oman"

    ,
  210. "Pacific Islands"

    ,
  211. "Pakistan"

    ,
  212. "Panama"

    ,
  213. "Papua New Guinea"

    ,
  214. "Paraguay"

    ,
  215. "Peru"

    ,
  216. "Philippines"

    ,
  217. "Poland"

    ,
  218. "Portugal"

    ,
  219. "Puerto Rico"

    ,
  220. "Qatar"

    ,
  221. "Reunion"

    ,
  222. "Romania"

    ,
  223. "Russian Federation"

    ,
  224. "Rwanda"

    ,
  225. "Saint Kitts and Nevis"

    ,
  226. "Saint Lucia"

    ,
  227. "Saint Vincent\'

    s & Grenadines"

    ,
  228. "Samoa"

    ,
  229. "Sao Tome and Principe"

    ,
  230. "Saudi Arabia"

    ,
  231. "Senegal"

    ,
  232. "Serbia"

    ,
  233. "Seychelles"

    ,
  234. "Sierra Leone"

    ,
  235. "Singapore"

    ,
  236. "Slovak Republic (Slovakia)"

    ,
  237. "Slovenia"

    ,
  238. "Solomon Islands"

    ,
  239. "Somalia"

    ,
  240. "South Africa"

    ,
  241. "Korea, Republic of (South Korea)"

    ,
  242. "South Sudan"

    ,
  243. "Spain"

    ,
  244. "Sri Lanka"

    ,
  245. "Sudan"

    ,
  246. "Suriname"

    ,
  247. "Swaziland"

    ,
  248. "Sweden"

    ,
  249. "Switzerland"

    ,
  250. "Syria"

    ,
  251. "Tajikistan"

    ,
  252. "Tanzania"

    ,
  253. "Thailand"

    ,
  254. "Timor Leste"

    ,
  255. "Togo"

    ,
  256. "Trinidad & Tobago"

    ,
  257. "Tunisia"

    ,
  258. "Turkey"

    ,
  259. "Turkmenistan"

    ,
  260. "Turks & Caicos Islands"

    ,
  261. "Uganda"

    ,
  262. "Ukraine"

    ,
  263. "United Arab Emirates"

    ,
  264. "United States of America (USA)"

    ,
  265. "Uruguay"

    ,
  266. "Uzbekistan"

    ,
  267. "Venezuela"

    ,
  268. "Vietnam"

    ,
  269. "Virgin Islands (UK)"

    ,
  270. "Virgin Islands (US)"

    ,
  271. "Yemen"

    ,
  272. "Zambia"

    ,
  273. "Zimbabwe"

    }

    )

    ;
  274. this

    .

    txt_country

    .

    AutoCompleteMode

    =

    System

    .

    Windows

    .

    Forms

    .

    AutoCompleteMode

    .

    Suggest

    ;
  275. this

    .

    txt_country

    .

    AutoCompleteSource

    =

    System

    .

    Windows

    .

    Forms

    .

    AutoCompleteSource

    .

    CustomSource

    ;
  276. this

    .

    txt_country

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    177

    , 199

    )

    ;
  277. this

    .

    txt_country

    .

    Name

    =

    "txt_country"

    ;
  278. this

    .

    txt_country

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    262

    , 35

    )

    ;
  279. this

    .

    txt_country

    .

    TabIndex

    =

    4

    ;
  280. //
  281. // label2
  282. //
  283. this

    .

    label2

    .

    AutoSize

    =

    true

    ;
  284. this

    .

    label2

    .

    Font

    =

    new

    System.Drawing

    .

    Font

    (

    "Arial"

    , 12F, System.Drawing

    .

    FontStyle

    .

    Regular

    , System.Drawing

    .

    GraphicsUnit

    .

    Point

    , (

    (

    byte

    )

    (

    0

    )

    )

    )

    ;
  285. this

    .

    label2

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    27

    , 202

    )

    ;
  286. this

    .

    label2

    .

    Name

    =

    "label2"

    ;
  287. this

    .

    label2

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    135

    , 27

    )

    ;
  288. this

    .

    label2

    .

    TabIndex

    =

    3

    ;
  289. this

    .

    label2

    .

    Text

    =

    "COUNTRY:"

    ;
  290. //
  291. // txt_name
  292. //
  293. this

    .

    txt_name

    .

    Location

    =

    new

    System.Drawing

    .

    Point

    (

    177

    , 97

    )

    ;
  294. this

    .

    txt_name

    .

    Name

    =

    "txt_name"

    ;
  295. this

    .

    txt_name

    .

    Size

    =

    new

    System.Drawing

    .

    Size

    (

    262

    , 35

    )

    ;
  296. this

    .

    txt_name

    .

    TabIndex

    =

    2

    ;
  297. //
  298. // Main
  299. //
  300. this

    .

    AutoScaleDimensions

    =

    new

    System.Drawing

    .

    SizeF

    (

    9F, 20F)

    ;
  301. this

    .

    AutoScaleMode

    =

    System

    .

    Windows

    .

    Forms

    .

    AutoScaleMode

    .

    Font

    ;
  302. this

    .

    ClientSize

    =

    new

    System.Drawing

    .

    Size

    (

    518

    , 378

    )

    ;
  303. this

    .

    Controls

    .

    Add

    (

    this

    .

    groupBox1

    )

    ;
  304. this

    .

    Name

    =

    "Main"

    ;
  305. this

    .

    StartPosition

    =

    System

    .

    Windows

    .

    Forms

    .

    FormStartPosition

    .

    CenterScreen

    ;
  306. this

    .

    Text

    =

    "Main"

    ;
  307. this

    .

    groupBox1

    .

    ResumeLayout

    (

    false

    )

    ;
  308. this

    .

    groupBox1

    .

    PerformLayout

    (

    )

    ;
  309. this

    .

    ResumeLayout

    (

    false

    )

    ;

  310. }

  311. #endregion

  312. private

    System

    .

    Windows

    .

    Forms

    .

    Button

    btn_submit;
  313. private

    System

    .

    Windows

    .

    Forms

    .

    Label

    label1;
  314. private

    System

    .

    Windows

    .

    Forms

    .

    GroupBox

    groupBox1;
  315. private

    System

    .

    Windows

    .

    Forms

    .

    TextBox

    txt_country;
  316. private

    System

    .

    Windows

    .

    Forms

    .

    Label

    label2;
  317. private

    System

    .

    Windows

    .

    Forms

    .

    TextBox

    txt_name;
  318. }
  319. }

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.
2018-04-25_13_44_15-simple_autocomplete_text_-_microsoft_visual_studio.png


After that select the ... in the AutoCompleteCustomSource then put all the needed string here,
2018-04-25_13_47_44-string_collection_editor.png

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.
  1. using

    System

    ;
  2. using

    System.Collections.Generic

    ;
  3. using

    System.ComponentModel

    ;
  4. using

    System.Data

    ;
  5. using

    System.Drawing

    ;
  6. using

    System.Linq

    ;
  7. using

    System.Text

    ;
  8. using

    System.Threading.Tasks

    ;
  9. using

    System.Windows.Forms

    ;
  10. using

    System.Data.SQLite

    ;
  11. using

    System.IO

    ;

  12. namespace

    Simple_AutoComplete_Text
  13. {
  14. public

    partial

    class

    Main :

    Form
  15. {
  16. SQLiteConnection conn;
  17. SQLiteCommand cmd;
  18. String

    connectString;


  19. public

    Main(

    )
  20. {
  21. InitializeComponent(

    )

    ;
  22. connectString =

    @"Data Source="

    +

    Application.

    StartupPath

    +

    @"\data.db;version=3"

    ;
  23. GenerateDatabase(

    )

    ;
  24. }

  25. private

    void

    Submit(

    object

    sender, EventArgs e)

    {
  26. if

    (

    txt_name.

    Text

    !=

    ""

    &&

    txt_country.

    Text

    !=

    ""

    )
  27. {
  28. try

    {
  29. conn =

    new

    SQLiteConnection(

    connectString)

    ;
  30. cmd =

    new

    SQLiteCommand(

    )

    ;
  31. cmd.

    CommandText

    =

    @"INSERT INTO member (name, country) VALUES(@name, @country)"

    ;
  32. cmd.

    Connection

    =

    conn;
  33. cmd.

    Parameters

    .

    Add

    (

    new

    SQLiteParameter(

    "@name"

    , txt_name.

    Text

    )

    )

    ;
  34. cmd.

    Parameters

    .

    Add

    (

    new

    SQLiteParameter(

    "@country"

    , txt_country.

    Text

    )

    )

    ;
  35. conn.

    Open

    (

    )

    ;

  36. int

    i =

    cmd.

    ExecuteNonQuery

    (

    )

    ;

  37. if

    (

    i ==

    1

    )
  38. {
  39. MessageBox.

    Show

    (

    "Successfully Created!"

    )

    ;
  40. txt_name.

    Text

    =

    ""

    ;
  41. txt_country.

    Text

    =

    ""

    ;
  42. }

  43. conn.

    Close

    (

    )

    ;
  44. }
  45. catch

    (

    Exception ex)

    {
  46. MessageBox.

    Show

    (

    ex.

    Message

    )

    ;
  47. }
  48. }
  49. else

    {
  50. MessageBox.

    Show

    (

    "Required Fields!"

    )

    ;
  51. }
  52. }

  53. private

    void

    GenerateDatabase(

    )

    {
  54. String

    path =

    Application.

    StartupPath

    +

    @"\data.db"

    ;
  55. if

    (

    !

    File.

    Exists

    (

    path)

    )

    {
  56. conn =

    new

    SQLiteConnection(

    connectString)

    ;
  57. conn.

    Open

    (

    )

    ;
  58. string

    sql =

    "CREATE TABLE member (ID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, country TEXT)"

    ;
  59. cmd =

    new

    SQLiteCommand(

    sql, conn)

    ;
  60. cmd.

    ExecuteNonQuery

    (

    )

    ;
  61. conn.

    Close

    (

    )

    ;
  62. }
  63. }


  64. }
  65. }

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!!!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

449,193

322,229

322,238

Top