Write text in a Picture Box

Posted on 2012-09-03
Last Modified: 2012-09-05
I have numeric keyboard, and i have a picture with a key background. I need to show a "label" for each numeric key. For ex. all keys (pictures) has the same image (background) and i need write in each one the correspondent number.
Question by:rflorencio
    LVL 29

    Assisted Solution

    by:Randy Downs
    Try this

        Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
            Dim t As String = "This is some text"
            Dim f As Font = New Font("Arial", 12, FontStyle.Regular)
            Dim g As Graphics = e.Graphics
            Dim StringSize As SizeF = g.MeasureString(t, f)
            Dim StringTop As Single = CSng((PictureBox1.Height / 2) - (StringSize.Height / 2))
            Dim StringLeft As Single = CSng((PictureBox1.Width / 2) - (StringSize.Width / 2))
            g.DrawString(t, f, Brushes.Aquamarine, StringLeft, StringTop)
        End Sub
        Private Sub PictureBox1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.Resize
        End Sub

    Open in new window

    LVL 85

    Accepted Solution

    This will divide the PictureBox into a 3x4 grid and draw the digits centered in each cell:
    Public Class Form1
        Private Sub PictureBox1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
            Dim digits(,) As String = {{"7", "8", "9"}, {"4", "5", "6"}, {"1", "2", "3"}, {"", "0", "."}}
            Dim width As Integer = PictureBox1.Size.Width / 3
            Dim height As Integer = PictureBox1.Size.Height / 4
            Dim SF As New StringFormat
            SF.Alignment = StringAlignment.Center
            SF.LineAlignment = StringAlignment.Center
            For row As Integer = 0 To 3
                For col As Integer = 0 To 2
                    Dim rc As New Rectangle(col * width, row * height, width, height)
                    e.Graphics.DrawString(digits(row, col), Me.Font, Brushes.Black, rc, SF)
        End Sub
        Private Sub PictureBox1_Resize(sender As Object, e As System.EventArgs) Handles PictureBox1.Resize
        End Sub
    End Class

    Open in new window

    *As Number-1 has done, you may want to create your own font and use that instead of "Me.Font".

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video discusses moving either the default database or any database to a new volume.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now