vb.net - Enumerate all controls in a form (redundant) -
i'm trying enumerate controls in form satisfy condition code beelow
public enum methodseachenum startswith = 1 endswith = 2 contains = 3 end enum public function getallcontrols(control control, key string, methodseach methodseachenum, controltype type, optional usetag boolean = true) ienumerable(of control) dim controls = control.controls.cast(of control)() return (controls.selectmany(function(ctrl) getallcontrols(ctrl, metodo)).concat(controls).where(function(c) select case methodseach case metodoseachenum.endswith if (usetag) return c.tag.tostring.toupper.endswith(key.toupper) , c.gettype() controltype else return c.name.toupper.endswith(key.toupper) , c.gettype() controltype end if case metodoseachenum.startswith if (usetag) return c.tag.tostring.toupper.startswith(key.toupper) , c.gettype() controltype else return c.name.toupper.startswith(key.toupper) , c.gettype() controltype end if case metodoseachenum.contains if (usetag) return c.tag.tostring.toupper.contains(key.toupper) , c.gettype() controltype else return c.name.toupper.contains(key.toupper) , c.gettype() controltype end if case else return false end select end function)) end function
inside form there groupbox , inside textbox. these textbox not returned , i'm not understanding why...
here how call function
dim ctrls = getallcontrols(formtagliente, "txtquote", metodoseachenum.startswith, gettype(textbox), false) each txt textbox in ctrls ... next
there imho few information answer question "why doesn't work specific case"
getallcontrols 2 argument missing in code maybe problem lies there
anyway toyed little code (but haven't tested it's more poc) , here's got :
enum searchmethod startswith = 1 endswith = 2 contains = 3 end enum function getallcontrols(of t control)(ctrl control, key string, method searchmethod, optional usetag boolean = true) ienumerable(of t) ' todo validate args dim upperkey = key.toupper dim searchpredicates() func(of string, boolean) = { function(src, tgt) src.startswith(upperkey), function(src, tgt) src.endswith(upperkey), function(src, tgt) src.contains(upperkey) } dim ctrlselector func(of control, string) = if(usetag, function(c) c.tag.tostring.toupper, function(c) c.name.toupper) return getallcontrolsiterator(of t)(ctrl, ctrlselector, searchpredicates(cint(method) - 1)) end function private iterator function getallcontrolsiterator(of t control)(ctrl control, ctrlselector func(of control, string), searchpredicate func(of string, boolean)) ienumerable(of t) each child in ctrl.controls if searchpredicate(ctrlselector(child)) andalso typeof child t yield directcast(child, t) each grandchild in getallcontrolsiterator(of t)(child, ctrlselector, searchpredicate) yield directcast(grandchild, t) next next end function
the idea separate "construct criteria logic" actual "loop, search, yield" one, using generic constraint force targettype control (and having directly "good" return type). find simpler use iterator
block that's more personal
maybe solve problem ?
Comments
Post a Comment