|
-
Jul 26th, 2000, 02:06 PM
#1
Thread Starter
Member
I work on a small program in which the user opens a textfile into a richtextbox.
I want the program to add some characters to each line of the text that the user has selected to open with the commondialog Open.
1. Now, how can my program tell the path of the file that the user has selected, because as far as i can see, i need the path of the file in order to be able to open it for appending i.e.?
2. How do i count all the lines in a text?
Kind regards
Mantooth
-
Jul 26th, 2000, 02:48 PM
#2
Frenzied Member
this will load a selected file into a RichTextBox:
Code:
Private Sub Command1_Click()
CommonDialog1.ShowOpen
RichTextBox1.FileName = CommonDialog1.FileName
End Sub
to count the lines you could just count the CRLF chars
Code:
Private Function countLines() As Integer
Dim i As Integer
Dim crlfpos As Long
Dim strtemp As String
strtemp = RichTextBox1.Text
Do
crlfpos = InStr(strtemp, vbCrLf)
If crlfpos = 0 Then Exit Do
i = i + 1
strtemp = Mid(strtemp, crlfpos + 1)
Loop
countLines = i
End Function
-
Jul 26th, 2000, 04:45 PM
#3
Thread Starter
Member
That works fine but....
I want to add characters to a textfile with over 20000 lines in it.
Something that can write "V " in front of each line in the document.
How can i make something that adds that to a line and then proceeds with the next line until the end of the text?
I really appreciate your help
I´ve just started with VB6
Frank
-
Jul 26th, 2000, 04:51 PM
#4
As I understand you correctly, you want to append V (or any other character(s)) to the beginning of each line:
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)
'append "V" at the beginning of the line
strRichText = strRichText & "V " & strBuffer & vbCrLf
Next
'rewrite text in RichTextBox
.Text = strRichText
End With
End Sub
-
Jul 26th, 2000, 05:08 PM
#5
Thread Starter
Member
This works very well for some of my files.
However when i try to do it with the biggest file i
get an overflow error?
Frank
-
Jul 26th, 2000, 05:21 PM
#6
Thread Starter
Member
Serge.
Well, actually after trying with a smaller but still big file, i just discovered that when i try to run the program it freezes.
Frank.
-
Jul 27th, 2000, 09:28 AM
#7
This may happen if the number of lines in the RichTextBox more then 2,147,483,647, which is a long data type. If that's the case, then all you have to do is to change lngCount to something bigger, like double, then it will allow you have up to 4.94065645841247E-324 lines. Also, when you have a very big loop, then it may look that the program freezes, so let's fix that too.
Lets assume that double will do it, so let's change the code to 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 dblCount As Double
Dim dblLineIndex As Double
Dim dblLength As Double
Dim strBuffer As String
Dim strRichText As String
Dim i As Double
'Get Line count
dblCount = SendMessage(RichTextBox1.hwnd, EM_GETLINECOUNT, 0, 0)
With RichTextBox1
For i = 0 To dblCount - 1
'Allow operating system to procces other events
DoEvents
'Get line index
dblLineIndex = SendMessage(.hwnd, EM_LINEINDEX, i, 0)
'get line length
dblLength = SendMessage(.hwnd, EM_LINELENGTH, dblLineIndex, 0)
'resize buffer
strBuffer = Space(dblLength)
'get line text
Call SendMessageStr(.hwnd, EM_GETLINE, i, ByVal strBuffer)
'append "V" at the beginning of the line
strRichText = strRichText & "V " & strBuffer & vbCrLf
Next
'rewrite text in RichTextBox
.Text = strRichText
End With
End Sub
-
Jul 27th, 2000, 02:21 PM
#8
Thread Starter
Member
Thanks alot Serge!
This works perfect.
Is it possible to add a Progressbar since the process takes
a long time. In case yes, How do i do that.
Kind regards
Frank
-
Jul 27th, 2000, 02:52 PM
#9
Fanatic Member
Just so that Serge doesn't get too many Post Counts...
Code:
For i = 0 To dblCount - 1
'Allow operating system to procces other events
DoEvents
'MY CODE, NOT SERGE'S, MINE
ProgressBar1.Value = cint(i/dblCount * 100)
'Get line index
dblLineIndex = SendMessage(.hwnd, EM_LINEINDEX, i, 0)
'get line length
dblLength = SendMessage(.hwnd, EM_LINELENGTH, dblLineIndex, 0)
'resize buffer
strBuffer = Space(dblLength)
'get line text
Call SendMessageStr(.hwnd, EM_GETLINE, i, ByVal strBuffer)
'append "V" at the beginning of the line
strRichText = strRichText & "V " & strBuffer & vbCrLf
Next
'rewrite text in RichTextBox
.Text = strRichText
-
Jul 27th, 2000, 02:54 PM
#10
Fanatic Member
Hmmm. I suppose you're getting a division by zero error on the first loop. Well, I'm sure Serge can figure that one out.
-
Jul 27th, 2000, 04:06 PM
#11
Yeap, it's easy enough to fix:
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 dblCount As Double
Dim dblLineIndex As Double
Dim dblLength As Double
Dim strBuffer As String
Dim strRichText As String
Dim i As Double
'Get Line count
dblCount = SendMessage(RichTextBox1.hwnd, EM_GETLINECOUNT, 0, 0)
With RichTextBox1
For i = 0 To dblCount - 1
'You have to start with 1 (i + 1)
'Otherwise you would have progressbar not finished completely
ProgressBar1.Value = (i + 1) / dblCount * 100
'Allow operating system to procces other events
DoEvents
'Get line index
dblLineIndex = SendMessage(.hwnd, EM_LINEINDEX, i, 0)
'get line length
dblLength = SendMessage(.hwnd, EM_LINELENGTH, dblLineIndex, 0)
'resize buffer
strBuffer = Space(dblLength)
'get line text
Call SendMessageStr(.hwnd, EM_GETLINE, i, ByVal strBuffer)
'append "V" at the beginning of the line
strRichText = strRichText & "V " & strBuffer & vbCrLf
Next
'rewrite text in RichTextBox
.Text = strRichText
End With
End Sub
-
Jul 28th, 2000, 05:24 AM
#12
Thread Starter
Member
this works well.
My program is used for conversion of navigational data.
I still have a problem with the largest file which contains
Fixes or intersections. There are probably 130.000 of them so the textfile is that long.
I was wondering if there is another method of writing to a text without looping the hole text, as it would take forever to convert these data? The progressbar hardly moves at all.
The other files which contains radiobeacons are easy enough to convert.
Kind regards
Frank
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|