How do I make my program to a specified character in a file. Let's say I want it to go to line 15 of the file and to the 40 character!
Printable View
How do I make my program to a specified character in a file. Let's say I want it to go to line 15 of the file and to the 40 character!
You need to use a loop, that inputs a line at a time. When uoi have reached the line that you wish to get the character from, then use Mid$() to extract the char from the string.
Code:Private Sub Form_Load()
MsgBox getChar("e:\test\test.txt", 15, 40)
End Sub
Private Function getChar(strFile As String, lLineNum As Long, lCharNum As Long) As String
Dim lCount As Long
Dim strLine As String
Open strFile For Input As #1
Do
lCount = lCount + 1
'read in as line
Line Input #1, strLine
'of we have hit the right line
If lCount = lLineNum Then
'return the charcter from the position in lCharNum
getChar = Mid$(strLine, lCharNum, 1)
'close
Close #1
'and exit
Exit Function
End If
Loop Until EOF(1)
End Function
If you want to set the caret at a position in a textbox then use the following code:
Just call the SelectPos function in the following manner: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 Const EM_LINEFROMCHAR = &HC9
Private Const EM_LINEINDEX = &HBB
Public Sub SelectPos(txtBox As TextBox, Row As Long, Col As Long)
Dim lngPos As Long
If Row < 1 Or Col < 1 Then
MsgBox "The row and column numbers must be 1 or higher", vbCritical
Exit Sub
End If
With txtBox
lngPos = SendMessage(.hwnd, EM_LINEINDEX, Row - 1, 0)
If lngPos > -1 Then
.SelStart = lngPos + Col - 1
End If
End With
End Sub
Good luck!Code:SelectPos Text1, 15, 40
[Edited by Joacim Andersson on 09-09-2000 at 08:32 PM]