ms access - selstart returns position 0 if text is entered in memo field (not clicked) -
i have memo field , list. want accomplish if typing in memo field , click on text record in list text shows in memo positioned beginning cursor was.
after research, , googling succeed make it. did .selstart property.
but me seems selstart has bug. works if click somewhere in memo (then works great.) if typing something, , click on text in list (without clicking in memo field) selstart returns position 0.
this makes me huge problem.
can help? thank you.
as found out, problem cursor position lost when move away memo.
due fact access form controls not "real" controls: real windows controls when have focus. rest of time, sort of images of control pasted onto form.
so, need track cursor position (and selected length of text) during various interractions:
- when user moves cursor using keyboard (
keyup
event) - when user clicks inside memo (
click
event, position cursor or select text using mouse) - when memo gets focus (
getfocus
, first time, whole text selected , cursor @ position 0)
to test this, made small form:
the added following code form:
'---------------------------------------------------------- ' track position of cursor in memo '---------------------------------------------------------- private currentposition long private currentsellen long private sub txtmemo_click() recordcursorposition end sub private sub txtmemo_gotfocus() recordcursorposition end sub private sub txtmemo_keyup(keycode integer, shift integer) recordcursorposition end sub private sub recordcursorposition() currentposition = txtmemo.selstart currentsellen = txtmemo.sellength end sub '---------------------------------------------------------- ' insert when user double-click listbox or press button '---------------------------------------------------------- private sub listsnippets_dblclick(cancel integer) inserttext end sub private sub btinsert_click() inserttext end sub '---------------------------------------------------------- ' actual insertion of text '---------------------------------------------------------- private sub inserttext() if len(nz(listsnippets.value, vbnullstring)) = 0 exit sub echo false 'avoid flickering during update ' update memo content dim oldstr string oldstr = nz(txtmemo.value, vbnullstring) if len(oldstr) = 0 txtmemo.value = listsnippets.value else txtmemo.value = left$(oldstr, currentposition) & _ listsnippets.value & _ mid$(oldstr, currentposition + currentsellen + 1) end if 'we place cursor after inserted text dim newposition long newposition = currentposition + len(listsnippets.value) txtmemo.setfocus txtmemo.selstart = newposition txtmemo.sellength = 0 currentposition = newposition currentsellen = 0 echo true end sub
i have made test accdb database can download can see details , play around this.
Comments
Post a Comment