1

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
5
  • 1
    What are gv_AD, gv_CT, gv_FN and gv_LN? Looking at you logic it seems quite feasible that latter conditions are overriding the first one(s). Perhaps your answer might be "ElseIf" rather than separate operations Commented Sep 29, 2020 at 22:39
  • @Hursey Poor Naming convention they are global variables AD = Address FN & LN are First & Last Names and CT is City Max values are controlled in the data entry Text Boxes Let me try the code with Elseif Commented Sep 29, 2020 at 22:43
  • @Hursey Some improvement BUT NOT reaching the last Test where font size is set to 15 it might be the Or Test Values in the second Test Could just try Last ElseIf with NO parameters Commented Sep 29, 2020 at 22:59
  • Got some test values that you expect to go into the 3rd if? Maybe update the question with your new code Commented Sep 29, 2020 at 23:02
  • @Hursey The ElseIF Solved the Issue I will Update the Question Thanks for the Help Commented Sep 29, 2020 at 23:19

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Learning to code is like the time I tried to learn to play Bridge very slow at times I am sure you can appreciate that Using List(of is the real KEY here It eliminates all the ELSEIF and IF statements I cling to for testing variables Lesson Learned
@Vector I am still slowly learning both. I could have used an array but I have moved to list for so many things I jus do it by habit. Currently learning weak twos in bridge. :-)
1

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

2 Comments

Looks promising will take it for a spin
How about using OrElse to short circuit the If statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.