JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Adding Images to Image Lists in Code

We've already seen that you can add new images to image lists at design time by using a collection editor with the Images collection in the image list, but you also can add new images to an image list in code. (Note that the new image should be the same size as the other images in the list, or they'll automatically be resized to the size in the image list's ImageSize property.)

To see how that works, take a look at the "Add image" button in Figure 10.1. When the user clicks this button, the program displays a Open File dialog and uses the Add method of the image list's Images collection to add the newly selected images to the image list. (There's a new image, Image5.jpg, the same size as the others in the image list, in the ImageLists folder on the CD-ROM that you can add to the image list.) Here's how that looks in code—note that I'm allowing the user to make multiple selections in the Open File dialog (in which case you use the dialog's FileNames property) as well as single selections (in which case you use the FileName property):

Private Sub Button4_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button4.Click
    If openFileDialog1.ShowDialog() = DialogResult.OK Then
        If Not (openFileDialog1.FileNames Is Nothing) Then
            Dim intLoopIndex As Integer
            For intLoopIndex = 0 To OpenFileDialog1.FileNames.Length - 1
            Next intLoopIndex
        End If
    End If
End Sub

And that's it—now the user can add new images to the image list at run time.

Related solution:

Found on page:

Creating Open File Dialogs


Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor