CENTER txt in picturebox...

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

CENTER txt in picturebox...

Post by sal21 »

Code: Select all

Option Explicit
Private Sub Command1_Click()

  Call DrawText
  
End Sub
Private Sub DrawText()

    Const SAMPLE_TEXT As String = "0123456789"

    ' Draw on the picture.
    Me.Picture1.Font.Name = "ARIAL"
    Me.Picture1.Font.Size = 24
    Me.Picture1.Font.Bold = True
    Me.Picture1.ForeColor = vbBlack

    ' Center the text horizontally at the top of the picture.
    Me.Picture1.CurrentX = (Me.Picture1.ScaleWidth - Me.Picture1.TextWidth(SAMPLE_TEXT)) / 2
    Me.Picture1.CurrentY = 0

    Me.Picture1.AutoRedraw = True
    Me.Picture1.Print SAMPLE_TEXT

    ' Make the text a permanent part of the image.
    ' This is important if you later need to copy
    ' the picture to another control or the Printer.
    Me.Picture1.Picture = Me.Picture1.Image

End Sub
but the text is cetered to the top of picturebox.

i need to center text in orizontal and vertical, possible?
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: CENTER txt in picturebox...

Post by HansV »

Does this work?

Code: Select all

    Me.Picture1.CurrentY = (Me.Picture1.ScaleHeight - Me.Picture1.TextHeight(SAMPLE_TEXT)) / 2
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

Re: CENTER txt in picturebox...

Post by sal21 »

HansV wrote:
17 Mar 2021, 16:04
Does this work?

Code: Select all

    Me.Picture1.CurrentY = (Me.Picture1.ScaleHeight - Me.Picture1.TextHeight(SAMPLE_TEXT)) / 2
:clapping: :clapping: :clapping: :clapping:

HUMMMM...
i use this code as function...
but how to clear the old text, before to insert a new txt?

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: CENTER txt in picturebox...

Post by HansV »

If you look at the line for CurrentX, you could easily have modified the line for CurrentY!

You cannot remove text written by the Print method of the PictureBox.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4355
Joined: 26 Apr 2010, 17:36

Re: CENTER txt in picturebox...

Post by sal21 »

GOOGLING...
correct:

Me.Picture1 = Nothing
Me.Picture1.Cls

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: CENTER txt in picturebox...

Post by HansV »

Yes, you can do that, but that will clear the entire picturebox.
Best wishes,
Hans