Solved

GoTo line number in text box--VB6.0

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

747 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

12 Experts available now in Live!

Get 1:1 Help Now