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.
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:
RichTextBox1.RightMargin = Screen.Width * 1000
When it comes to point 4 you can use this code with both RTF and TextBox
VB Code:
Private Sub RichTextBox1_GotFocus()
Dim c As Control
On Error Resume Next
For Each c In Me.Controls
c.TabStop = False
Next
End Sub
Private Sub RichTextBox1_LostFocus()
Dim c As Control
On Error Resume Next
For Each c In Me.Controls
c.TabStop = True
Next
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:
Private Sub RichTextBox1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyTab Then
RichTextBox1.SelText = vbTab
KeyCode = 0
End If
End Sub
You can also use a RTB to highlight different words however searching for them and replacing the color is a slow progress.
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?
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.
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
.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:
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.