Results 1 to 11 of 11

Thread: A better textbox control?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Posts
    123

    Question A better textbox control?

    Okay I an fairly new to VB in applications so please bare with me.

    Here is what i want to accomplish with some sort of textbox control, in order of importance:

    1. Multiline
    2. Top and side scroll bars
    3. No word wrap
    4. Tab key will tab the text, not go to next control on TabIndex
    5. Stuffing a string of text into the control will not cause it to lose any basic formatting (CrLf, tabs, spacing, etc)
    6. Ability to highlight keywords in some way (ala the VB6 IDE where they highlight in blue)

    1-4 is really a must, 5 is pretty much needed to, and 6 would be very nice.

    I have only tried the textbox and richtextbox control and they do not seem to accomplish what I want, especially #4.

    Help please!!!

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Points 1-3 is available for both regular TextBoxes as well as RichTextBoxes. A TextBox will not word-wrap if you have scrollbars set to 3-Both (and MultiLine set to True of course). With a RTB you have to set the RightMargin for it not to word-wrap. Actually it will but if you set the RightMargin to a high enough number the chances that someone will type such a long line of text will be very slim:
    VB Code:
    1. RichTextBox1.RightMargin = Screen.Width * 1000
    When it comes to point 4 you can use this code with both RTF and TextBox
    VB Code:
    1. Private Sub RichTextBox1_GotFocus()
    2.     Dim c As Control
    3.    
    4.     On Error Resume Next
    5.     For Each c In Me.Controls
    6.         c.TabStop = False
    7.     Next
    8. End Sub
    9.  
    10. Private Sub RichTextBox1_LostFocus()
    11.     Dim c As Control
    12.    
    13.     On Error Resume Next
    14.     For Each c In Me.Controls
    15.         c.TabStop = True
    16.     Next
    17. End Sub
    This will turn off the TabStop property for all the controls on the form when the textbox gets focus and turn it back on when it loses it. If the TextBox is the first control to get focus when your application loads you might want to turn it off in the Form_Load event as well since the GotFocus event will not be fired at start-up. If you're using a RTB box you can also use the following code to get the same effect:
    VB Code:
    1. Private Sub RichTextBox1_KeyDown(KeyCode As Integer, Shift As Integer)
    2.     If KeyCode = vbKeyTab Then
    3.         RichTextBox1.SelText = vbTab
    4.         KeyCode = 0
    5.     End If
    6. End Sub
    You can also use a RTB to highlight different words however searching for them and replacing the color is a slow progress.

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Oh forgot point number 5... Always use the SelText property to insert text.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Posts
    123
    thank you for the quality answers!

    as for #6 - so would it be a waste of time for me to write a function to search the RichTextBox1.SelText for keywords and change their color?

    what i am working on involves a SQL stored procedure editor capability so it would be really nice to color the SQL keywords such as Query Analyzer does.

    At first consideration it seems like I would need to parse out all the words in the textbox and then do a compare on each against a list of keywords. Are you saying it would be slow going to write the code, or slow execution?

    any thoughts?

    thanks

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    The execution will be slow. Especially when opening a large file. It could be OK to colorize code while the user is typing because you could simply just colorize one line at the time (the last line the user was editing, when the caret is moved to a new line). But when you open a large file you have to go through the whole thing and that can be very slow with a RTFBox.

  6. #6
    Addicted Member Xdream's Avatar
    Join Date
    Mar 2001
    Location
    Switzerland
    Posts
    194
    Hi

    you could use regular expressions for searching special chars or words in the text, they are really fast. Afterwards, you can colorise each match as you want

    regards

  7. #7
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    I posted a fast function to colorize text in the CodeBank.. shouldn't be to hard to change for your needs



    http://www.vbforums.com/showthread.p...hreadid=241611

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Posts
    123
    again thanks for your helpful answers

    joacim - i will color the text on the current line, good idea, how do i get the line of text on which the cursor is active?

    fox - that should work nicely with some modifications, thanks


    last question (i think) - Whats the difference between the .Text property and the .SelText property in a RTB?

  9. #9
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    .Text is the whole text in your textbox's memory.
    .SelText is the currently marked text (by the user).

    See also
    .SelStart
    .SelLength

    All .Sel* properties refer to the currently selected text,
    eg. you can select a text and change it's format...
    Try the following snippet on a default RTB, it'll colorize
    the part "chTex" from the content and then move the
    cursor to the very end:

    VB Code:
    1. With RichTextBox1
    2.    'Select text
    3.    .SelStart = 2
    4.    .SelLength = 5
    5.  
    6.    'Change format
    7.    .SelColor = 255
    8.    .SelBold = 1
    9.    
    10.    'Move cursor
    11.    .SelStart = Len(.Text)
    12. End With

  10. #10
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    btw: This code will get your line:

    VB Code:
    1. Public Sub GetLine()
    2.     Dim Pos As Long
    3.    
    4.     Dim LineStart As Long
    5.     Dim LineEnd As Long
    6.    
    7.     Dim Temp As String
    8.     Dim Token As String
    9.    
    10.     With RichTextBox1
    11.         'Setup
    12.         Token = vbNewLine
    13.        
    14.         'Get cursor position
    15.         Pos = .SelStart + 1
    16.         If Pos > Len(.Text) Then: Pos = Length
    17.        
    18.         'Get line start / end
    19.         LineStart = InStrRev(.Text, Token, Pos)
    20.         LineEnd = InStr(LineStart + 1, .Text, Token)
    21.        
    22.         'Check values
    23.         If LineStart = 0 Then
    24.             LineStart = 1
    25.         Else
    26.             LineStart = LineStart + Len(Token)
    27.         End If
    28.         If LineEnd = 0 Then
    29.             LineEnd = Len(.Text)
    30.         Else
    31.             LineEnd = LineEnd - 1
    32.         End If
    33.        
    34.         'Get line
    35.         Temp = Mid(.Text, LineStart, LineEnd - LineStart + 1)
    36.     End With
    37.    
    38.     'Show result
    39.     MsgBox Temp
    40. End Sub

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You can use this class I wrote a few years back.
    VB Code:
    1. Private rtx As CRtfEx
    2.  
    3. Private Sub Form_Load()
    4.     Set rtx = New CRtfEx
    5.     Set rtx.TextBox = RichTextBox1
    6. End Sub
    The class has a number of properties and methods Microsoft didn't thought VB developers needed. For example: GetLine will return the text of any line, just pass the zero based line number or -1 to get the text of the current line. I'll leave it up to you to explore it.
    Attached Files Attached Files

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