Vector
Vector

Reputation: 3235

vb net set font size based on string length

I am trying to set the font size based on the length of three strings of text
The limiting factor is the print area which is the Avery Address Label 1" X 2-5/8"
The three strings of data to be printed are represented by Global variables gv
Because the length of these three strings can vary based on the data my code to test the length is trying to compare 3 elements to make a decision on the font size. If one set is too long then it dictates the font size
The first test works if all the data is real long
The second test works but is not able to permit the third test to function. Posted CODE will make this a lot clearer. I hope

I am not sure if the failure is my test code construction as I have tried numerous designs
I have looked at this concept but feel it is not workable for my issue

StringSize = e.Graphics.MeasureString(strS, myFont)

Because I am dealing with 3 elements that are highly variable I feel my test code is not a workable concept

If a better solution can help solve this issue suggestions are desired
OR can my Test Code be fixed to work?

Test Code

    Dim FL As Integer = gv_FN.Length + gv_LN.Length

    If gv_AD.Length >= 24 Or FL >= 26 Or gv_CT.Length >= 16 Then
        fontSIZE = 11
    End If
    If gv_AD.Length = 23 Or gv_AD.Length = 22 Or gv_AD.Length = 21 Or gv_AD.Length = 20 Or gv_AD.Length = 19 _
        Or FL = 25 Or FL = 24 Or FL = 23 Or FL = 22 Or FL = 21 _
        Or gv_CT.Length = 15 Or gv_CT.Length = 14 Or gv_CT.Length = 13 Or gv_CT.Length = 12 Then
        fontSIZE = 13
    End If
    If gv_AD.Length <= 18 Or FL <= 20 Or gv_CT.Length <= 11 Then
        fontSIZE = 15
    End If
    tbInfo.Text = fontSIZE.ToString
    Dim labelFont As Font = New Font("Times New Roman", fontSIZE, FontStyle.Bold)

This is an EDIT with the FIX which as @Hursey suggested to use ElseIf

        If gv_AD.Length >= 24 Or FL >= 26 Or gv_CT.Length >= 16 Then
        fontSIZE = 11

    ElseIf gv_AD.Length = 23 Or gv_AD.Length = 22 Or gv_AD.Length = 21 Or gv_AD.Length = 20 Or gv_AD.Length = 19 _
     Or FL = 25 Or FL = 24 Or FL = 23 Or FL = 22 Or FL = 21 _
     Or gv_CT.Length = 15 Or gv_CT.Length = 14 Or gv_CT.Length = 13 Or gv_CT.Length = 12 Then
        fontSIZE = 13
    Else
        fontSIZE = 15
    End If
    tbInfo.Text = fontSIZE.ToString
    Dim labelFont As Font = New Font("Times New Roman", fontSIZE, FontStyle.Bold) 'Times New Roman

Upvotes: 1

Views: 1205

Answers (2)

Mary
Mary

Reputation: 15091

I hope that the 3 lines of the label are all the same font size. Get the longest line and test just that to get the font size.

Resolve the string lengths once; then use the local variables. Using the .Max function returns the longest line.

Private gv_FN As String
Private gv_LN As String
Private gv_AD As String
Private gv_CT As String
Private Sub OP2Code()
    Dim fontSIZE As Integer
    Dim FirstLine As Integer = gv_FN.Length + gv_LN.Length
    Dim SecondLine = gv_AD.Length
    Dim ThirdLine = gv_CT.Length
    Dim Longest = (New List(Of Integer)({FirstLine, SecondLine, ThirdLine})).Max
    Select Case Longest
        Case >= 26
            fontSIZE = 11
        Case >= 21
            fontSIZE = 13
        Case <= 20
            fontSIZE = 15
    End Select
    tbInfo.Text = fontSIZE.ToString
    Dim labelFont As Font = New Font("Times New Roman", fontSIZE, FontStyle.Bold)
End Sub

Upvotes: 1

James_Duh
James_Duh

Reputation: 1371

You could just total the three string elements then you only need to test one value
The test values will be very different than your current test values
I put together some abstract numbers so you will need your own set

    Dim FL As Integer = gv_FN.Length + gv_LN.Length
    Dim AD As Integer = gv_AD.Length
    Dim CT As Integer = gv_CT.Length
    Dim tot As Integer = FL + AD + CT
    If tot >= 54 Then
        fontSIZE = 12
    End If
    If tot = 53 Or tot = 52 Or tot = 51 Or tot = 50 Or tot = 49 Or tot = 48 _
        Or tot = 47 Or tot = 46 Or tot = 45 Or tot = 44 Or tot = 43 Then
        fontSIZE = 13
    End If
    If tot <= 42 Then
        fontSIZE = 15
    End If

Upvotes: 1

Related Questions