0

How can I have a picturebox (control) with a caption? I already can add Text over this picturebox, but I want it under the picturebox. But if the location of the text is beyond Picturebox's size, it won't be visible. It would be great if the text had a border & background color too.

Please help.

Here's the code:

Public Class neoPic
   Inherits PictureBox
   Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
       MyBase.OnPaint(e)
       e.Graphics.DrawString("Caption ", New Font("Cambria", 10), Brushes.Black, New PointF(0, 60))
   End Sub
End Class
Amin Darvand
  • 367
  • 2
  • 19
  • 2
    If it has to go *under* the picture box then you'll have to consider using a Label since that can't be done with the Paint event. How big you make the pbox and whether or not you decide to hide the Label is entirely up to you. – Hans Passant Nov 27 '17 at 13:09
  • 3
    If you want the PictureBox and Label to work as a "unit", then create a **UserControl** with those two controls in it. You can setup the Anchor properties so that the Label is always at the bottom and the width of the UserControl, while the PictureBox will take up the rest of the space. – Idle_Mind Nov 27 '17 at 13:22
  • Thanks @Idle_Mind and HansPassant, so it seems like I should be creating a totally new control. I haven't done that before. I should look up some tutorials. Thank you. – Amin Darvand Nov 27 '17 at 14:37

1 Answers1

1

I ended up creating a UserControl. I put the working code here in case it saves another developer's time. I added a UserControl to my project (named PicTitled) and added a Picturebox (named PTPicturebox) and a Label (named PTLabel) stacked on each other. Then I added Text & Image property for the PicTitled and an event handler for mouse click.

Public Class PicTitled
    Public Shadows Event MouseClick As MouseEventHandler


    Overrides Property Text As String
        Get
            Return PTLabel.Text
        End Get
        Set(ByVal Value As String)
            PTLabel.Text = Value
        End Set
    End Property

    Property Image As Image
        Get
            Return PTPicturebox.Image
        End Get
        Set(ByVal Value As Image)
            PTPicturebox.Image = Value
        End Set
    End Property

    Private Sub PicTitled_MouseClick(sender As Object, e As MouseEventArgs) Handles MyBase.MouseClick, PTPicturebox.MouseClick, PTLabel.MouseClick
        RaiseEvent MouseClick(Me, e)
    End Sub


End Class

And in the main form, I added a code like:

    Private Sub CreateObj()
        Dim pbPicture As New PicTitled
        pbPicture.Name = "Object"
        pbPicture.Location = New System.Drawing.Point(40, 40)
        pbPicture.Text = "Object"
        pbPicture.Image = My.Resources.IMG
        pbPicture.Size = New System.Drawing.Size(50, 50)
        AddHandler pbPicture.MouseClick, AddressOf PictureBox_MouseClick

        Panel1.Controls.Add(pbPicture)
    End Sub

    Private Sub PictureBox_MouseClick(sender As Object, e As MouseEventArgs)
        'Do stuff when mouse click happens...
    End Sub     
Amin Darvand
  • 367
  • 2
  • 19