Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Selecting text within a text field
#1
HI,

This is strange. I feel I should know how to do this already but can't seem to figure out how to select text in a window and replace or delete it.
I can snag the entire text using getwintext then use a replacerx statement but I just want to find and replace one line within it.

Thanks,
Stuart
#2
If it is Edit or RichEdit20A or similar control, can be used edit and rich edit control messages. Some other control types also have their own interfaces that allow to do it. Otherwise I don't know a way other than replacing whole text.

str s
s.getwintext(...)
s.replacerx etc
s.setwintext, or key Ca; s.setsel.
#3
Hi Gintaras,
This is the control window information:

WindowsForms10.RichEdit20W.app.0.378734


So it looks like a rich edit.

I don't know much on how to use rich edit controls.
I tried with the

Code:
Copy      Help
str s
s.getwintext(...)
s.replacerx etc
s.setwintext, or key Ca; s.setsel.


approach but the program didn't like it. I think it like to track any edit changes itself so the fact that I "pulled a fast one" on it "beneath it's very nose" kind of freaked it out.

Is there an easy way use rich edit control to find the statement (either a specific str "x y z" or perhaps a RegEx pattern and then delete it.

If this is too complicated to explain to me at my programming level, I understand...

Stuart
#4
Use EM_EXSETSEL message. Documented in the MSDN library. Possibly also you can find something about it here. Then use either EM_REPLACESEL, or, if the program does not like it, use keyboard or str.setsel.
#5
Hi Gintaras,

I can see how the MSDN library could be a great resource. Can you give a simple example of how to use those type of commands (e.g. EM_REPLACESEL) with QM?

I have tried to reverse engineer some of your examples already in use in QM but they are quite complex. Maybe you could provide a very simple example or someone can point me to an easy explanation on the web.

Thanks, Stuart
#6
I tested EM_REPLACESEL, and it does not work when the window belongs to other process. But can be used str.setsel or WM_CHAR.

Function EditControlSelectReplace
Code:
Copy      Help
;/
function hwnd ifrom ito [$replacetext]

;Selects and optionally replaces part of text of an Edit or rich edit control.
;The window can be inactive. It can belong to any process.


;hwnd - control handle.
;ifrom, ito - 0-based character index. Use 0/-1 to select all text. Use -1/-1 to remove selection. Use -2/-2 to move the text cursor to the end.
;replacetext - text that will replace the selection. Can be "" to remove the selected text. If omitted or 0, does not replace.



SendMessage hwnd EM_SETSEL ifrom ito

if(replacetext)
,if(!replacetext[0]) SendMessage hwnd EM_REPLACESEL 0 0 ;;this works in other process too
,else
,,GetWindowThreadProcessId hwnd &_i
,,if(_i=GetCurrentProcessId) SendMessage hwnd EM_REPLACESEL 0 replacetext
,,else for(_i 0 len(replacetext)) SendMessage hwnd WM_CHAR replacetext[_i] 0

Now tested with EM_REPLACESEL on Windows 7, and it works. Then better use this version:
Function EditControlSelectReplace
Code:
Copy      Help
;/
function hwnd ifrom ito [$replacetext]

;Selects and optionally replaces part of text of an Edit or rich edit control.
;The window can be inactive. It can belong to any process.

;hwnd - control handle.
;ifrom, ito - 0-based character index. Use 0/-1 to select all text. Use -1/-1 to remove selection. Use -2/-2 to move the text cursor to the end.
;replacetext - text that will replace the selection. Can be "" to remove the selected text. If omitted or 0, does not replace.


SendMessageW hwnd EM_SETSEL ifrom ito
if(replacetext) SendMessageW hwnd EM_REPLACESEL 1 _s.unicode(replacetext)
#7
Hi Gintaras,
Sorry for delaying in thanking you!
Thanks for trying to help me with the RichEdit Text - no matter what I did, I kept on getting the same errors as I described previously.
Quote:An Unexpected Error Occurred:
Exception has been thrown by the target of the invocation.
Click Retry or Cancel to terminate the application
Quote:A fatal error occurred and the application will now terminate:
COM returned an unexpected error code: Details are RPC_E_CANTCALLOUT_ININPUTSYNCCALL

I think I know what the problem is: this text has fields that you can tab to mixed in with it - for the different parts of a medical report - i.e. HISTORY, COMPARISON, FINDINGS, IMPRESSION.

I believe this is generating the errors. So I have decided to go with the Copy All text, ReplaceRx, Paste string back in approach. It works!!


Code:
Copy      Help
act win("Sample App" "WindowsForms10.Window.8.app.0.378734a");err ret


int reportcid=GetWinId(child("" "WindowsForms10.RichEdit20W.app.0.378734a" win("SampleApp" "WindowsForms10.Window.8.app.0.378734a") 0x5))
str initialtext.getwintext(child(reportcid "" "WindowsForms10.RichEdit20W.app.0.378734a" win("SampleApp" "WindowsForms10.Window.8.app.0.378734a") 0x5))

str InsertedText=initialtext

InsertedText.replacerx("(?i)(?<=SAMPLE SITE OF INSERTION:)\s" "Sample Inserted Text")

act child(reportcid "" "WindowsForms10.RichEdit20W.app.0.378734a" win("SampleApp" "WindowsForms10.Window.8.app.0.378734a") 0x5);err ret

'Ca;; selects all
0.1
'X;; deletes all

outp InsertedText;err ret


To undo - the user just presses Ctrl-Z twice (once for the paste, once for the previous delete)

Thanks!!

Stuart


Forum Jump:


Users browsing this thread: 1 Guest(s)