VB PictureBox control


The PictureBox control is used for displaying images on the form. The Image property of the control allows you to set an image both at design time or at run time.


Properties of the PictureBox Control

The following are some of the commonly used properties of the PictureBox control:

S.NPropertyDescription
1AllowDropSpecifies whether the picture box accepts data that a user drags on it.
2ErrorImageGets or specifies an image to be displayed when an error occurs during the image-loading process or if the image load is cancelled.
3ImageGets or sets the image that is displayed in the control.
4ImageLocationGets or sets the path or the URL for the image displayed in the control.
5InitialImageGets or sets the image displayed in the control when the main image is loaded
6SizeModeDetermines the size of the image to be displayed in the control. This property takes its value from the PictureBoxSizeMode enumeration, which has values:
  • Normal - the upper left corner of the image is placed at upper left part of the picture box
  • StrechImage - allows stretching of the image
  • AutoSize - allows resizing the picture box to the size of the image
  • CenterImage - allows centering the image in the picture box
  • Zoom - allows increasing or decreasing the image size to maintain the size ratio.
7TabIndexGets or sets the tab index value.
8TabStopSpecifies whether the user will be able to focus on the picture box by using the TAB key.
9TextGets or sets the text for the picture box.
10WaitOnLoadSpecifies whether or not an image is loaded synchronously.

Methods of the PictureBox Control

The following are some of the commonly used methods of the PictureBox control:

S.N Method Name & Description
1 CancelAsync Cancels an asynchronous image load.
2 Load Displays an image in the picture box
3 LoadAsync Loads image asynchronously.
4 ToString Returns the string that represents the current picture box.

Events of the PictureBox Control

The following are some of the commonly used events of the PictureBox control:
S.N Event Description
1 CausesValidationChanged Overrides the Control.CausesValidationChanged property.
2 Click Occurs when the control is clicked.
3 Enter Overrides the Control.Enter property.
4 FontChanged Occurs when the value of the Font property changes.
5 ForeColorChanged Occurs when the value of the ForeColor property changes.
6 KeyDown Occurs when a key is pressed when the control has focus.
7 KeyPress Occurs when a key is pressed when the control has focus.
8 KeyUp Occurs when a key is released when the control has focus.
9 Leave Occurs when input focus leaves the PictureBox.
10 LoadCompleted Occurs when the asynchronous image-load operation is completed, been canceled, or raised an exception.
11 LoadProgressChanged Occurs when the progress of an asynchronous image-loading operation has changed.
12 Resize Occurs when the control is resized.
13 RightToLeftChanged Occurs when the value of the RightToLeft property changes.
14 SizeChanged Occurs when the Size property value changes.
15 SizeModeChanged Occurs when SizeMode changes.
16 TabIndexChanged Occurs when the value of the TabIndex property changes.
17 TabStopChanged Occurs when the value of the TabStop property changes.
18 TextChanged Occurs when the value of the Text property changes.
Example

In this example, let us put a picture box and a button control on the form. We set the image property of the picture box to logo.png, as we used before. The Click event of the button named Button1 is coded to stretch the image to a specified size:

Public Class Form1
   Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      ' Set the caption bar text of the form.  
      Me.Text = "fastlearning.in"
   End Sub
   
   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      PictureBox1.ClientSize = New Size(300, 300)
      PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
   End Sub
End Class

Share this article on