Udostępnij za pośrednictwem


Change the border color and style of PictureBox in VB.NET

You know that by default you don't have too many options to change the Border Color/Style of a normal PictureBox control. I wanted to customize this control a little bit, so that you can at least change the border color and style when you click on any image. Let's see how it looks, and then I will show you how to code it.

image 

 

<---- You see two Picture boxes here with Green Border.

 

 

 

 

<----- This Picture box is the one which comes by default. The border color here is Black and you don't have any property to change it directly.

 

 

 

 

image

 

 

<---- When you click on the picturebox, you will see that the border styles changes along with the color. It is no rocket science and the code to achieve this is pretty simple too.

 

 

 

 

 

 

 

 

 

Create a new class called myPictureBox.vb in your VB.NET project.

Imports System.Windows.Forms
Imports System.Drawing
Public Class myPictureBox
    'This picture box is a bit special since you can change the border of the Picturebox.
    'Notice that in the default picture box, you don't have too many options to customize
    'the border. In this class, you just override the OnPaint event and draw the
    'appropriate border when you click on any image control.
    'I wanted to do this, since I had multiple images in a page, and I wanted to change
    'the border or something when you click the image
    Inherits PictureBox
    Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)
        If Me.BackColor = Color.LightGray Then
            ControlPaint.DrawBorder(pe.Graphics, pe.ClipRectangle, Color.Gray, ButtonBorderStyle.Solid)
        Else
            ControlPaint.DrawBorder(pe.Graphics, pe.ClipRectangle, Color.Green, ButtonBorderStyle.Dashed)
        End If
        MyBase.OnPaint(pe)
    End Sub
    Private Sub myPictureBox_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        If Me.BackColor = Color.LightGray Then
            Me.BackColor = Color.White
        Else
            Me.BackColor = Color.LightGray
        End If
    End Sub
End Class

To check this class, I have created a Form called PictureBoxDemo (I am using VS 2005, by the way). It is the same form which you see in the pictures above. Pretty easy... isn't it?

Public Class PictureBoxDemo
    Private Sub PictureBoxDemo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myPic1 As New myPictureBox
        Dim myPic2 As New myPictureBox
        Dim pctActualPictureBox As New PictureBox
        With myPic1
            .Top = 10
            .Left = 10
            .BackColor = Color.LightGray
        End With
        With myPic2
            .Top = 10
            .Left = 150
            .BackColor = Color.LightGray
        End With
        With pctActualPictureBox
            .Top = 150
            .Left = 10
            .BackColor = Color.LightGray
            .BorderStyle = BorderStyle.FixedSingle
        End With
        Me.Controls.Add(myPic1)
        Me.Controls.Add(myPic2)
        Me.Controls.Add(pctActualPictureBox)
    End Sub
End Class

Until next time Wave
Rahul

Share this post :

Comments

  • Anonymous
    April 06, 2008
    Hi Rahul. Thanks for your tutorial, it really helped me create a proper Vista style form, with the grey bottom border, white background and invisible left, top and right borders!

  • Anonymous
    October 21, 2008
    Dear Mr. Rahul, That’s what I’m looking for. But my wish is; To copy Image from RichTextbox. Can you help me regarding copy/paste image on RichTextBox? Thanks’ in advance. Naim naim70@pwnet.ch

  • Anonymous
    October 21, 2008
    Dear Mr. Rahul, That’s what I’m looking for. But my wish is; To copy Image from RichTextbox. Can you help me regarding copy/paste image on RichTextBox? Thanks’ in advance. Naim naim70@pwnet.ch

  • Anonymous
    October 08, 2009
    Thankx a lot for this tutorila its really helpful

  • Anonymous
    January 17, 2010
    How to make graphics Persistance in VB.Net2008 using 'onpaint' method

  • Anonymous
    January 17, 2010
    Hi rahul... Can you please help me...regarding.. How to make graphics Persistance in VB.Net2008 using 'onpaint' method I tried with picturebox with 'Paint' event but only lastly drawn gaphics are persistance...How to maintain all the drawn graphics on the picturebox in vb.net 2008 Thanks in advance...!!!