Visual Basic 2017

Hot

Thursday, November 2, 2017

How to get number of items in ListBox

November 02, 2017 1
How to get number of items in ListBox is very easy in Visual Basic 2017.

Let say we have a listbox populated with some items. We need to put the number of all items in Listbox into TextBox.

TextBox1 = ListBox1.Items.Count

How to get number of items in ListBox

Read More

Monday, October 30, 2017

How to Save ListBox Items to Text File using OpenFile Dialog

October 30, 2017 0
To explain How to Save ListBox Items to Text File using OpenFile Dialog I will use 3 buttons and 1 ListBox.

  • Button1 = Add items
  • Button2 = Save ListBox items
  • Button3 = Read Text File
  • ListBox
Above Public Class Form1 we put the bellow code:

Imports System.IO

Button1 code = Add items:


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ListBox1.Items.Add("item 1")
        ListBox1.Items.Add("item 2")
        ListBox1.Items.Add("item 3")
        ListBox1.Items.Add("item 4")
        ListBox1.Items.Add("item 5")
        ListBox1.Items.Add("item 6")
        ListBox1.Items.Add("item 7")
    End Sub

Button2 code = Save ListBox Items:

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.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

This code actually is rewriting the Text File if it contains some data. In one of my next posts I will include a code for Appending.

Button3 code = Read Text File

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.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

How to Save ListBox Items to Text File using OpenFile Dialog


Read More

Friday, October 27, 2017

How to read a text file in Listbox opened with OpenFileDialog

October 27, 2017 0
Knowing how to read a text file in Listbox is essential part in Visual Basic 2017 programming.

In this post I will teach you how to do it by using OpenFileDialog.

First let create a text file with notepad with the following data.

Read a text file in ListBox

Now save the file somewhere on the local disk. I save it as a test file.txt on the Desktop.

Add a button in the form and title it Read text file; add a listbox too.

Above Public Class Form1 insert the following code

Imports System.IO

If you are complete beginner and you don't know how it looks like see on the picture bellow!

How to read a text file in Listbox opened with OpenFileDialog


In the button insert the following code to read the text file in Listbox.

        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


Read More

Thursday, October 26, 2017

How to remove multiple selected items in ListBox

October 26, 2017 0
To explain better how to remove multiple selected items in ListBox I will use the form structure from the previous post "How to remove selected item in Listbox" with little changes. Plus I will add an option for multi selection in Listbox and additional button for multi deletion.

Add items in Listbox
        ListBox1.Items.Add("some text")
        ListBox1.Items.Add("some text 1")
        ListBox1.Items.Add("some text 2")
        ListBox1.Items.Add("some text 3")
        ListBox1.Items.Add("some text 4")

To add multi selection in Listbox I added a code in Form1_Load event

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ListBox1.SelectionMode = SelectionMode.MultiSimple
End Sub

Remove multiple selected items in Listbox code

       For i As Integer = ListBox1.SelectedIndices.Count - 1 To 0 Step -1
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndices.Item(i))
        Next

How to remove multiple selected items in ListBox

Read More

How to remove selected item in ListBox?

October 26, 2017 0
In Visual Basic 2017 to remove selected item in ListBox is the same as in the previous releases in Visual Basic Net editions.

To solve this problem I putted a listbox in the form and two button. The first button is actually to add items in the listbox.
Here is the code for the first button:

ListBox1.Items.Add("some text")

The second button is the "Remove" button, as on the picture bellow.

ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)

How to remove selected item in ListBox?

Read More

Monday, October 16, 2017

How to Add Item in ListBox Control from TextBox

October 16, 2017 0
First create a form similar to the picture bellow!
How to Add Item in ListBox Control

- 1 TextBox control
- 1 Button
- 1 ListBox control

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

We can polish this code by adding a code to clear the content in the TextBox control after pressing the button and focusing the TextBox.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ListBox1.Items.Add(TextBox1.Text)

        TextBox1.Clear()
        TextBox1.Select()
    End Sub

Probably you noticed that I used the code TextBox1.Select() to focus the cursor onto the TextBox control. This is because in Visual Basic 2017 you can't set focus on a Windows Form TextBox control.
Read More

Wednesday, October 11, 2017

Intro to Programming Using Visual Basic Coding Exercises

October 11, 2017 0

Learn VB .Net by doing programming projects for beginners

Visual Basic .net is an excellent programming language. Beginners can appreciate it’s English-like syntax, and more advanced programmers can appreciate its vast libraries and flexibility. It’s part of the .Net family of languages, and as such, it allows you to program some truly amazing solutions. So, congratulations on choosing to learn this amazing language. :-)

What Will You Learn

  • Practice and understand loops, arrays, structures, LINQ, Databases, OOP and more
  • Code beginner and intermediate projects using Visual Basic
  • Become an intermediate programmer by understanding the how and why of each line in your program
  • Think like a programmer
  • Uderstand project requirements and choose the right approach to each solution
  • Construct, identify, and use Windows Form Controls effectively

What Are The Requirements

  • Students should have installed Visual Studio in order to follow along
  • Students should be actively learning Visual Basic
  • Students should be familiar with the programming concepts presented in the exercises

Includes:

  • 14 hours on-demand video
  • 56 Supplemental Resources
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion



Read More