my text box have multi lines i want to count those lines and show it in form caption.

spoz i have 12 lines in text1.text i want show it like this

Sockz-12

Recommended Answers

All 3 Replies

1 Textbox, 1 Button. Result showing in form caption :

Private Sub Command1_Click()
    Dim TextLines As Variant
    TextLines = Split(Text1, vbNewLine)
    Form1.Caption = "Sockz-" & (UBound(TextLines) + 1)
End Sub
commented: agree +13
commented: agree +2
commented: :D +1

That's fine, as long as you have explicit line breaks on every line in your text.

If you have a multi-line text box without horizontal scroll (a.k.a. "Word Wrap"), it gets way more complicated. You have to use some Windows function calls to get the text metrics for your textbox, use the average character width and height values to calculate how wide a given line is to figure out how many lines you have for the font that your text box is using. Not for the faint of heart.

However, at the risk of making things WAY worse, there is a handy control called a "RichTextBox" that implements a method "GetLineFromChar". You can put a RichTextBox control (hidden, of course) with the same dimensions as your text box on your form, copy your text into the hidden RichTextBox, then use that method to get the line number you need. Then your code would look like this:

Private Sub Command2_Click()
    Dim x As Integer
    Me.RichTextBox1.Text = Me.Text1.Text
    x = Me.RichTextBox1.GetLineFromChar(Len(Me.RichTextBox1.Text)) + 1
    Form1.Caption = "Sockz-" & Str(x)
End Sub

Granted, it's not pretty, and it does require an extra component when you deploy, but it works.

Oh, and by the way...the GetLineFromChar starts counting from line 0 (that's why you have to add 1).

Hope this is more useful than ugly. Good luck!

i never found a site good for new learners like me like this site, thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.