Results 1 to 26 of 26

Thread: SelStart = The beggining of SelText

  1. #1

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336

    SelStart = The beggining of SelText

    I Have an RTB and i have selected text i.e.

    Seltext="DFGHJKIHLKJLKJ"

    I want the cursor to go back to the begging of the seltext and leave the text selected.

    So in other words Select "DFGHJKIHLKJLKJ" and move the cursor back in front of the "D".

    This seltext can be anywhere in the richtextbox so it is variable.

    THanks for the HELP!
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  2. #2
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Québec, Canada
    Posts
    284
    This does not exactly do the job you want, but I still can't find the way the make it yht way you want it, so I'm posting it, maybe someone will find out...

    VB Code:
    1. Private Declare Function SendMessagePoint _
    2.  Lib "user32" Alias "SendMessageA" ( _
    3.  ByVal hWnd As Long, _
    4.  ByVal wMsg As Long, _
    5.  ByVal wParam As Long, _
    6.  ByRef lParam As POINTL) As Long
    7.  
    8. Private Type POINTL
    9.     x As Long
    10.     y As Long
    11. End Type
    12.  
    13. Private Const EM_CHARFROMPOS = &HD7
    14.  
    15. Private Sub RichTextBox1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    16.     Dim p As POINTL
    17.     Dim nRetVal As Long
    18.    
    19.     If (Button And vbLeftButton) = vbLeftButton Then
    20.         'Recalculate the position from Twips to Pixels
    21.         p.x = x \ Screen.TwipsPerPixelX
    22.         p.y = y \ Screen.TwipsPerPixelY
    23.         nRetVal = SendMessagePoint(RichTextBox1.hWnd, EM_CHARFROMPOS, 0, p)
    24.         nRetVal = nRetVal And &HFFFF&
    25.         [B]RichTextBox1.SelStart = nRetVal - RichTextBox1.SelLength[/B]
    26.     End If
    27. End Sub

    The bolded line should be the one to modify, but I can't figure out how!!!

    Hope somebody will help you !

  3. #3

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Hi, thanks for the help. But your right that doesnt work.

    I didn't think this would require API. Surely there is a way just using the RTB methods to accomplish this.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    You could add your own sub or property procedure to do that.
    VB Code:
    1. Public Property Let SelText(ByVal str As String)
    2.     With RichTextBox1
    3.         .SelText = str
    4.         .SelStart = .SelStart - Len(str)
    5.         .SelLength = Len(str)
    6.         .SetFocus
    7.     End With
    8. End Property
    Now instead of calling RichTextBox1.SelText = "xyz" you call Me.SelText = "xyz"

    Best regards

  5. #5

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Hmm i didnt think this was so hard.


    Is it possible to return the cursor to the begining of the selected text without deselecting the text?


    Like i have "XYZ" selected. I need the cursor to go back to the "X" but leave the "XYZ" Still highlighted.


    Is this not possible?
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  6. #6

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Bumpage
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  7. #7
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Well

    hmm...

    you could possibly set a negative .SelLength.

    Try it I guess.


  8. #8

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    been there, done that.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  9. #9
    Fanatic Member rudvs2's Avatar
    Join Date
    Mar 2001
    Location
    NZ
    Posts
    935
    yes i was just going to suggest what about selecting it backwards starting at the end point and finishing at the start eg noramally to select the word "Hello" you would start at "H" and end at "o" instead start your selection at "o" and end at "h"

  10. #10
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Why is this so important?

  11. #11

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Does it matter why it's so important? =p It's important cause I want to know it.

    You can't Select text backwards. I have tried. I would be glad if someone could prove me wrong.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  12. #12
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Why would you need to?

  13. #13

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    why would i need to? Did you read the thread?
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  14. #14
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Originally posted by Arc
    Bumpage
    Yes I did.

  15. #15
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    The SelStart and the SelLength property sends an EM_SETSEL message to the textbox or rtb.
    With EM_SETSEL you specify the start position of the selection in wParam and the end position in lParam.
    But according to MSDN Library:
    The caret is placed at the end of the selection indicated by the greater of the two values nEnd and nStart.
    So I suppose you can't do it through code but you could always play with the SetCaretPos API function if you like but I think (haven't tested it yet) that that would destroy any selection.

  16. #16

    Thread Starter
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Damnit! This can't be impossible! I've never tried to do anything in VB that turned out to be impossible.

    THere has to be Some way!

    I have put waaaaaayyyy too much work into this project for this part to not work. If this doesnt work i might as well scrap the whole thing.


    Hey Joacim. What i am trying to do is duplicate the way a listbox selects items. It highlights the text. BUt in a listbox it doesnt scroll off to the right when an item is selected. however in a RTB it does.

    Can you think of anyway i can duplicate this process? ANything would be appreciated. At this point i am very depressed.

    So any hope at all would be great!
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  17. #17
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Read my answer in your other thread
    http://www.vbforums.com/showthread.p...hreadid=126669

    Best regards

  18. #18
    Member
    Join Date
    Apr 2016
    Posts
    61

    Re: SelStart = The beggining of SelText

    Why not try Len() and Left$()?! It's something like :

    Text1.text being a string

    If Len(Text1.Text) = 14 Then
    Text1.Text = Left$(Text1.Text, 1) & Text1.Text
    End If

    I never tried these code, but this might help to get you started...
    Last edited by ramp10er; May 3rd, 2017 at 10:45 PM.

  19. #19
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SelStart = The beggining of SelText

    Two things 1) this is over 16 years old. If they haven't gotten their answer by now, they'll never get it. 2) your code still doesn't do what was asked.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  20. #20
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: SelStart = The beggining of SelText

    I second that.
    My usual boring signature: Nothing

  21. #21
    Lively Member
    Join Date
    Nov 2014
    Posts
    93

    Re: SelStart = The beggining of SelText

    I tried this:
    Code:
        rtb.Text = "Mary had a little lamb"
        i = InStr(rtb.Text, "little")
        rtb.SelStart = i - 1
        rtb.SelLength = 6
        
        Debug.Print rtb.SelStart & " " & rtb.SelText
    Debug printed 11 little is that what you're looking for?

  22. #22
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,624

    Re: SelStart = The beggining of SelText

    K---
    Did you even READ posts 19 & 20?

  23. #23
    Lively Member
    Join Date
    Nov 2014
    Posts
    93

    Re: SelStart = The beggining of SelText

    Yes, as a matter of fact I did. Why do you ask?

  24. #24
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: SelStart = The beggining of SelText

    Because you're replying to a person who asked the question sixteen years back and hasn't been active on the forum for about four years. The odds that they are still interested in the question are pretty slim. The odds that they are still using the language aren't that good, either. And finally, the answer you gave seems to miss the point of the question. Selecting part of the text is easy. Selecting part of the text, leaving it selected, AND putting the cursor elsewhere is what wasn't so easy.
    My usual boring signature: Nothing

  25. #25
    Lively Member
    Join Date
    Nov 2014
    Posts
    93

    Re: SelStart = The beggining of SelText

    Needless to say, I disagree with all points. First, as this board apparently has no Statute of Limitations, a fourteen year old threads remain active, it did/does not seem out of line to post a reply. Quite possibly the message originator has fallen from his perch a long time ago, but as evidenced by your responses, the posting has not gone unread (the object of the exercise). Lastly, the original message is explicit and states the selStart is to be moved to the front of the selText. Not at some unspecified position.

    Notwithstanding the above, I bow to the censure of a 'super moderator',
    Bob.

  26. #26
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: SelStart = The beggining of SelText

    True, there is no expiration on an open thread. However, there is an expiration on the utility of an answer to an open thread, and there is one, possible, side effect to replying to such an ancient thread. Frankly, I kind of think it might be beneficial, so I'm pretty ambivalent about it. The side effect is due to the fact that you can subscribe to a thread, in which case you'll get an email notice about a reply to the thread. So, the side effect is that a reply could, 'raise the dead'. That may annoy people, or it may bring back those who have wandered off. I know of cases where both results have occurred, which is why I'm ambivalent about such a post.

    As to the code, where's the cursor after you run that?
    My usual boring signature: Nothing

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