Results 1 to 17 of 17

Thread: MultiLine TextBox [RESOLVED]

  1. #1

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677

    MultiLine TextBox [RESOLVED]

    I have a TextBox that allows multiple lines. I need to be able to limit each line to only 30 characters. How can I achieve this? Thanks, Jeremy
    Last edited by JCScoobyRS; Mar 26th, 2003 at 03:16 PM.
    He who listens well, speaks well.

  2. #2
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    mltiline textboxes automatically wrap the text so that it doesnt go out to the right. if you use a fixed char width font in your text box you can size the textbox so that it only shows 30 chars per line. but i doubt this is a decent solution for you... other than that, you will have to insert vbCrLf characters every 30 chars. you can do this inside the _Change() event of your text box. first solution will keep your text free of CRLF sequences, 2nd solution will change the user input.
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  3. #3
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Originally posted by radum
    mltiline textboxes automatically wrap the text so that it doesnt go out to the right
    ...Unless you set the scrollbar property, that is!
    ~seaweed

  4. #4
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: MultiLine TextBox

    Originally posted by JCScoobyRS
    I have a TextBox that allows multiple lines. I need to be able to limit each line to only 30 characters. How can I achieve this? Thanks, Jeremy
    This code will do what you asked:

    VB Code:
    1. Option Explicit
    2.  
    3. 'Code by Kristopher Wilson
    4. 'Email: [email][email protected][/email]
    5. 'Web: [url]http://www.vbshelf.com[/url]
    6.  
    7. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
    8.   ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    9.  
    10. Private Const EM_LINEINDEX = &HBB
    11. Private Const EM_LINELENGTH = &HC1
    12.  
    13. Private Sub Text1_KeyPress(KeyAscii As Integer)
    14. Dim lLen As Long
    15.  
    16.   lLen = SendMessage(Text1.hwnd, EM_LINELENGTH, EM_LINEINDEX, 0&)
    17.  
    18.   If lLen = 30 And KeyAscii <> vbKeyReturn Then KeyAscii = 0
    19. End Sub
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    This may sound dumb but how do I use your code? I have tried to copy and paste into my Form's Source but I got an error. Does this have to be in a module? Thanks, Jeremy
    He who listens well, speaks well.

  6. #6
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Worked perfectly for me with no modifications, in the Form's code window. What error did you get?
    ~seaweed

  7. #7
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Re: MultiLine TextBox

    VB Code:
    1. Option Explicit
    2.  
    3. 'Code by Kristopher Wilson
    4. 'Email: [email][email protected][/email]
    5. 'Web: [url]http://www.vbshelf.com[/url]
    6.  
    7. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
    8.   ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    9.  
    10. Private Const EM_LINEINDEX = &HBB
    11. Private Const EM_LINELENGTH = &HC1

    Put the above portion at the very top of the form code window. If you already have some declarations there, just fit it in accordingly.

    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. Dim lLen As Long
    3.  
    4.   lLen = SendMessage(Text1.hwnd, EM_LINELENGTH, EM_LINEINDEX, 0&)
    5.  
    6.   If lLen = 30 And KeyAscii <> vbKeyReturn Then KeyAscii = 0
    7. End Sub

    Put the above portion in with the rest of your code and change all occurances of Text1 to the name of your TextBox.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    VB Code:
    1. Compile Error:
    2.  
    3. Only comments may appear after End Sub, End Function or End Property

    This code is what's highlighted:

    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
    2.   ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

    Probably something easy, I just have been working on this diligently and I overlook somethings. Thanks, Jeremy
    He who listens well, speaks well.

  9. #9

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    DUH!!! Hobo, that fixed it. Let's give it a try and I'll be back with the results. Thanks, Jeremy
    He who listens well, speaks well.

  10. #10
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    You might want to update the code to this:

    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. Dim lLen As Long
    3.  
    4.   lLen = SendMessage(Text1.hwnd, EM_LINELENGTH, EM_LINEINDEX, 0&)
    5.  
    6.   If lLen = 10 And KeyAscii <> vbKeyReturn And KeyAscii <> vbKeyBack Then KeyAscii = 0
    7. End Sub

    This makes it so backspace will work when there's 10 characters.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  11. #11

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    Very Nice...it works. I didn't doubt it but I am very impressed. I need to learn that kind of stuff. Thanks, Jeremy
    He who listens well, speaks well.

  12. #12

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    Now, the next task. Is there a way to get the value, as a string, from each line where each line is a new value? Or is there a way to reference the line through code:

    txtVariable = TextBox.Line1.Text

    Thanks, Jeremy
    He who listens well, speaks well.

  13. #13
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    This would be the easiest way:

    VB Code:
    1. Private Function GetLine(txtBox As TextBox, iLine As Integer)
    2. Dim strLines() As String
    3.  
    4.   strLines() = Split(txtBox.Text, vbCrLf)
    5.   GetLine = strLines(iLine - 1)
    6. End Function

    To use it, just do:

    VB Code:
    1. Msgbox GetLine(Text1, 2)

    Where Text1 is the name of the box, and 2 is the line number you want. Note: you could assign this to a variable instead of MsgBoxing it.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  14. #14
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Check out the attachment. It does what you want and it has some other nice features.
    Attached Files Attached Files


    Has someone helped you? Then you can Rate their helpful post.

  15. #15

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    Ok...let's say that for each line in the TextBox, I have to do an insert into a database. How should I go about doing this? Thanks, Jeremy
    He who listens well, speaks well.

  16. #16
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Something like:

    VB Code:
    1. Private Sub DoInsert(txtBox As TextBox)
    2. Dim strLines() As String, i As Integer
    3.  
    4.   strLines() = Split(txtBox.Text, vbCrLf)
    5.  
    6.   For i = 0 To Ubound(strLines)
    7.     'code to insert line here
    8.     'line can be accessed by strLines(i)
    9.   Next
    10. End Function


    Then just call the subroutine when you need it:

    VB Code:
    1. DoInsert Text1
    My evil laugh has a squeak in it.

    kristopherwilson.com

  17. #17

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    That works...what about pulling data from a database. If I retrieve 3 rows, I need to have 3 lines in the TextBox with the corresponding Text in that row. Can you help, again? Thanks for your continued support and help, Jeremy
    He who listens well, speaks well.

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