CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2006
    Posts
    392

    [RESOLVED] RichtextBox

    Dear All,
    Is there any method avaliable to get text of the present cursor postion in a richtext box.

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: RichtextBox

    Yes. The present cursor position is the .SelStart property.
    If you set the .SelLength property to say 5, you can access these 5 characters after .SelStart in the .SelText property.
    The .SelText property is always equal to the text beginning at .SelStart having .SelLength characters. If the rtb would get the focus, this text would be marked.

  3. #3
    Join Date
    Sep 2006
    Posts
    392

    Re: RichtextBox

    Dear WOF,
    I want to simply get the entire word at the current cursor location.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: RichtextBox

    You can use the .Find property to search for the first blank after .SelStart, or whichever character you need.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Sep 2006
    Posts
    392

    Re: RichtextBox

    Can you pls exaplain with an example.

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: RichtextBox

    Try this out. It finds and bolds words.

    Code:
    Option Explicit
    
    Private Sub Form_Load()
    Dim a%, b%, q%
    With rtb
      .Text = "In process reject rate for Department 1 : 4.34 %" & vbCrLf & _
    	  "In process reject rate for Department 1 : 124.34 %" & vbCrLf
      q = .Find(":") - 1
      Do While q > 0
    	a = .Find(":", q)
    	b = .Find(vbCrLf, a)
    	.SelStart = a + 1
    	.SelLength = b - a + 1
    	.SelBold = True
    	q = .Find(":", b + 1)
      Loop
      .SelStart = 0
    End With
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: RichtextBox

    or you can use the SendMessage. just search the msdn for EM_FINDWORDBREAK (WB_MOVEWORDLEFT, WB_RIGHTBREAK, etc..) for info.
    Busy

  8. #8
    Join Date
    Sep 2006
    Posts
    392

    Re: RichtextBox

    Dear Thread,
    I am new to Sendmessage,Can you please give one example?

  9. #9
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: RichtextBox

    actually, plenty of sample codes can be found here in the forum. i think what you need is the actual code (just kidding though ) ..


    it requires a richtextbox (rtb) and a command button (command1).
    Code:
    Option Explicit
    
    Private Const WM_USER As Long = &H400
    Private Const EM_FINDWORDBREAK As Long = (WM_USER + 76)
    Private Const WB_MOVEWORDLEFT As Long = 4
    Private Const WB_MOVEWORDRIGHT As Long = 5
    Private Const WB_RIGHTBREAK As Long = 7
    Private Const EM_SETSEL As Long = &HB1
    Private Const WB_CLASSIFY As Long = 3
    
    Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
    
    
    Private Sub Command1_Click()
    Dim i1&, i2&, i3&
    
      If SendMessage(rtb.hwnd, EM_FINDWORDBREAK, WB_CLASSIFY, ByVal rtb.SelStart) = 0 Then
        
        ' det word boundary (left and right position)
        i1 = SendMessage(rtb.hwnd, EM_FINDWORDBREAK, WB_MOVEWORDLEFT, ByVal rtb.SelStart + 1)
        i2 = SendMessage(rtb.hwnd, EM_FINDWORDBREAK, WB_RIGHTBREAK, ByVal i1 + 1)
        i3 = SendMessage(rtb.hwnd, EM_FINDWORDBREAK, WB_MOVEWORDRIGHT, ByVal i1 + 1)
        If i2 > i3 Then i2 = i3
        
        ' select word
        SendMessage rtb.hwnd, EM_SETSEL, i1, ByVal i2
        
        ' display word
        MsgBox rtb.SelText
        
      Else
    
        MsgBox "Cursor is not pointing to a word/char."
    
      End If
         
    End Sub

    hope it helps!
    Busy

  10. #10
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: RichtextBox

    That's a good one, Thread1. What does the WB_CLASSIFY call actually do?

    Isn't there somewhere a good tutorial or summary about the SendMessage usage? It seems there is such a lot of messages you can send. That's such an enormous amount of possibilities I can't use because I don't know them.

  11. #11
    Join Date
    Sep 2006
    Posts
    392

    Re: RichtextBox

    Good Code.Works Like a Charm .

    Warm Welcome to Thread1,
    Danasegar

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured