This shows you the differences between two versions of the page.
sample_code:textedit_search [2015/04/01 12:24] stevegarman created |
sample_code:textedit_search [2015/08/05 14:40] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== TextEdit Search ====== | ||
- | This function is designed to search for a string in a textEdit. | ||
- | It starts searching from the current position of the cursor and, if it finds an occurrence of the string, positions the cursor immediately before the occurrence. | ||
- | |||
- | An area immediately following the cursor is highlighted as the selected area. | ||
- | |||
- | If it does not find an occurrence, | ||
- | <code JavaScript findtext.js> | ||
- | function findtext(thisedt, | ||
- | // | ||
- | // | ||
- | { | ||
- | // | ||
- | var lc = thisedt.GetText().toLowerCase(); | ||
- | sought = sought.toLowerCase(); | ||
- | // | ||
- | var curs = thisedt.GetCursorPos(); | ||
- | // | ||
- | lc = lc.slice(curs+1) | ||
- | // | ||
- | var pos = lc.search(sought); | ||
- | if ( pos > | ||
- | { | ||
- | // | ||
- | pos = pos + curs +1 | ||
- | thisedt.SetCursorPos(pos) | ||
- | thisedt.SetSelection(pos, | ||
- | } | ||
- | else | ||
- | { | ||
- | // | ||
- | // | ||
- | thisedt.SetCursorPos(0) | ||
- | } | ||
- | } | ||
- | </ | ||
- | This function can be used in an app in the manner shown in this sample code | ||
- | <code JavaScript samplefind.js> | ||
- | var edt, | ||
- | // | ||
- | function OnStart() | ||
- | { | ||
- | // | ||
- | var lay = app.CreateLayout( " | ||
- | var hlay = app.CreateLayout( " | ||
- | var btn = app.CreateButton(" | ||
- | btn.SetOnTouch(doFind); | ||
- | hlay.AddChild(btn); | ||
- | fnd = app.CreateTextEdit(" | ||
- | fnd.SetHint(" | ||
- | hlay.AddChild(fnd); | ||
- | lay.AddChild(hlay); | ||
- | // | ||
- | |||
- | var scrollEdit = app.CreateScroller( 1.0, | ||
- | // | ||
- | edt = app.CreateTextEdit( "", | ||
- | edt.SetText(app.ReadFile("/ | ||
- | // | ||
- | // | ||
- | scrollEdit.AddChild(edt); | ||
- | lay.AddChild( scrollEdit); | ||
- | |||
- | |||
- | // | ||
- | app.AddLayout( lay ); | ||
- | } | ||
- | |||
- | function doFind() | ||
- | { | ||
- | app.HideKeyboard(); | ||
- | var findstring = fnd.GetText(); | ||
- | findtext(edt, | ||
- | } | ||
- | |||
- | function findtext(thisedt, | ||
- | // | ||
- | // | ||
- | { | ||
- | // | ||
- | var lc = thisedt.GetText().toLowerCase(); | ||
- | sought = sought.toLowerCase(); | ||
- | // | ||
- | var curs = thisedt.GetCursorPos(); | ||
- | // | ||
- | lc = lc.slice(curs+1) | ||
- | // | ||
- | var pos = lc.search(sought); | ||
- | if ( pos > | ||
- | { | ||
- | // | ||
- | pos = pos + curs +1 | ||
- | thisedt.SetCursorPos(pos) | ||
- | thisedt.SetSelection(pos, | ||
- | } | ||
- | else | ||
- | { | ||
- | // | ||
- | // | ||
- | thisedt.SetCursorPos(0) | ||
- | } | ||
- | } | ||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | </ |