Results 1 to 31 of 31

Thread: [RESOLVED] total lines in a textbox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Resolved [RESOLVED] total lines in a textbox

    I need to know how to determine the total number of lines in a multi line textbox.


    I have looked thru the properties and I cannot come up with a solution.

  2. #2
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: total lines in a textbox

    Code:
    Dim s As String = TextBox1.Text
    Using sw As New StreamWriter(My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData  & "\someTempFileIMade.txt")
        sw.Write(s)
    End Using
    
    Dim tempInteger As Integer = 0
    Using sr As New StreamReader("Same as above")
        Do Until sr.EndOfStream
            sr.ReadLine()
            tempInteger += 1
        Loop
    End Using
    tempInteger is how many lines there is.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: total lines in a textbox

    I will try it out.

    if there are any other ways to do the same function I wold like to know the other ways also.

    thanks.

  4. #4
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: total lines in a textbox

    If your worried about size, add the code to a function...

    Code:
    Public Function getTextBoxLines(ByVal text As String) As Integer
    Using sw As New StreamWriter(My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData  & "\someTempFileIMade.txt")
        sw.Write(text)
    End Using
    
    Dim tempInteger As Integer = 0
    Using sr As New StreamReader("Same as above")
        Do Until sr.EndOfStream
            sr.ReadLine()
            tempInteger += 1
        Loop
    End Using
    Return tempInteger
    End Function
    MsgBox(getTextBoxLines(TextBox1.Text))

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: total lines in a textbox

    I am getting the message that "streamreader" is not defined.

  6. #6
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: total lines in a textbox

    Are you guys serious?!

    textBox1.Lines.Length


  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: total lines in a textbox

    Textboxes has a Lines property that returns a string array, containing each line in the textbox. Using this array, you can easily find out how many lines there are in the textbox, considering all arrays have a Length property.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: total lines in a textbox

    I have looked thru the properties and I cannot come up with a solution.
    I didn't have VS open...

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: total lines in a textbox

    I have the code working that Icyculyr has posted, but, it does not count a line that is blank and I need to count the blank lines also.

    any ideas?

  10. #10
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: total lines in a textbox

    textBox1.Lines.Length

    *cough*

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: total lines in a textbox

    I just finished trying textBox1.Lines.Length.


    thanks a lot! it works just like I need.

    Iam going to keep the other code because it works in a different way that I may need sometime.

    thanks again!!!

  12. #12
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: total lines in a textbox

    Remember to mark your thread as resolved.

    Thread Tools -> Mark Thread as Resolved.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: total lines in a textbox

    I have a problem, it is not counted as a line unless the user hits the enter key.

    if the user types and allows the text box to wrap automatically then the lines are not counted properly.

    any ideas?

  14. #14
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: total lines in a textbox

    Do you have the WordWrap property set to true?

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: total lines in a textbox

    yes.

  16. #16
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: total lines in a textbox

    What to you mean by, "allows the text box to wrap automatically?"

    Certainly if WordWrap is set to true, you cannot allow it to wrap automatically, yes?

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: total lines in a textbox

    if the user continues to type when he\she reaches the far right side of the textbox the text will jump to the second line, but, the line count does not change unless the user hits the enter key before the cursor jumps to the second line.

  18. #18
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: total lines in a textbox

    That's because it is not a new line, it appears to be a new line since the textbox wraps it, but it's not...

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: total lines in a textbox

    can this be fixed?

  20. #20
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: total lines in a textbox

    I highly doubt that... even if it could, I would say it would take a long time to make those kind of modifications...

    but if I was going to, I would think of TRYING to get the y position of each character (as it will be the same on the first line) but as soon as it moves to the second, it will be different... but I am not sure if that is possible.

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: total lines in a textbox

    ok. thanks

  22. #22
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [RESOLVED] total lines in a textbox

    Edit: Agh, there's 3 new replies already?! These boards move too fast =(

    Ohhhhhhhh alright. Yea, that definitely is a problem.

    This is probably due to the fact that since the Length property is an array of integers. When the textbox is created when the program runs, the arrays index is set to 0 and everything the user types goes in index 0 of the array. Then, when the user presses enter, the index is increased by one, so everything the user types now is in the index 1.

    How to fix this I haven't the foggiest. I suppose you could handle the TextChanged event and simulate the key press of the Enter key when the condi-actually, that doesn't sound like much of a good idea.

    Maybe you should just use Icyculyr's code, if it does not produce the same error, that is.

  23. #23

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: [RESOLVED] total lines in a textbox

    his code produces the same effect.

  24. #24
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [RESOLVED] total lines in a textbox

    Yea, I thought it would. It all makes sense if you think about it. It is just wrapping the current line (Icyculyr already said this). It's not actually a new line. What do you need this for anyways?

  25. #25

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: [RESOLVED] total lines in a textbox

    I have a database field that I want to limit to 300 charactors & spaces but also limit it to 6 lines. this allows a way to print the data to paper easily since I will always know how many lines to allow for.

    here is the code for VB 6 that worked perfect.


    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long


    dim lCount as integer


    lCount = SendMessage(StatusNotes.hwnd, EM_GETLINECOUNT, 0, ByVal 0)




    'lCount is the number of lines.

  26. #26
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [RESOLVED] total lines in a textbox

    You can do call Win32 API functions in VB .Net, can you not? I know you can do it in C#.

    Imports System.Runtime.InteropServices

    Looks like you can o_O

    Also looks like all hope is not lost!

  27. #27

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: [RESOLVED] total lines in a textbox

    I would think the code I used for VB 6 could be converted to VB 2008 XE.


    I could write a small program using the function and then run it thru the VB 2008 VB XE converter.

  28. #28
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [RESOLVED] total lines in a textbox

    vb.net Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class Form1
    4.     Private Const EM_GETLINECOUNT As Integer = &HBA
    5.     Declare Auto Function SendMessage Lib "user32" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    6.  
    7.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    8.         Dim lineCount As Integer = SendMessage(TextBox1.Handle, EM_GETLINECOUNT, 0, 0)
    9.     End Sub
    10. End Class

    We win!

  29. #29

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: [RESOLVED] total lines in a textbox

    thanks!!! it works just like in VB 6.


    thanks again!!!

  30. #30
    New Member
    Join Date
    Dec 2007
    Posts
    1

    Could anyone help me?

    I'm new to VB and need help on this project? Could anyone of you help?

    Programming 1 - Final

    Create a multi-form project that on one form the user enters the following information:
    Form 1: Customer Entry
    Customer Number: should be 5 digits and unique
    Customer Name:
    Customer Street Address:
    City:
    State:
    Zip:
    Customer Telephone:
    Discount Percentage: .85 represents the customer is charged 85% of total sale.

    Form 2: Order Entry
    Customer Number:
    Item Number:
    Item Description:
    Price of Item:
    Quantity Ordered
    Delivery Date:
    Shipping Cost:
    Total Cost w/out tax:
    Total Cost with Tax:

    Form 3: Invoice:
    This form should display one invoice per customer displaying all the Orders entered in the order entry and give the user the ability to specifically lookup one customer number and display all orders from form 2.

    The invoice should display all info from form 1 and all info from form 2 per order, and a total amount or the overall orders.

    New things you will need for this project:
    List boxes
    Multiple Forms
    Arrays
    Code Module
    User Defined Types


    Grading:
    Form 1 = 15%
    Form 2 = 15%
    Form 3 = 15%
    Interactivity = 55%

    Errors – 5%
    Efficiency – 5%
    Use of Arrays + 5%
    Code Module + 5%
    Comments – 10%
    List Boxes + 5%

  31. #31

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Location
    ohio, usa
    Posts
    719

    Re: [RESOLVED] total lines in a textbox

    You need to post this on a new thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width