dcsimg
Results 1 to 10 of 10

Thread: Getting the position of the caret

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    Getting the position of the caret

    I want to stop the typing or insertion of text into a textbox or any control that the user can type in when the text area has reached a certain point. Counting characters or figuring character widths is not an option. I think what I need to know is the location of the caret so I can use that value to determine weather I should end any further typing or insertion of text.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Getting the position of the caret

    And is this in a webbrowser control or is it in a vb textbox?

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    Re: Getting the position of the caret

    It will be related to a webbrowser control in my particular case now but would still like to know how for othercontrols. I found a edit browser, DHTMLEdit, that behaves like a web browser but can also be used for editing; like typing into it. This is what I am using in my project that you have been helping me with. I thought that if I can get the know-how for a regular textbox that same approach might be adapted for the edit browser.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  4. #4
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,057

    Re: Getting the position of the caret

    If your edit control has or is going to have scrollbar(s), the caret might be hidden when that control is scrolled. It would then be difficult to figure out the coordinates of the hidden caret. For a TextBox, the closest functionality offered is the EM_POSFROMCHAR message.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  5. #5
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Getting the position of the caret

    ok here might be a start for you i did mess around with this the other day took me a while but i got to here... you will need to click (with mouse) on the textbox and then try this sub (maybe in a command button click)

    Code:
    Private Function CursorPos() As Single
    Dim oField As Variant
    Dim iCaretPos As Variant
    Dim oSel As Object
           
      WebBrowser1.Document.childnodes.Item(0).focus
     
      Set oSel = WebBrowser1.Document.selection.createRange
        iCaretPos = -(oSel.moveStart("character", -(Len(WebBrowser1.Document.childnodes.Item(0).innertext) + 1)))
        iCaretPos = iCaretPos - 1
    
    CursorPos = iCaretPos
    End Function
    i hope you can fix it better... but i think this is working

    my childnodes.Item(0) is a editable DIV so it probably works with a textbox if you change it to your html object

    Edit:
    like i said i have a editabl DIV which is not a textbox you could try this for textbox (hopefully it works) its kinda the same thing as mine but I do not have a .text or a .value property so the .text.length and .value.length does not work

    Code:
    document.activeElement.focus();             
    var sel2 = document.selection.createRange();
    sel2.moveStart('character', -document.activeElement.value.length);
    var caretPos = sel2.text.length;
    alert(caretPos);
    Last edited by Max187Boucher; Dec 16th, 2012 at 03:12 PM.

  6. #6
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Getting the position of the caret

    also if you want to add a word/character where your caret is you could do this

    Code:
      Set oSel = WebBrowser1.Document.selection.createRange
        oSel.Text = "d"
    "d" will be added to the caret position


    ALSO if you want to read (See the text) of your selection
    Code:
      Set oSel = WebBrowser1.Document.selection.createRange
        Debug.Print oSel.Text

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    Re: Getting the position of the caret

    Now that I think about it the caret position may not be what I need. If the text is typed in from left to right and that is all then yes the caret position will work but what if a person moves the caret back to the middle of the text and inserts addition text which moves the text on the right forward. Now the text has increased but the caret position reflects the point of insertion which will produce incorrect results. I think this may be too much of a headache to try and figure out. What would be better is to allow the extra text to be entered like you see in a textbox that has horizontal scroll; as you type beyond the right border the text added will show because it appears that the text on the left is shifted left as you add addition characters.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  8. #8
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Getting the position of the caret

    ya it can be a pain to deal with webbrowser object from vb6! but you could always use .html files for scripts (or use the .html file from a string in vb instead)

    I'd like to see what you are doing, and this DHTMLEdit is it already in windows/vb6 ? or you need to download an addition .dll/.ocx file?

    thats why i didnt work more on my "unique app" because it was getting hard to keep track of what was typed in! but with a little bit (or alot) of work you will get it working like a charm i'm sure.. i hope you can attach a small example of what you are using so i can help a bit more... i a, interested in this webbrowser/chat/emoticons thing. I like to learn and the only way is to help other people or create your own programs.

    so if you dont mind attach me an example or let m know how to use that DHTMLEdit and what kind of textbox you are using for your html.. and how you add text... etc....

    anyways i am gone for a bit might be back later hopefully you posted an attachment/example

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    Re: Getting the position of the caret

    I need to do just a little bit more clean-up and I made a last minute change by using a second Form for the emots so as soon as I get that done I will post my project here and/or on the other thread we were on.

    The DHTMLEdit is in your VB6 (even VB5) components list. I used it quite some time ago but never got the hang of it so I don't know too much about using it but I remembered that I was able to enter text on the browser window. It works similar to a WebBrowser control and you can configure it to be like a browser (ie no user keyboard input) or you can make it editable. Remembering that feature is what made me think to go and put it on my Form for the user input box which can hold animated emots and text. It's tricky (only because I am not familiar with all the things one can do with it) and I had to do some mickey-mouse coding to get it to work. You'll see what I mean when I post my project.
    Last edited by jmsrickland; Dec 16th, 2012 at 09:04 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,073

    Re: Getting the position of the caret

    @Max

    OK, here it is for whatever it's worth. Maybe you can see what I am trying to get at and perhaps improve the way I have done it. It uses resource file so you'll need to compile it an exe for it to work.
    Attached Files Attached Files


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width