Programming & Scripting Tutorials

Visual Basic: Listboxes and Pictureboxes





Listboxes



We have already covered some basic tools like textboxes and buttons, so lets move on to listboxes.
Listboxes can be very useful, as the user can add things to a list, or select things from a list.
Listboxes (as well as textboxes and buttons) are very easy to use. This is a simple program which lets the user type something, then add it to a listbox:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim input As String
        input = TextBox1.Text

        ListBox1.Items.Add(input)
    End Sub

As you will notice with most visual basic code from this point we have included the whole button click code view. Also as you may have guessed this form in design view has a textbox, a listbox and a button; In later VB lessons you may not be told what is on the form, as you can read the code and figure it out.
The code above as you can see creates the variable "input" and makes it a string, then sets input to whats in the textbox, then finally it adds "input" to the listbox.


PictureBoxes



Pictureboxes are very useful in Visual basic for as the name suggests displaying pictures; and like listboxes they are easy to use. This is a simple bit of code that displays an image (that is stored in a location specific to the user):

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

PictureBox1.Image = Image.FromFile("c:\Users\Public\Pictures\Sample Pictures\Flowers.jpg")

End Sub

This contains a button and a picturebox on the form in design view. As you can see this is an extremely simple bit of code; It simple changes where the picturebox looks for an image when the button is clicked.

This Visual Basic tutorial was written by


Back to Visual Basic

Advertisement: