Resolved How can I carelate between string length and form size?

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
I want to bind longest string length which is an array element to a form size. I will find longest string length then link up its length with form size. Form size belongs to string length, I mean. But there is another dilemma that string font size how occupy a place on the form its directly effect size of form.
 
Last edited:
Hi and what?

That is one heck of a statement for someone to get their head round.

Assuming that what you mean is to make the longest string in an Array be displayed on a Form in a particular Font then you can use the Graphics palette of the Form to work out the length of a String in that Font and then Re-Size the Form as needed. Here is a quick example of what I mean:-

Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
  Dim myString As String = "The Quick Brown Fox Jumped Over The Fence"
  Dim myFont As New Font("Arial", 28)
 
  Dim myStringLength As SizeF = e.Graphics.MeasureString(myString, myFont)
  Me.Width = CInt(myStringLength.Width)
  e.Graphics.DrawString(myString, myFont, Brushes.Black, 0, 0)
End Sub


If that is anywhere near what you are trying to achieve then you must know that there is a major flaw in the above code using variable width Fonts but, until I know any better, I will leave it fot you to work out what that flaw may be.

Hope that helps.

Cheers,

Ian
 
Thanks a lot, spot on! Now I will find longest string in an Array then make size of form as the longest one.
 
Wow,

I see that my ESP is spot on today:wink: but keep an eye on that flaw that I mentioned. To demonstrate, see what happens with these strings which are of EQUAL Length:-

"iiiii"
"qqqqq"

Cheers,

Ian
 
But I need something more than that after I focused enough that the paint event show only one string.. I need to show 6 string. now another issue just come out :)
 
Sorry Socarsky,

I guessed a bit on your first post but I have no idea what you mean with your last post.

I appreciate that English is not your first language but you need to try and re-word that last post a bit better for us to help you further.

Cheers,

Ian
 
There are 6 strings must appear on the form, then the issue starts with the paint event because it accepts only one.
Ok another case that if I handle then the issue will resolve.
How can I get without error in my sub below?
Dim myStringLength As SizeF = e.Graphics.MeasureString(myString, myFont)
        Me.Width = CInt(myStringLength.Width)


poppp.jpg
 
I solved the matter, thanks Ian

 Private Sub MySub()
        Dim labelsLength As New List(Of Integer)
        With labelsLength
            .Add(Label1.Text.Length)
            .Add(Label2.Text.Length)
            .Add(Label3.Text.Length)
            .Add(Label4.Text.Length)
            .Add(Label5.Text.Length)
        End With
        labelsLength.Sort()
        Dim nLongestStr As Integer = labelsLength.Item(4)
        Dim myString As String = Label2.Text
        Dim myFont As New Font("Arial", 14)
        Dim myStringLength As Size = TextRenderer.MeasureText(myString, myFont)
        Me.Width = CInt(myStringLength.Width)
    End Sub
 
Back
Top