Solved

GoTo line number in text box--VB6.0

Posted on 2004-10-17
3
830 Views
Last Modified: 2013-12-26
Hi,
I am using VB 6.0 for a front end in which i need to have the below functionality--

I have a error log displayed in a text box. If i click on the error which contains the file name and the line number also. when i double click it, i am able open that file in separate text box, but i want to go to that particular line number when the file opens. Either cursor can stop there or highlight the whole line.
Please help me doing this.

Regards,
hSindhu
0
Comment
Question by:hSindhu
  • 2
3 Comments
 
LVL 11

Expert Comment

by:dbrckovi
ID: 12336643
Hi!

Try this:

 - create two textboxes and a command button
 - Set HideSelection property of Text1 to False
 - Set Multiline property of Text1 to True
 - fill Text1 with some Multiline text
 - paste this code:
'------------------------------------------------------
Private Sub Command1_Click()
    On Error Resume Next
   
    Dim LineStart() As Long
    Dim Char As Integer
    Dim X As Integer
    Dim GotoLine As Integer
   
    GotoLine = Val(Text2.Text)
    If GotoLine = 0 Then GotoLine = 1
   
    ReDim LineStart(0 To 1)
    LineStart(1) = 0
   
    For X = 1 To Len(Text1.Text)
        Char = Asc(Mid(Text1.Text, X, 1))
        If Char = Asc(vbCrLf) Then
            ReDim Preserve LineStart(UBound(LineStart) + 1)
            LineStart(UBound(LineStart)) = X + 1
        End If
    Next X

    Text1.SelStart = LineStart(GotoLine)
    If Err <> 0 Then
        Text1.SelStart = Len(Text1.Text)
    End If
   
    DoEvents
    Text1.SelLength = 1
    DoEvents
End Sub
'-------------------------------------------------
 - to use it, enter line number in textbox2, and click the button
0
 
LVL 11

Accepted Solution

by:
dbrckovi earned 400 total points
ID: 12336676
Or create a function or sub which jumps to a specific line in specific textbox:

'----------------------------------------------------------
Private Sub Command1_Click()
    GotoLine Text1, 15
End Sub

Sub GotoLine(TXT As TextBox, LineNumber As Integer)
    On Error Resume Next
   
    Dim LineStart() As Long        'defines an array which will hold position of first character in a line
    Dim Char As Integer
    Dim X As Integer
   
    If LineNumber = 0 Then LineNumber = 1      'there is no line before first :-)
   
    ReDim LineStart(0 To 1)
    LineStart(1) = 0                    'define start of the first line (just to make sure)
   
    For X = 1 To Len(TXT.Text)              'loop through all characters
        Char = Asc(Mid(TXT.Text, X, 1))     'get each character's ascii code
        If Char = Asc(vbCrLf) Then          'if character is CR/LF then this means this is the end of the first line
                                            'so we expect another line after it
            ReDim Preserve LineStart(UBound(LineStart) + 1)   'add element item in array
            LineStart(UBound(LineStart)) = X + 1              'define the start of new line
        End If
    Next X

    TXT.SelStart = LineStart(LineNumber)          'goto starting position of desired line
    If Err <> 0 Then
        TXT.SelStart = Len(TXT.Text)              'if error occures, it means we tried to go to line number
    End If                                        'which doesn't exist. In that case goto last line
   
    DoEvents
    TXT.SelLength = 1            'set selection length, so jump will be visible
    DoEvents
End Sub
'----------------------------------------------------------------
0
 

Author Comment

by:hSindhu
ID: 12336911
Thanks dbrckovi .It worked out.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question