Adding Items in the ListBox Using InputBox in VB.Net

0
1287

In this tutorial “Listbox VB.net Add Item“. I’m going to teach you how to add the items in the ListBox using an InputBox in VB.Net. The InputBox is like a MessageBox, their only difference is that it has a TextBox that you can input the data into it. With this, the data that you have typed in the text box of the InputBox will be added in the ListBox.

 

Let’s begin:

 

Open the Visual Studio and create a new Windows Form Application. Drag a Button and a ListBox and set the Form just like this.
inputboxform
After setting up the Form, double-click the Button to fire the eventclick handler.
Create a string variable for storing data and set the initial value in it.
[vbnet] Dim input_data As String = “”
[/vbnet] Now, you have to create a constant variable and set the default message it.

<vbnet] Const message As String = “Data Entry”
[/vbnet] After that, add the data that you have input in the InputBox to the ListBox.
[vbnet] ListBox1.Items.Add(InputBox(message, “Input”, input_data))
[/vbnet] Lastly, for clearing data in the ListBox, you have to put this code in the click event handler of the “Clear” Button.
[vbnet] Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
‘CLEARING THE DATA IN THE LISTBOX
ListBox1.Items.Clear()
End Sub
[/vbnet] These are all the codes that you have made.
[vbnet] Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
‘SET A STRING VARIABLE FOR THE DATA
‘TO BE INPUT AND FOR THE MESSAGE OF THE INPUTBOX
Dim input_data As String = “”
Const message As String = “Data Entry”

‘ADDING THE DATA IN THE LISTBOX THROUGH INPUTBOX
ListBox1.Items.Add(InputBox(message, “Input”, input_data))

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
‘CLEARING THE DATA IN THE LISTBOX
ListBox1.Items.Clear()
End Sub
End Class
[/vbnet]

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.