Re: EditText Focus & Highlighting

new topic     » goto parent     » topic index » view thread      » older message » newer message

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 thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu