?
Solved

GoTo line number in text box--VB6.0

Posted on 2004-10-17
3
Medium Priority
?
889 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 1600 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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…
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…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 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