Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27020

VS 2010 Measuring the size of contents of a richtextbox to equal one printed page.

$
0
0
Just wondering out there if there is a better way to measure the contents of a richtextbox so you know how many printed pages there will be. What I really want is for a message to appear saying "You have reached the end of page 1." or something like that. I have it sorta figured out but all text has to be the same size. Does not really matter what font or what size, it just has to be one size. I would really like to be able to make a word or two a larger size but that throws everything off with the below code. I have searched and have not found anything close.

Any help would be appreciated

Thanks

Pat


vb.net Code:
  1. 'Speller1 is my richtextbox
  2.             Dim sz As Integer = SpellerFntSize
  3.             Dim NoLines As Integer = Speller1.Lines.Count
  4.             Dim txt As String = Trim(Speller1.Text)
  5.  
  6.             Dim txtA() As String = txt.Split(" ")
  7.             Dim WrdCnt As Integer = txtA.GetUpperBound(0) + 1
  8.             Dim BoxWidth As String = ProposalSettings(9)
  9.             Dim BoxWidthD As Decimal = 765
  10.             If Not IsNumeric(BoxWidth) Then
  11.                 BoxWidth = 765
  12.                 BoxWidthD = BoxWidth
  13.             Else
  14.                 BoxWidthD = BoxWidth
  15.             End If
  16.  
  17.             Dim dpiX As Single
  18.             Dim dpiY As Single
  19.             Dim dpiR As Single
  20.             Dim dpiN As Single = 1
  21.  
  22.             Using g As Graphics = Me.CreateGraphics()
  23.                 dpiX = g.DpiX
  24.                 dpiY = g.DpiY
  25.             End Using
  26.             'seeing if user is using large fonts
  27.             If dpiX <> 96 Then
  28.                 dpiR = 96
  29.                 dpiN = dpiX / dpiR
  30.             End If
  31.            
  32.             'BoxWidthD is the width of the box
  33.             BoxWidthD = BoxWidthD * dpiN
  34.  
  35.             Dim currentFont As System.Drawing.Font = Speller1.SelectionFont
  36.             Dim FntSize As Integer = currentFont.Size
  37.             Dim realnumber As Integer = 0 'NoLines
  38.  
  39.             For i = 0 To NoLines - 1
  40.                 Dim text1 As String = Speller1.Lines(i).Trim
  41.                 'arialBold is not really the font, its just a var that I used to get the name of the selected font
  42.                 Dim arialBold As New Font(Speller1.SelectionFont.OriginalFontName, sz)
  43.                 Dim textSize As Size = TextRenderer.MeasureText(text1, arialBold)
  44.                 Dim a As Decimal = 0
  45.                 Dim b As Decimal = textSize.Width
  46.                 If b <> 0 Then
  47.                     a = b / BoxWidthD
  48.                     a = Math.Ceiling(a)
  49.                     If a > 1 Then
  50.                         realnumber = realnumber + a
  51.                     Else
  52.                         realnumber = realnumber + 1
  53.                     End If
  54.                 Else
  55.                     realnumber = realnumber + 1
  56.                 End If
  57.             Next
  58.             'amount is the number of lines allowed for a specific font size taken from a table I made
  59.             TsLinsRm.Text = "Lines Remaining: " & Amount - realnumber
  60.             Dim LinesRm As Integer = CInt(Amount - realnumber)
  61.             LiveCntAmount = LinesRm
  62.             If LinesRm < 3 Then
  63.                 TsLinsRm.ForeColor = Color.Maroon
  64.             Else
  65.                 TsLinsRm.ForeColor = Color.DarkSlateGray
  66.             End If
  67.  
  68.  
  69.         Catch ex As Exception
  70.  
  71.         End Try

Viewing all articles
Browse latest Browse all 27020

Trending Articles