How to Add the Items in the ListBox Using InputBox in VB.Net

In this tutorial I will teach you how to add the items in the ListBox in VB.Net. With this, you can type whatever data you want in the InputBox and it will be added in the ListBox. The InputBox has been just like a MessageBox, their only difference is that the InputBox has a TextBox that you can input the data. Let’s begin: Open the Visual Studio and create a new Project. Drag a Button and a ListBox. Then, do the Form just like this. First Form InputBox After that, double click the Button to fire the click event handler. Now, create a string variable to store the data that you have input and set the value on it.
  1. Dim inputdata As String = Nothing
Then, you have to create a constant variable to store a message on it and set it to its default message.
  1. Const msg As String = "Input data"
After that, you have to add the input data from the InputBox in the ListBox.
  1. ListBox1.Items.Add(InputBox(msg, "INPUT", inputdata))
Lastly, do this following code for clearing the data in the ListBox of the “Clear” Button.
  1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  2. 'CLEARING THE DATA IN THE LISTBOX
  3. ListBox1.Items.Clear()
  4. End Sub
These are all the codes that we made.
  1. Public Class Form1
  2. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  3. 'SET A STRING VARIABLE FOR THE DATA
  4. 'TO BE INPUT AND FOR THE MESSAGE OF THE INPUTBOX
  5. Dim inputdata As String = Nothing
  6. Const msg As String = "Input data"
  7.  
  8. 'ADDING THE DATA IN THE LISTBOX THROUGH INPUTBOX
  9. ListBox1.Items.Add(InputBox(msg, "INPUT", inputdata))
  10.  
  11. End Sub
  12. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  13. 'CLEARING THE DATA IN THE LISTBOX
  14. ListBox1.Items.Clear()
  15. End Sub
  16. End Class

Add new comment