editbox / combobox text check for charicaters? lazarus / 10-Designer ---------------------------------------------------------------------- cazzajay · 2009-10-09 07:20 i have some edits and combo boxes that end up creating filenames.  is there any way to run a little procedure to check to ensure they don't contain the windows-forbidden characters such as ! etc? im looking for as simple a solution as possible, any help? thanks cj ---------------------------------------------------------------------- theo · 2009-10-09 09:10 What is exactly your question? You don't know how to scan a string for certain characters? Or you don't know which event to use for checking the content of some controls? What do you mean with "simple a solution as possible"? ---------------------------------------------------------------------- cazzajay · 2009-11-18 18:15 ok ive managed to work around this but the problem has cropped up again. i have an edit box, and i want to scan every single character in that box to check it ain't from a known "bad list".  pseudocode something like: y := 0 i := edit1.length for j := 0 to i do begin if edit1.character[j] = '!' then y := y + 1; end; for x := 0 to i do begin if edit1.character[j] = '$' then y := y + 1; end; if y > 1 then begin showmessage('Dont be an idiot - you cant use ! or $ in that field'); exit; end; except i cant find the right bits for edit1.character[j] and edit1.length and the same for a memobox... ---------------------------------------------------------------------- Blaazen · 2009-11-18 18:59 Hi, I think so Code: [Select]i:=length(Edit1.Text); and Code: [Select]for j:=1 to i do begin   if Edit1.Text[j] = '!' then .... Is important to start the loop from 1. You can try this wild characters remove automatically. ---------------------------------------------------------------------- Marc · 2009-11-18 19:21 to make sure the edit text is not retrieved on every call, better store it locally first. Code: [Select]var   S: String;   n: Integer; begin   S := Edit1.Text;   for n := 1 to Length(S) do... ---------------------------------------------------------------------- Blaazen · 2009-11-18 20:52 Marc: Quoteto make sure the edit text is not retrieved on every call, better store it locally firstI'm interested in this. Is this faster - I mean making local variable rather than accessing object preperty again and again ? I usually do it, but sometimes not, I never tested difference. ---------------------------------------------------------------------- Blaazen · 2009-11-18 21:27 I made little test (measured using function Now) and it seems searching in local string variable is more than 200 times faster than accessing object property. ---------------------------------------------------------------------- cazzajay · 2009-11-19 12:44 Quote from: Blaazen on November 18, 2009, 10:27:43 pmI made little test (measured using function Now) and it seems searching in local string variable is more than 200 times faster than accessing object property. Really? I had no idea!!!  What OS is that on? ---------------------------------------------------------------------- Blaazen · 2009-11-19 14:29 On Kubuntu 9.10, Qt 4.5, 64 bit, i can give here code but now I am away from my PC. ---------------------------------------------------------------------- cazzajay · 2009-11-19 14:59 dont worry about it - i did it the local variable way, on win32, and the difference is hardly noticeable but even so - nice to have my app running at its optimum ---------------------------------------------------------------------- Blaazen · 2009-11-20 00:23 Here is a little test, code is simple, I hope it will work on your PCs (it is without project1.lrs file) Edit: Be patient, test run ~ 15 seconds. On my CPU Intel Penryn 7350 2GHz usually multiplication is 3.2x faster and searching in local variable is 150 - 250 x faster, depend on length of string and also if test run from Lazarus (under debugger) or normally from binary. ---------------------------------------------------------------------- Garry Hopkins · 2010-01-08 09:56 Blaazen has worked this out,any issues after this? I think this should work. ---------------------------------------------------------------------- samps · 2010-04-22 03:44 Windows don't mind the '!' character. In fact, I use it often to ensure MY stuff is listed first.. I would use SETs to discern what is filename legal and what's not: goodies = SET of CHAR; goodies = [a..z,A..Z,0..9,!,[,],etc.....]; while i < length(filename) do     if filename not in goodies then filename i := whatever-fallback- character-routine-returns That's the idea anyway, mind the syntax (why isn't there a syntax highlighter/grammar checker for web content? Like: TinyMCE on proggin' steroids?) cheers Samps