1. EditText Focus & Highlighting

Hi,

When any message is displayed ( message_box() ) on an EditText using events like w32HLostFocus, irrespective of the focus being returned to same control or the next in order, the cursor or text highlighting (e.g. using setFocus() and setIndex(x, {1,0})) are not displayed within that EditText. The user gets confused about the current active control.

Clicking/Double clicking on that EditText is expected to restore the cursor/highlighting within the same. Though visual confirmation is not present, this is working, as one can click within the EditText at desired point and insert text. Also, even if entire text within the EditText is selected but not indicated so on screen, pressing space key deletes the entire contents of the EditText.

This behaviour is not present when the validation returns true.

e.g.

--  code generated by Win32Lib IDE v1.0.4 Build July-06-2008 
 
constant TheProgramType="exw" 
 
without type_check 
without warning 
 
include win32lib.ew as win32lib 
 
-------------------------------------------------------------------------------- 
--  Window Window1 
constant Window1 = createEx( Window, "Window1", 0, Default, Default, 499, 129, 0, 0 ) 
constant Combo16 = createEx( Combo, "Combo16", Window1, 12, 36, 148, 20*6, 0, 0 ) 
constant EditText17 = createEx( EditText, "EditText17", Window1, 176, 36, 140, 20, 0, 0 ) 
constant EditText18 = createEx( EditText, "EditText18", Window1, 332, 36, 148, 20, 0, 0 ) 
--------------------------------------------------------- 
-------------------------------------------------------------------------------- 
procedure EditText17_onLostFocus (integer self, integer event, sequence params)--params is () 
	if length(getText(self)) < 11 then 
		VOID = message_box("Length should be >= 11", "Msg", MB_ICONINFORMATION) 
		setFocus(self) 
		setIndex(self, {1, 0}) 
	end if 
end procedure 
setHandler( EditText17, w32HLostFocus, routine_id("EditText17_onLostFocus")) 
--------------------------------------------------------- 
 
WinMain( Window1,Normal ) 

Thanks & Regards, Rad.

new topic     » topic index » view message » categorize

2. Re: EditText Focus & Highlighting

I'm working on this problem but you might like to consider an alternative, more user-friendly, way of notify the user about field errors.

The code below employs the concept of not interrupting the user's workflow, while still giving them visual clues that an error exists. I know it looks like a lot more work, but when you have many fields to deal with, this technique often works out easier to implement.

I might even put a lot of this stuff into the win32lib as standard.

without type_check  
without warning  
  
include win32lib.ew as win32lib  
--------------------------------------------------------------------------------  
--  Window Window1  
constant Window1 = createEx( Window, "Window1", 0, Default, Default, 499, 220, 0, 0 )  
constant Combo16 = createEx( Combo, "Combo16", Window1, 12, 36, 148, 20*6, 0, 0 )  
constant EditText17 = createEx( EditText, "EditText17", Window1, 176, 36, 140, 20, 0, 0 )  
constant EditText18 = createEx( EditText, "EditText18", Window1, 332, 36, 148, 20, 0, 0 )  
constant OkBtn = createEx( DefButton, "&Accept", Window1, 5, 5, 50, 20, 0, 0 )  
 
constant SB = createEx(StatusBar, "", Window1, 0,0,0,0,0,0) 
---------------------------------------------------------  
 
sequence fldErrors fldErrors = {} 
sequence validationRtns validationRtns = {} 
procedure addError(integer id, sequence text) 
	if length(text) = 0 then 
		return 
	end if 
	for i = 1 to length(fldErrors) do 
		if fldErrors[i][1] = id then 
			fldErrors[i][2] = text 
			return 
		end if 
	end for 
	fldErrors = append(fldErrors, {id, text}) 
	setWindowBackColor(id, Pink) 
end procedure 
 
procedure clearError(integer id) 
	sequence kids 
	setText(SB, "") 
	if id = 0 then 
		fldErrors = {} 
		kids = getChildren( Window1) 
		for i = 1 to length(kids[1]) do 
			if kids[2][i] = EditText then 
					setWindowBackColor(kids[1][i], BrightWhite) 
			end if 
		end for 
		return 
	end if 
	 
	for i = 1 to length(fldErrors) do 
		if fldErrors[i][1] = id then 
			fldErrors = fldErrors[1..i-1] & fldErrors[i+1..$] 
			setWindowBackColor(id, BrightWhite) 
			return 
		end if 
	end for 
 
end procedure 
 
function hasError(integer id) 
	for i = 1 to length(fldErrors) do 
		if fldErrors[i][1] = id then 
			return fldErrors[i][2] 
		end if 
	end for 
	return "" 
end function 
 
 
procedure validateField(integer id, object context) 
	for i = 1 to length(validationRtns) do 
		if validationRtns[i][1] = id then 
			clearError(id) 
			call_proc(validationRtns[i][2], {id, context, validationRtns[i][3]}) 
		end if 
	end for 
end procedure 
 
procedure addValidation(integer id, integer rtn, object val_id) 
	sequence valset 
	 
	valset = {id, rtn, val_id} 
	for i = 1 to length(validationRtns) do 
		if equal(validationRtns[i], valset) then 
			return 
		end if 
	end for 
	validationRtns = append(validationRtns, valset) 
end procedure 
 
 
procedure check_EditText17(integer id, object context, object val_id) 
	if length(getText(id)) < val_id then  
		addError(id, sprintf("Length should be >= %d", val_id)) 
	end if  
end procedure  
 
procedure check_EditText18(integer id, object context, object val_id) 
	sequence text 
	 
	text = trim(getText(id)) 
	if not find(' ', text) then  
		addError(id, "Must contain at least one space character") 
		setText(id, text) 
	end if  
end procedure  
 
--------------------------------------------------------------------------------  
procedure EditText_onLostFocus (integer self, integer event, sequence params)--params is ()  
	validateField(self, 0) 
end procedure  
setHandler( {EditText17, EditText18}, w32HLostFocus, routine_id("EditText_onLostFocus"))  
---------------------------------------------------------  
  
--------------------------------------------------------------------------------  
procedure EditText_onGotFocus (integer self, integer event, sequence params)--params is ()  
	setText(SB, hasError(self)) 
end procedure  
setHandler( {EditText17, EditText18}, w32HGotFocus, routine_id("EditText_onGotFocus"))  
---------------------------------------------------------  
  
--------------------------------------------------------------------------------  
procedure OkBtn_onClick (integer self, integer event, sequence params)--params is ()  
	sequence kids 
 
	clearError(0)	 
	kids = getChildren( Window1) 
	for i = 1 to length(kids[1]) do 
		validateField(kids[1][i], 0) 
	end for 
	if length(fldErrors) > 0 then 
		VOID = message_box("You need to correct the error(s) before accepting", "ERRORS FOUND", 0) 
	else 
		-- Process the field data 
		-- eg. Update a database record... 
	end if 
end procedure  
setHandler( OkBtn, w32HClick, routine_id("OkBtn_onClick"))  
---------------------------------------------------------  
  
clearError(0) 
addValidation(EditText17, routine_id("check_EditText17"), 11) 
addValidation(EditText18, routine_id("check_EditText18"), 0) 
 
WinMain( Window1,Normal )  
new topic     » goto parent     » topic index » view message » categorize

3. Re: EditText Focus & Highlighting

DerekParnell said...

I'm working on this problem but you might like to consider an alternative, more user-friendly, way of notify the user about field errors.

Hi Derek,

Great piece of code! Thanks for the same. A nice way to get the user's attention at errors. I have already started incorporating it in my application.

Thanks & Regards, Rad.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu