Page.MeasureText Correct Usage

Posted by: jallen on 13 March 2020, 6:48 am EST

    • Post Options:
    • Link

    Posted 13 March 2020, 6:48 am EST

    I am hoping to display different text in a section report text box if the text is too long for the text box.

    Is this the correct way to do it?

    Public Class SectionReport1
        Private Counter As Integer = 0
        Dim Text1 As String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas aliquet hendrerit tortor"
    
        Private Sub SectionReport1_DataInitialize(sender As Object, e As EventArgs) Handles Me.DataInitialize
            Fields.Add("Test")
        End Sub
    
        Private Sub Detail_Format(sender As Object, e As EventArgs) Handles Detail.Format
            If CurrentPage.MeasureText(Text1).Width > TextBox1.Width Then
                TextBox1.Text = "TOO LONG"
            End If
        End Sub
    
        Private Sub SectionReport1_FetchData(sender As Object, eArgs As FetchEventArgs) Handles Me.FetchData
            Counter += 1
            If Counter = 10 Then
                eArgs.EOF = True
                Return
            End If
            Fields("Test").Value = Text1
            eArgs.EOF = False
        End Sub
    End Class
    

    The code above is not accurate enough, since the ‘TOO LONG’ message does not display even though the text is truncated.

    John

  • Posted 16 March 2020, 9:34 pm EST

    Hello,

    Could you please try with the following line of code:

    
     Private Counter As Integer = 0
        Dim Text1 As String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas aliquet hendrerit tortor"
    
        Private Sub Detail_Format(sender As Object, e As EventArgs) Handles Detail.Format
            If CurrentPage.MeasureText(Text1, TextBox1.Font).Width > TextBox1.Width Then
                TextBox1.Text = "TOO LONG"
            End If
        End Sub
    
        Private Sub SectionReport1_DataInitialize(sender As Object, e As EventArgs) Handles MyBase.DataInitialize
            Fields.Add("Test")
        End Sub
    
        Private Sub SectionReport1_FetchData(sender As Object, eArgs As FetchEventArgs) Handles MyBase.FetchData
            Counter += 1
            If Counter = 10 Then
                eArgs.EOF = True
                Return
            End If
            Fields("Test").Value = Text1
            eArgs.EOF = False
        End Sub
    
    

    If the issue is still reproducible, then could you please modify the attached sample so that I can reproduce the issue at my end.

    Thanks,

    Mohit

    SectionReportApplication53.zip

  • Posted 16 March 2020, 11:58 pm EST

    That is good, thanks!

    John

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels