Thread View: borland.public.cppbuilder.internet.web
4 messages
4 total messages
Started by James O'Brien
Fri, 11 Feb 2005 21:09
DISPID_HTMLELEMENTEVENTS2_ONFOCUS :: How to force text insertion point(caret position) of a input text box
Author: James O'Brien
Date: Fri, 11 Feb 2005 21:09
Date: Fri, 11 Feb 2005 21:09
91 lines
4176 bytes
4176 bytes
Hello,
I'm new to programming DHTML, so pardon my ignorance. I'm using
TCppWebBrowser control to display HTML which contains a number of <input
type=text> elements. My program loads the HTML page, searches for all
<input type=text> elements and then attaches an EventDispatcher based on
DIID_HTMLInputTextElementEvents2 to each of the InputText elements. All
this is working fine now.
With the EventDispatcher, I'm able to track the cursor/caret location as the
user types into each of the InputText elements. What I'm ultimately trying
to do is to maintain the curser/caret position for each of the InputText
elements as they go in and out of focus, just as with standard WIN32 edit
boxes. Try as I might, I can't get this to happen. Below is the ONFOCUS
event handler used for the InputText elements. The "CaretPos" value that is
passed in is unique to whatever InputText element fired the event(I've
verified this).
After this event handler executes and indicates that the cursor/caret has
infact been moved, the text in the InputText element is fully selected
(default behavior).
What am I missing? or is this not Possible?
Thanks
James O'Brien
//---------------------------------------------------------------------------
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*)&InputText))){
//get IID_IHTMLDocument2 interface
pElement->get_document(&Dispatch);
if(SUCCEEDED(Dispatch->QueryInterface(IID_IHTMLDocument2,
(LPVOID*)&Doc))){
//get IID_IHTMLTxtRange interface
Doc->get_selection(&Selection);
Selection->createRange(&Range);
if(SUCCEEDED(Range->QueryInterface(IID_IHTMLTxtRange,
(LPVOID*)&TextRange))){
//get existing select text and display main window's statusbar
if(SUCCEEDED(TextRange->get_text(&bstr))){
INPUT_Status->Panels->Items[1]->Text=bstr;
::SysFreeString(bstr);
}
//attempt to create empty slection and move to location secified
by CaretPos
if(SUCCEEDED(TextRange->move(WideString("character"), *CaretPos,
&pos))){
//Display where the cursor now be, into main window's
statusbar
A.sprintf("Cursor should be at position %d.", pos);
INPUT_Status->Panels->Items[2]->Text = A;
INPUT_Status->Refresh();
}
}
}
}
return;
}
//---------------------------------------------------------------------------
Re: DISPID_HTMLELEMENTEVENTS2_ONFOCUS :: How to force text insertion point(caret position) of a input text box
Author: James O'Brien
Date: Fri, 11 Feb 2005 21:16
Date: Fri, 11 Feb 2005 21:16
10 lines
1290 bytes
1290 bytes
Hello Agian, Just in case anyone is wondering why any of the interfaces in my event handler aren't being released. I've created typdefs of the various IID_**** interfaces using TComInterface base class. The interfaces should automatically be released when event-handler exit. James O'Brien
Re: DISPID_HTMLELEMENTEVENTS2_ONFOCUS :: How to force text insertion point(caret position) of a input text box
Author: Remy Lebeau (TeamB)
Date: Fri, 11 Feb 2005 21:37
Date: Fri, 11 Feb 2005 21:37
36 lines
2303 bytes
2303 bytes
"James O'Brien" <obrien@niscnet.com> wrote in message
news:420d1f09@newsgroups.borland.com...
> Below is the ONFOCUS event handler used for the InputText elements.
> The "CaretPos" value that is passed in is unique to whatever InputText
> element fired the event(I've verified this).
You did not show the code that is calling ElementCTRL_OnFocus(), or the code
that is saving the caret positions for each element.
> After this event handler executes and indicates that the cursor/caret has
> infact been moved, the text in the InputText element is fully selected
> (default behavior).
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.
> //get IID_IHTMLDocument2 interface
> pElement->get_document(&Dispatch);
> if(SUCCEEDED(Dispatch->QueryInterface(IID_IHTMLDocument2,
> (LPVOID*)&Doc))){
Why are you going back to the document?
> //get IID_IHTMLTxtRange interface
> Doc->get_selection(&Selection);
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.
Gambit
Re: DISPID_HTMLELEMENTEVENTS2_ONFOCUS :: How to force text insertion point(caret position) of a input text box
Author: James O'Brien
Date: Sun, 13 Feb 2005 03:45
Date: Sun, 13 Feb 2005 03:45
73 lines
3702 bytes
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;
}
//--------------------------------------------------------------------------
-
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads