🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.web
Article #1424

Re: DISPID_HTMLELEMENTEVENTS2_ONFOCUS :: How to force text insertion point(caret position) of a input text box

#1424
From: James O'Brien
Date: Sun, 13 Feb 2005 03:45
73 lines
3702 bytes
>
> You are querying the document's selection information.  Why?  You said
> that you wanted to set the INPUT ELEMENT's caret position, but your code
is not
> actually doing that at all.  You are modifying the DOCUMENT'S selection
> instead.
>

In any examples I've seen regarding getting cursor/caret position, the code
referenced the  document interface to get its TextRange interface.
Since a document can only have one textrange and the input text object has
focus, isn't the documents textrange going to correspond to the cursor
position in the input text object?  Anyway, where else am I going to get it
from?  The ITMLInputTextElement interface has a createTextRange() method,
but this doesn't give me existing cursor selection information.

> What is the actual problem you are having?  My guess would be that the
> browser to assigning its own caret positioning after the OnFocus event has
> exited.

The event-handler code below is now working as I need it.  Since I already
have the previous caret position
stored,  I can go ahead and use input text element's createTextRange()
method, collapse() that textrange, then
move() the insertion point to the value of stored CaretPos, and finally
select the text range.   Once the OnFocus
event handler returns the caret is where I want it to be.

Basically, I needed to select the TextRange object after I modified it.

//--------------------------------------------------------------------------
-
void TDHTMLForm::ElementCTRL_OnFocus(T_IHTMLElement &pElement,
T_IHTMLEventObj &pElementEvent, long *CaretPos)
{
           BSTR  bstr;

            long  pos;

             T_IDispatch  Dispatch;
             T_IDispatch  Range;

        T_IHTMLDocument2  Doc;

 T_IHTMLInputTextElement  InputText;

  T_IHTMLSelectionObject  Selection;
         T_IHTMLTxtRange  TextRange;


  //get IHTMLInputTextElement interface

if(SUCCEEDED(pElement->QueryInterface(IID_IHTMLInputTextElement,(LPVOID*)&In
putText))){

      InputText->createTextRange(&TextRange);
      if(SUCCEEDED(TextRange->get_text(&bstr))){
         INPUT_Status->Panels->Items[1]->Text=bstr;
         ::SysFreeString(bstr);
      }
      TextRange->collapse(true);
      TextRange->move(WideString("character"), *CaretPos, &pos);
      TextRange->select();


      A.sprintf("Cursor should be at position %d.", pos);
      INPUT_Status->Panels->Items[2]->Text = A;

  }
  return;
}
//--------------------------------------------------------------------------
-
Technical Details
Message-ID: <420ecd4f$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!feeder2.on.meganewsservers.com!meganewsservers.com!textfeed1.on.meganewsservers.com!38.119.100.83.MISMATCH!news-out.nntp.be!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <420d1f09@newsgroups.borland.com> <420d24c4$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: feeec,b4f655a4eb3bf0d7
X-Google-Attributes: gidfeeec,public
X-Google-Language: ENGLISH,ASCII-7-bit
Reply-To: "James O'Brien" <obrien@niscnet.com>
From: "James O'Brien" <obrien@niscnet.com>
Newsgroups: borland.public.cppbuilder.internet.web
References: <420d1f09@newsgroups.borland.com> <420d24c4$1@newsgroups.borland.com>
Subject: Re: DISPID_HTMLELEMENTEVENTS2_ONFOCUS :: How to force text insertion point(caret position) of a input text box
Date: Sat, 12 Feb 2005 22:45:23 -0500
Lines: 75
Organization: NISC
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
NNTP-Posting-Host: 69.171.99.154
Message-ID: <420ecd4f$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1108266319 69.171.99.154 (12 Feb 2005 19:45:19 -0800)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!feeder2.on.meganewsservers.com!meganewsservers.com!textfeed1.on.meganewsservers.com!38.119.100.83.MISMATCH!news-out.nntp.be!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail