Solved

GoTo line number in text box--VB6.0

Posted on 2004-10-17
3
843 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

734 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