How to Insert a ComboBox from an Array in Visual Basic.NET

This article discusses how to insert or insert several data options into a ComboBox that is taken from an Array in Visual Basic.NET (VB.Net) with the aim of not having to be tired of filling in one data list manually. Moreover, in the selection of the data, it will display the results of the data / value / value that is taken from the index value of the ComboBox selection in making Desktop-based applications.

You can take a simple case study, which is an application that displays fruit prices. You can insert or enter some fruit data into a ComboBox that is taken from a List or Array along with creating a data list for fruit prices in the form of an Array which is useful for displaying fruit prices according to the index value on the selected ComboBox in making desktop-based applications with programming languages. Visual Basic.NET. For this tutorial, you can use Visual Studio tools with various versions including Visual Studio 2008, Visual Studio 2010, et al.

CREATE APPLICATION FORM OR DISPLAY

From the appearance of the form above, you need 2 label units, 1 combobox unit, and 1 textbox unit which have their respective functions in the ToolBox, including ComboBox to enter some fruit data according to your needs.

SOURCE CODE

In this section, there are several command codes that are used to display fruit prices based on the selection of the ComboBox.

'Buat Array untuk nama buah.
Dim buah() As String = {"Apel", "Belimbing", "Ceri", "Delima", "Duku", "Pisang"}

'Buat array untuk harga buah.
Dim harga() As Integer = {12000, 13000, 15000, 20000, 15000, 12000}

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'Tampilkan harga buah dari pemilihan combobox secara otomatis.
TextBox1.Text = harga(ComboBox1.SelectedIndex)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Memasukkan list nama buah ke dalam Combobox.
For i = 0 To buah.Length - 1
With ComboBox1
.Items.Add(buah(i))
End With
Next
End Sub

Filling the fruit price list requires the same number of indexes from the previous fruit list. For example, you make a list of 7 indexes of fruit names, so to make a list of fruit prices, it must be the same as the number of indexes from the list of fruit names.

THE FINAL RESULT

In this section will show the final results in making a simple program as follows:

A. Initial View

B. Fruit Name Selection Display

C. Display of Data Selection Results

In the image display point C, it immediately displays the price of the fruit automatically based on the selection of the fruit name from the ComboBox earlier. Therefore, you must make a list of fruit prices with the same number of indexes as the previous list of fruit names.

CLOSING

In making desktop applications, you must make one or more data validations in the selection of certain types or types that can appear data from the selection of data using the Visual Basic.NET programming language. One of them is the ComboBox component. If you don’t use a database or file to insert some data into a ComboBox, then you can use a list or an Array.

From this tutorial you can develop a desktop-based application that suits your creativity in making applications by using data validation for selecting certain data through a ComboBox which will display the results of the data that has been selected from the ComboBox automatically through the use of Arrays.

Leave a comment

Your email address will not be published.