focus to a particula line in richtextbox

focus to a particula line in richtextbox

Page 1 of 1

5 Replies - 6640 Views - Last Post: 23 October 2008 - 05:10 AM Rate Topic: -----

#1 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

focus to a particula line in richtextbox

Posted 22 October 2008 - 06:49 AM

hi there

i want to create a notepad in vb.net i get struck in a portion this is following

as notepad want lot of things like print etc i create a custom control on that control i just add this code

it works it moves only the scroll bar but i want to move the cursor
any help

Private Overloads Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr
        Private Declare Function PutFocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
        Public Sub GoToLineNo(ByVal Lineno As Long)
            'scrolling the line according to the given NoOf lines
            Dim Hnd As Long = Me.Handle
            Dim s As Long
            s = SendMessage(Me.Handle, EMFlags.EM_LINESCROLL, CType(0, IntPtr), CType(Lineno, IntPtr))
            Dim linesMoved As Long = PutFocus(Hnd)
        End Sub



where
EM_LINESCROLL = &HB6
is it right
its pretty tough to me :cry: :cry: :cry: :cry: :sad3: :sad3: :sad3:

This post has been edited by thava: 22 October 2008 - 06:50 AM


Is This A Good Question/Topic? 0
  • +

Replies To: focus to a particula line in richtextbox

#2 RodgerB  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 66
  • View blog
  • Posts: 2,284
  • Joined: 21-September 07

Re: focus to a particula line in richtextbox

Posted 23 October 2008 - 12:09 AM

Possibly not the best method, but it worked for me.

Public Sub SetLineNum(ByRef rtb As RichTextBox, ByVal line As Integer)
    Dim textContents As String = rtb.Text
    Dim lineCount As Integer = 1

    Dim strReader As New StringReader(textContents)

    Try
        While strReader.Peek
            Dim lineContents As String = strReader.ReadLine()

            If lineCount = line Then
                rtb.Select(textContents.IndexOf(lineContents, 0), 0)
                Exit While
            End If

            lineCount += 1
        End While
    Catch
        '' An error occured. Probably because it cant find the line number specified
    End Try

    strReader.Dispose()
End Sub


Was This Post Helpful? 0
  • +
  • -

#3 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: focus to a particula line in richtextbox

Posted 23 October 2008 - 12:29 AM

i think i get an idea from your code i will try it
any way thanks a lot for your response

Here it is my code

Public Sub SetLineNum(ByRef Rtb As RichTextBox, ByVal lineNo As Integer)  
On Error Resume Next
            str = Rtb.Lines
            Me.Select(Rtb.Text.IndexOf(str(Lineno - 1)), 0)
End sub



but the problem is when there is two line with the same text it denote the first line instead of the next line
that's why i choose the api function
any more help

This post has been edited by thava: 23 October 2008 - 12:48 AM

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: focus to a particula line in richtextbox

Posted 23 October 2008 - 01:26 AM

Here's another way of acomplishing this, using Win32 API

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
          (ByVal hwnd As Long, ByVal wMsg As Long, _
           ByVal wParam As Long, lParam As Any) As Long



Private Const EM_SETSEL = &HB1
Private Const EM_GETLINECOUNT = &HBA 
Private Const EM_LINEINDEX = &HBB       

Private Sub SelectLine(pLineNumber As Long)
  Dim chrsToStart As Long
  Dim chrsToEnd As Long
  Dim lineCount As Long

  With RichTextBox1
      'Get the line count in the RichTextBox
      lineCount = SendMessage(.handle, EM_GETLINECOUNT, 0, ByVal 0&)            

      'Make sure pLineNumber isn't greater than total lines
      If pLineNumber > lineCount - 1 Then Exit Sub             

      chrsToStart = SendMessage(.handle, EM_LINEINDEX, pLineNumber, ByVal 0&)            

      chrsToEnd = SendMessage(.handle, EM_LINEINDEX, pLineNumber + 1, ByVal 0&)

      .SetFocus

      Call SendMessage(.handle, EM_SETSEL, chrsToStart, ByVal chrsToEnd)
  End With
End Sub



Private Sub Command1_Click(ByRef sender As Object, ByRef e As EventArgs)
     SelectLine(0)
End Sub 



Hope that helps some :)
Was This Post Helpful? 1
  • +
  • -

#5 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: focus to a particula line in richtextbox

Posted 23 October 2008 - 02:14 AM

View PostPsychoCoder, on 23 Oct, 2008 - 01:26 AM, said:

Here's another way of acomplishing this, using Win32 API

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
          (ByVal hwnd As Long, ByVal wMsg As Long, _
           ByVal wParam As Long, lParam As Any) As Long



Private Const EM_SETSEL = &HB1
Private Const EM_GETLINECOUNT = &HBA 
Private Const EM_LINEINDEX = &HBB       

Private Sub SelectLine(pLineNumber As Long)
  Dim chrsToStart As Long
  Dim chrsToEnd As Long
  Dim lineCount As Long

  With RichTextBox1
      'Get the line count in the RichTextBox
      lineCount = SendMessage(.handle, EM_GETLINECOUNT, 0, ByVal 0&)            

      'Make sure pLineNumber isn't greater than total lines
      If pLineNumber > lineCount - 1 Then Exit Sub             

      chrsToStart = SendMessage(.handle, EM_LINEINDEX, pLineNumber, ByVal 0&)            

      chrsToEnd = SendMessage(.handle, EM_LINEINDEX, pLineNumber + 1, ByVal 0&)

      .SetFocus

      Call SendMessage(.handle, EM_SETSEL, chrsToStart, ByVal chrsToEnd)
  End With
End Sub



Private Sub Command1_Click(ByRef sender As Object, ByRef e As EventArgs)
     SelectLine(0)
End Sub 



Hope that helps some :)



you are genius

thanks for this help one thing did you help me where did you find this constants pls help me i tried but i get the constant name only not the value this will help me for lot of things
or pls give a tip how to find this API constants pls
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: focus to a particula line in richtextbox

Posted 23 October 2008 - 05:10 AM

Well thava, if you happen to have Visual Basic 6 installed you open the API Text Viewer (Look at the screen shot below). From there, select File in the menu then select Load Text File. When the next window opens select WIN32API.TXT. In the API Type dropdown list (once the file is loaded) select Constants and all the constants and their values are right there :)

Attached Image

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1