VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

VB.NET Source Code


Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 308,750 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 3,479 people online right now. Registration is fast and FREE... Join Now!





Select specified line in RichTextBox

This is a snippet utilizing Win32 API to select (scroll to) a specified line in a RichTextBox given the line number

Submitted By: PsychoCoder
Actions:
Rating:
Views: 3,261

Language: VB.NET

Last Modified: October 23, 2008
Instructions: Provide the line number and RichTextBox

Snippet


  1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
  2.   (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  3.  
  4.  
  5.  
  6. Private Const EM_SETSEL = &HB1
  7. Private Const EM_GETLINECOUNT = &HBA
  8. Private Const EM_LINEINDEX = &HBB
  9.  
  10. Private Sub SelectLine(ByRef lineNum As Long, ByRef rtb As System.Windows.Forms.RichTextBox)
  11.     Dim _start As Long
  12.     Dim _end As Long
  13.     Dim numLines As Long
  14.  
  15.     With rtb
  16.         'Get the line count in the RichTextBox
  17.         numLines = SendMessage(.handle, EM_GETLINECOUNT, 0, 0&)
  18.  
  19.         'Make sure the line number provided isnt greater
  20.         'than the number of lines in the box, if
  21.         'so then exit
  22.         If lineNum > numLines - 1 Then Exit Sub
  23.  
  24.         'Get the beginning of the line
  25.         _start = SendMessage(.handle, EM_LINEINDEX, lineNum, 0&)
  26.  
  27.         'Get the end of the line
  28.         _end = SendMessage(.handle, EM_LINEINDEX, lineNum + 1, 0&)
  29.  
  30.         'Set focus to the control
  31.         .Focus()
  32.  
  33.         'Highlight the desired row
  34.         Call SendMessage(.handle, EM_SETSEL, _start, _end)
  35.     End With
  36. End Sub

Copy & Paste


Comments


fayzandotcom 2008-10-25 14:44:26

Great Snippet, thanx!

lhbluesoft 2009-08-13 10:06:12

Thx!helpful!


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month