JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Using Image Lists with Other Controls

You can use an image list's Draw method to draw an image in a control that you wouldn't usually think of to display images, such as a panel control. You can pass the Draw method a Graphics object to draw in, the X and Y coordinates at which to draw the image, and the index of the image to draw from the internal list of images. To draw in a panel control, you can use the control's Paint event, which happens when a control is drawn. Here's how that looks when I draw image 0 from ImageList1 in Panel1 in the ImageLists example on the CD-ROM:

Private Sub Panel1_Paint(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
    ImageList1.Draw(e.Graphics, 0, 0, 0)
End Sub

The panel control appears at the bottom in Figure 10.1. The user also can click the "New image" button under the panel to load a new image into it, and that's a little trickier to implement, because we're not supplied a Graphics object. In this case, we can get the Windows handle for the panel (a Windows handle is how Windows keeps track of windows internally) with the Handle property, and create a Graphics object with the Graphics class's FromHandle method:

Dim ImageIndex As Integer = 0

Private Sub Button3_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button3.Click
    If ImageIndex < ImageList1.Images.Count - 1 Then
        ImageIndex += 1
    Else
        ImageIndex = 0
    End If
    ImageList1.Draw(Graphics.FromHwnd(Panel1.Handle), 0, 0, ImageIndex)
End Sub

Note that this means we should change the Panel1_Paint event handler, because as written, it only draws the first image, image 0, so only that image will appear when the panel needs to be redrawn (as when the form is minimized and then restored). We can make it draw the currently selected image by using the ImageIndex variable set by the above code instead:

Private Sub Panel1_Paint(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
    ImageList1.Draw(e.Graphics, 0, 0, ImageIndex)
End Sub
Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor