VB.NET ComboBox CRUD - ADD UPDATE DELETE CLEAR

vb.net crud

In this tutorial we want to see how to add update delete and clear items to and from a combobox. This is a windows froms tutorial to help us understand the combobox.

What is a ComboBox?

ComboBox is a Windows Forms control used to display data in a drop-down combo box.

Normally it appears in two parts:

  1. A top part which is a text box that allows the user to type a list item.
  2. The second part is a list box that displays a list of items from which the user can select one.

We have a YouTube Channel By the Way:

So if you prefer video tutorial check here:

Selecting an Item in a ComboBox

Normally we want to get an item that is selected from a ComboBox. We can use the SelectedIndex property which does return an integer value that corresponds to the selected list item.

If no item is selected, the SelectedIndex value is -1.

Performing CRUD in ComboBox

To add or delete items in a ComboBox control, use the Add, Insert, Clear or Remove method.

This is what we do in this class, performing CRUD on a ComboBox.

VB.NET ComboBox CRUD Example.

So in this class we want to see how to perform CRUD activities in our VB.NET windows forms combobx control.

The user types data in a TextBox, he clicks add and the item gets added.

He selects an item from the combobox, we set that selected item into the TextBox, he can the edit it and click update to update the combobox.

He can select and item and click delete to delete that particular item.'

If he clicks clear we clear all items from the combobox.

Add Imports

First create an console VB.NET application. We want to start from scratch.

Then add the System, System.Drawing and System.Windows.Forms into your project.

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Specify Instance Fields

First create specify our module name and add instance fields including:

  1. ComboBox
  2. TextBox
  3. Buttons.
Module Module1
    'Instance Fields
    Private WithEvents myComboBox As ComboBox
    Private nameTxt As TextBox
    Private WithEvents addBtn As Button
    Private WithEvents updateBtn As Button
    Private WithEvents deleteBtn As Button
    Private WithEvents clearBtn As Button
    Private myForm As Form
    Private selected As Boolean

Set Selected ComboBox Item To TextBox

When a combobox item is selected we want to set it's value to a TextBox:

    Private Sub myComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles myComboBox.SelectedIndexChanged
        nameTxt.Text = myComboBox.SelectedItem.ToString()
        selected = True
    End Sub

Adding Item To ComboBox

When the add button is clicked we will add the item typed in our textbox into our combobox.

    Private Sub addBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles addBtn.Click
        myComboBox.Items.Add(nameTxt.Text)
        myComboBox.SelectedItem = nameTxt.Text
        clearTxts()
    End Sub

Update Selected ComboBox Item

First the user selects the item to update, he edits it and clicks update.

    Private Sub updateBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles updateBtn.Click
        If selected Then
            Dim selectedIndex As Integer = myComboBox.SelectedIndex
            myComboBox.Items.RemoveAt(selectedIndex)
            myComboBox.Items.Insert(selectedIndex, nameTxt.Text)
        End If
    End Sub

Delete Selected ComboBox Item

The user selects an item from the combobox and clicks delete to delete that item from our combobox. Our combobox gets refreshed automatically.

    Private Sub deleteBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles deleteBtn.Click
        If selected Then
            Dim selectedIndex As Integer = myComboBox.SelectedIndex
            myComboBox.Items.RemoveAt(selectedIndex)
            clearTxts()
        End If
    End Sub

Let's look at the source code.

Module.vb

We have the source code in our module.All you need to do is copy paste it.

Imports System
Imports System.Drawing
Imports System.Windows.Forms

Module Module1
    'Instance Fields
    Private WithEvents myComboBox As ComboBox
    Private nameTxt As TextBox
    Private WithEvents addBtn As Button
    Private WithEvents updateBtn As Button
    Private WithEvents deleteBtn As Button
    Private WithEvents clearBtn As Button
    Private myForm As Form
    Private selected As Boolean

    'When item is selected, show a messagebox with the selected item
    Private Sub myComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles myComboBox.SelectedIndexChanged
        nameTxt.Text = myComboBox.SelectedItem.ToString()
        selected = True
    End Sub
    'clear textbox
    Private Sub clearTxts()
        nameTxt.Text = ""
    End Sub
    'Add Item To ComboBox
    Private Sub addBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles addBtn.Click
        myComboBox.Items.Add(nameTxt.Text)
        myComboBox.SelectedItem = nameTxt.Text
        clearTxts()
    End Sub
    'Update ComboBox Item
    Private Sub updateBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles updateBtn.Click
        If selected Then
            Dim selectedIndex As Integer = myComboBox.SelectedIndex
            myComboBox.Items.RemoveAt(selectedIndex)
            myComboBox.Items.Insert(selectedIndex, nameTxt.Text)
        End If
    End Sub
    'Delete Selected Item
    Private Sub deleteBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles deleteBtn.Click
        If selected Then
            Dim selectedIndex As Integer = myComboBox.SelectedIndex
            myComboBox.Items.RemoveAt(selectedIndex)
            clearTxts()
        End If
    End Sub
    'Clear ComboBox
    Private Sub clearBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles clearBtn.Click
        myComboBox.Items.Clear()
        clearTxts()
    End Sub
    'Create Form
    Private Sub createForm()
        myForm = New Form With {.Text = "VB.NET WinForms ComboBox : CRUD - ADD UPDATE DELETE", .ClientSize = New Size(734, 484), .BackColor = Color.Goldenrod}
        createComponents()
        Application.EnableVisualStyles()
        Application.Run(myForm)
    End Sub

    'Create Windows Form Controls
    Sub createComponents()
        Dim nameLabel As Label = New Label With {.Location = New Point(431, 137), .Size = New Size(45, 19), .Text = "Name"}
        nameTxt = New TextBox With {.Location = New Point(517, 137), .Size = New Size(194, 23), .TabIndex = 0}
        addBtn = New Button With {.Location = New Point(431, 284), .Size = New Size(83, 31), .Text = "ADD", .TabIndex = 2}
        updateBtn = New Button With {.Location = New Point(587, 284), .Size = New Size(83, 31), .Text = "UPDATE", .TabIndex = 3}
        deleteBtn = New Button With {.Location = New Point(431, 361), .Size = New Size(83, 31), .Text = "DELETE", .TabIndex = 4}
        clearBtn = New Button With {.Location = New Point(587, 361), .Size = New Size(83, 31), .Text = "CLEAR", .TabIndex = 5}
        myComboBox = New ComboBox With {.Location = New Point(24, 90), .Size = New Size(400, 400), .DropDownStyle = ComboBoxStyle.DropDownList}
        myForm.Controls.Add(clearBtn)
        myForm.Controls.Add(deleteBtn)
        myForm.Controls.Add(updateBtn)
        myForm.Controls.Add(addBtn)
        myForm.Controls.Add(nameTxt)
        myForm.Controls.Add(nameLabel)
        myForm.Controls.Add(myComboBox)
    End Sub
    'Main method
    Sub Main()
        createForm()
    End Sub
End Module

Result

Here's what we get

VB.NET ComboBox CRUD ADD UPDATE DELETE CLEAR

Best regards.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Previous Post Next Post