Spell checking using the Web browser control

2 years 1 month ago #426 by James Levin
James Levin created the code: Spell checking using the Web browser control
Spell checking using the Webbrowser control. This sample code is from NYSDOH.
This code sets up a basic HTML page with a text area with spellchecking.

We placed the new web browser control on a response window with this basic logic and now we have spellchecking within our application..
This includes the key code to make this work.
We hope this is helpful.

Create u_webbrowser_spellcheck from webbrowser (do not set the default url. )
Create variables
boolean ib_initialized = False
String is_text, is_text_html
long il_max_length

private function boolean of_set_text ();
boolean success = True
String ls_Result, ls_Error
// Text area with spell check for text area – use set focus to immediate show the spelling issues
String ls_pre_html = '<textarea id="text" spellcheck="true" rows="10" cols="80" autofocus'
String ls_pre_html_len = ''
String ls_pre_html_end = '>'
String ls_post_html = '</textarea>'
String ls_html

if il_max_length > 0 then
ls_pre_html_len = ' maxlength="' + string(il_max_length) + '" '
end if

ls_html = ls_pre_html + ls_pre_html_len + ls_pre_html_end + is_text_html + ls_post_html

this.Evaluatejavascriptsync("document.body.innerHTML = ' " + ls_html + " ' ", ls_Result, ls_Error)

if (ls_Error <> "") then
success = false
log error
End if

return success

end function

public function string of_get_text ();
String ls_java_script, ls_Result, ls_Error, ls_new_result
long ll_pos

ls_java_script = 'document.getElementById("text").value;'
this.Evaluatejavascriptsync(ls_java_script, ls_Result, ls_Error)

If ls_Error> "" then
messagebox("JavaScript error", "ls_Error=" + ls_Error)
// ls_Result -- has the following {"type":"string","value":"test this out"}
ll_pos = len('{"type":"string","value":"') + 1
ls_new_result = mid (ls_Result, ll_pos , len (ls_Result) - (ll_pos +1))
ls_new_result = Object function .of_javasciprt_returned_string(ls_new_result)
Function to.. update javascript returned string with characters to acceptable for PB
javascript uses the backslash as their escape character
ret_val = f_string_replace_all (ret_val, '\"', '"') // double quote
ret_val = f_string_replace_all (ret_val, '\\', '\') // backslash
ret_val = f_string_replace_all (ret_val, '\n', char(13) + char(10)) // newline
ret_val = f_string_replace_all (ret_val, '\t', char(9)) // tab
End if

return ls_new_result

end function

public function boolean of_set_text (string text, long max_length);
boolean success = True

is_text = text
is_text_html = object function .. of_html_string(is_text)
Function to.. update PB string with characters to acceptable for html and Javascript
ret_val = f_string_replace_all (ret_val, "&", "&") // ampersand
ret_val = f_string_replace_all (ret_val, '"', """) // double quote
ret_val = f_string_replace_all (ret_val, "'", "'") // apostrophe
ret_val = f_string_replace_all (ret_val, "~r~n", " ") // new line
ret_val = f_string_replace_all (ret_val,char(13) + char(10) , " ") // new line
ret_val = f_string_replace_all (ret_val, "~t", " ") // tab
ret_val = f_string_replace_all (ret_val, char(9) , " ") // tab
ret_val = f_string_replace_all (ret_val,'/' , "/") // slash
ret_val = f_string_replace_all (ret_val,'\' , "\") // backslash
il_max_length = max_length
// it’s key to make sure the browser has completely initialize before setting the html
if ib_initialized then success = of_set_text()
return success

end function

public function boolean of_get_initialized ();Return ib_initialized
end function

event navigationstatechanged;
if not ib_initialized then
ib_initialized = true //this is key ….once the web browser control ready then call set text
end if

end event

On our App Manager object – create a function to open spell check
public function boolean uf_open_spellcheck (datawindow adw, long row, string column_name, string column_label, ref string as_updated_text);
// Success result will be
// TRUE = when the column has is updated successfully
// The calling routines can extended as need for their own item change logic
// as_updated_text will contain the updated text
// False = No change or Error
boolean success = true
boolean lb_user_cancelled = False
String ls_note, ls_note_limit

If adw.accepttext() < 1 then Return false

ls_note = adw.getitemstring(row,column_name)
ls_note_limit= adw.Describe(column_name + ".Edit.Limit")

success = open spellcheck response window with text, limit and label
 This response window contains u_webbrowser_spellcheck, OK and Cancel button
 Returns true if user clicks ok and the is_updated_text
 In open event .. call settext from _webbrowser_spellcheck with text and max_length

if success then
If ls_note = is_updated_text then lb_user_cancelled = true

IF not lb_user_cancelled then
success = (adw.setitem (row,column_name, is_updated_text) = 1)
if success then
as_updated_text = is_updated_text
//local extension
// call after this routine completes with successs=true
- --error handling

end if
end if
end if

if not success then
error handing
end if

if success and lb_user_cancelled then
success = false
End if
return success

end function

Please Log in or Create an account to join the conversation.

Moderators: Appeon Administrator