Total Pageviews

Saturday, September 17, 2016

ListBox Complete Tutorial - Visual Basic 2015

Add data from TextBox into ListBox in Visual Basic 2015


Put one button into the form and title it "Add", Property Name btnAdd. In this button we can put the code. There are several ways to program this button to add data from the textbox into the listbox control.

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        ListBox1.Items.Add(TextBox1.Text)
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim txtboxinfo As String
        txtboxinfo = TextBox1.Text
        ListBox1.Items.Add(txtboxinfo)
    End Sub

Add numbers from 1 to 20 in ListBox control in Visual Basic 2015

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim i As Integer

        For i = 1 To 20
            ListBox1.Items.Add(i)
        Next
    End Sub

Make summary of all items in ListBox and show the results in a TextBox

    Private Sub btnSum_Click(sender As Object, e As EventArgs) Handles btnSum.Click
        Dim sum As Double
        For x As Integer = 0 To ListBox1.Items.Count - 1
            sum += CDbl(ListBox1.Items(x))
        Next
        TextBox1.Text = sum.ToString
    End Sub

Presenting the data can be done even in the ListBox. Usually I am not adding them in the listbox just to do not make difficulties if I have to use the Listbox items again.

    Private Sub btnSum_Click(sender As Object, e As EventArgs) Handles btnSum.Click
        Dim sum As Double
        For x As Integer = 0 To ListBox1.Items.Count - 1
            sum += CDbl(ListBox1.Items(x))
        Next
        ListBox1.Items.Add(sum.ToString)
    End Sub

Delete selected item from ListBox control in Visual Basic 2015

Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles Delete.Click
 Dim itemfordeletion
 itemfordeletion = ListBox1.SelectedItem
 ListBox1.Items.Remove(itemfordeletion)
End Sub

Transfer data from ListBox1 to ListBox2 in Visual Basic 2015

    Private Sub btnTransfer_Click(sender As Object, e As EventArgs) Handles btnTransfer.Click
        Dim i As Integer

        For i = 0 To ListBox1.Items.Count - 1
            ListBox2.Items.Add(ListBox1.Items(i))
        Next
    End Sub

If you want to clear the listbox1 after all data are transferred into the listbox1 2 you need to apply the following code at the end.

    Private Sub btnTransfer_Click(sender As Object, e As EventArgs) Handles btnTransfer.Click
        Dim i As Integer

        For i = 0 To ListBox1.Items.Count - 1
            ListBox2.Items.Add(ListBox1.Items(i))
        Next

        ListBox1.Items.Clear()
    End Sub

So I isolate the code to clear all listbox if necessary.

   Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        ListBox1.Items.Clear()
    End Sub

Search for an item in ListBox

    Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
        Dim index As Integer = ListBox1.FindString(TextBox1.Text)
        If index <> -1 Then
            ListBox1.SetSelected(index, True)
        Else
            MessageBox.Show("We didn't find any item in the ListBox", "No item", MessageBoxButtons.OK)
        End If
    End Sub

Open Text File in ListBox

First you have to insert the code:
Imports System.IO

    Private Sub btnOpenTextFile_Click(sender As Object, e As EventArgs) Handles btnOpenTextFile.Click
        Dim OpenFileDialog1 As New OpenFileDialog
        OpenFileDialog1.FileName = ""
        OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"

        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            Dim lines = File.ReadAllLines(OpenFileDialog1.FileName)
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(lines)
        End If
    End Sub

Save ListBox items in Text File

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Dim SaveFileDialog1 As New SaveFileDialog
        SaveFileDialog1.FileName = ""
        SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"

        If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
            Dim sb As New System.Text.StringBuilder()

            For Each o As Object In ListBox1.Items
                sb.AppendLine(o)
            Next

            System.IO.File.WriteAllText(SaveFileDialog1.FileName, sb.ToString())
        End If
    End Sub

 

No comments:

Post a Comment