How would I have each line numbered in a RichTextBox?
Printable View
How would I have each line numbered in a RichTextBox?
Try something like this:
Code:Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
Private Const EM_GETLINE = &HC4
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1
Private Sub Command1_Click()
Dim lngCount As Long
Dim lngLineIndex As Long
Dim lngLength As Long
Dim strBuffer As String
Dim strRichText As String
Dim i As Integer
'Get Line count
lngCount = SendMessage(RichTextBox1.hwnd, EM_GETLINECOUNT, 0, 0)
With RichTextBox1
For i = 0 To lngCount - 1
'Get line index
lngLineIndex = SendMessage(.hwnd, EM_LINEINDEX, i, 0)
'get line length
lngLength = SendMessage(.hwnd, EM_LINELENGTH, lngLineIndex, 0)
'resize buffer
strBuffer = Space(lngLength)
'get line text
Call SendMessageStr(.hwnd, EM_GETLINE, i, ByVal strBuffer)
'Number each line
strRichText = strRichText & CStr(i + 1) & " " & strBuffer & vbCrLf
Next
'rewrite numbered text in RichTextBox
.Text = strRichText
End With
End Sub
Private Sub Form_Load()
With RichTextBox1
.Text = "line one" & vbCrLf
.Text = .Text & "line two" & vbCrLf
.Text = .Text & "line three"
End With
End Sub
Thank you. I tried that and it is almost exactly what I'm looking for. I can work with that but what I really need is used sometimes in HTML editors like CuteHTML, it shows the line # for each line with text on it, but when you save the file it doesn't save the #s. Does that make sense to anyone? Sorry if I explained it wrong.
Open "c:\afile.txt" For Output As #1
Dim myline
myline = richtextbox1.Text
Write #1, myline
Close
produced this
"1 line one
2 line two
3 line three
"
I'm sorry, but I don't understand that. I just put your code into my form under the
Private Sub form_load()
Open "c:\afile.txt" For Output As #1
Dim myline
myline = richtextbox1.Text
Write #1, myline
Close
End Sub
and it did nothing. Obviously a user error, but I don't understand what you are saying works?
I think this is what you are looking for.
I am assuming that the format for the textbox is
#1 Line ones text
#2 Line twos text
so there is always a space after the line number.
Hope it helps. ;)Code:Private Sub mySaveFile(strText As String)
Dim ipCrlf1 As Long, ipCrLf2 As Long 'new line pointers
Dim ipSp1 As Long 'spce pointer
Dim strLine As String 'line of text
Open "myFile.Txt" For Output As #1
ipCrlf1 = 1: ipCrLf2 = 1
'break up on line feeds
Do
ipSp1 = 1
'find a line feed
ipCrLf2 = InStr(ipCrlf1, strText, vbCrLf)
If ipCrLf2 = 0 Then
'if there isn't one then set the length to the string
ipCrLf2 = Len(strText) + 1
End If
'get the line of text from the string
strLine = Mid$(strText, ipCrlf1, ipCrLf2 - ipCrlf1)
'find the first space character.
ipSp1 = InStr(1, strLine, " ")
If ipSp1 = 0 Then
ipSp1 = 1
Else
ipSp1 = ipSp1 + 1
End If
'remove the line number
strLine = Mid$(strLine, ipSp1, Len(strLine))
ipCrlf1 = ipCrLf2 + 2
Print #1, strLine
Loop Until ipCrLf2 >= Len(strText)
Close #1
End Sub
...that's cause I put it in the
command button after the line numbers
were added to the text box...
in the form load you haven't added
the numbers yet...
yes it works,,,I tested it and wouldn't
say it did if I didn't try it first...
the output was just a cut and paste from
the file I created and wrote to.
Have fun....
I don't want to sound stupid but I don't beleive I have any other options here.
What is Private Sub mySaveFile(strText as String)?
Is that a command button? I would have to have something named mySaveFile on my form for this to work right?
I made a new form to try this. It has a command button which I named mySaveFile and a Rich Text Box called RichTextBox1. When I run it it does nothing. I know I'm doing something wrong, but I am very new to VB and could use any help you can offer. Thanks.
no...it is a private sub that will save the file for you.
if you start a new project and add a richtextbox1 and
a command button (Command1) to it.
then copy and paste the code Serge gave you into your applicaion. Here it is with the little piece I added to
save your file to afile.txt on your c drive..
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
Private Const EM_GETLINE = &HC4
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1
Private Sub Command1_Click()
Dim lngCount As Long
Dim lngLineIndex As Long
Dim lngLength As Long
Dim strBuffer As String
Dim strRichText As String
Dim i As Integer
'Get Line count
lngCount = SendMessage(RichTextBox1.hwnd, EM_GETLINECOUNT, 0, 0)
With RichTextBox1
For i = 0 To lngCount - 1
'Get line index
lngLineIndex = SendMessage(.hwnd, EM_LINEINDEX, i, 0)
'get line length
lngLength = SendMessage(.hwnd, EM_LINELENGTH, lngLineIndex, 0)
'resize buffer
strBuffer = Space(lngLength)
'get line text
Call SendMessageStr(.hwnd, EM_GETLINE, i, ByVal strBuffer)
'Number each line
strRichText = strRichText & CStr(i + 1) & " " & strBuffer & vbCrLf
Next
'rewrite numbered text in RichTextBox
.Text = strRichText
End With
End Sub
Private Sub Form_Load()
With RichTextBox1
.Text = "line one" & vbCrLf
.Text = .Text & "line two" & vbCrLf
.Text = .Text & "line three"
End With
Open "c:\afile.txt" For Output As #1
Dim myline
myline = richtextbox1.Text
Write #1, myline
Close
End Sub
..to use mysavefile sub do the same
project with richtextbox1 and command1
add all this code to the project and run it'adding line numbrs to the front of text in a rich text box
'''you have to call the sub ...see the code in the
end of command1 code
lots of luck....
''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'copy all this into your code section
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
Private Const EM_GETLINE = &HC4
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1
Private Sub Command1_Click()
Dim lngCount As Long
Dim lngLineIndex As Long
Dim lngLength As Long
Dim strBuffer As String
Dim strRichText As String
Dim i As Integer
'Get Line count
lngCount = SendMessage(richtextbox1.hwnd, EM_GETLINECOUNT, 0, 0)
With richtextbox1
For i = 0 To lngCount - 1
'Get line index
lngLineIndex = SendMessage(.hwnd, EM_LINEINDEX, i, 0)
'get line length
lngLength = SendMessage(.hwnd, EM_LINELENGTH, lngLineIndex, 0)
'resize buffer
strBuffer = Space(lngLength)
'get line text
Call SendMessageStr(.hwnd, EM_GETLINE, i, ByVal strBuffer)
'Number each line
strRichText = strRichText & CStr(i + 1) & " " & strBuffer & vbCrLf
Next
'rewrite numbered text in RichTextBox
.Text = strRichText
End With
Open "c:\abcd.txt" For Output As #1
Dim myline
myline = richtextbox1.Text
Write #1, myline
Close
End Sub
Private Sub Form_Load()
With richtextbox1
.Text = "line one" & vbCrLf
.Text = .Text & "line two" & vbCrLf
.Text = .Text & "line three"
End With
Call mySaveFile(richtextbox1)
End Sub
'--------------------------------------------------------------------------------
Private Sub mySaveFile(strText As String)
Dim ipCrlf1 As Long, ipCrLf2 As Long 'new line pointers
Dim ipSp1 As Long 'spce pointer
Dim strLine As String 'line of text
Open "myFile.Txt" For Output As #1
ipCrlf1 = 1: ipCrLf2 = 1
'break up on line feeds
Do
ipSp1 = 1
'find a line feed
ipCrLf2 = InStr(ipCrlf1, strText, vbCrLf)
If ipCrLf2 = 0 Then
'if there isn't one then set the length to the string
ipCrLf2 = Len(strText) + 1
End If
'get the line of text from the string
strLine = Mid$(strText, ipCrlf1, ipCrLf2 - ipCrlf1)
'find the first space character.
ipSp1 = InStr(1, strLine, " ")
If ipSp1 = 0 Then
ipSp1 = 1
Else
ipSp1 = ipSp1 + 1
End If
'remove the line number
strLine = Mid$(strLine, ipSp1, Len(strLine))
ipCrlf1 = ipCrLf2 + 2
Print #1, strLine
Loop Until ipCrLf2 >= Len(strText)
Close #1
End Sub
Thank you very much. I will work on that later on tonight, I'm sure it works though.
Instead of going through that long process of removing the lines and stuff, why not just make TWO RichTextBoxes, make one of them invisible and save your text to both. You can have the numbered one visible and save the invisible one's text.