Solved

GoTo line number in text box--VB6.0

Posted on 2004-10-17
3
814 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

929 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now