winforms - VB.NET 2010 DataGridView Handling Keypress via EditingControlShowing Event -
i working datagridview first time , while have many questions, latest issue vexing me.
summary of issue: have datagridview (dgv) have set of columns defined. readonly editable.
editable columns need 4 things occur.
1) allow numeric entry
2) allow maximum of 2 digits
3) 0 pad entries <2 digits
4) issue:
if user types in 2 digit number, want detect , tab next column. cannot work.
sample code (with known working items left out):
private sub dgvdiary_editingcontrolshowing(sender object, e system.windows.forms.datagridvieweditingcontrolshowingeventargs) handles dgvdiary.editingcontrolshowing dim txtedit textbox = e.control txtedit.maxlength = 2 'remove existing handler removehandler txtedit.keypress, addressof txtdgvdiaryedit_keypress addhandler txtedit.keypress, addressof txtdgvdiaryedit_keypress end sub private sub txtdgvdiaryedit_keypress(byval sender object, byval e system.windows.forms.keypresseventargs) 'test numeric value or backspace if isnumeric(e.keychar.tostring()) _ or e.keychar = chrw(keys.back) e.handled = false 'if numeric else e.handled = true 'if non numeric end if 'if user typed in 2 characters, move on! 'don't work! if strings.len(me.dgvdiary.rows(me.dgvdiary.currentrow.index).cells(me.dgvdiary.currentcell.columnindex).value) = 2 sendkeys.send("{tab}") end if end sub
basically during event i'm not able see value of cell "will be" when entered.
i tried adding ".refreshedit" , ".commit" didn't work.
any way test code within event or there event fire afterward can use?
you looking in wrong place. need examine text in textbox, not grid, see how many characters being typed. try using textchanged event that:
private sub txtdgvdiaryedit_textchanged(sender object, e eventargs) if directcast(sender, textbox).text.length = 2 sendkeys.send("{tab}") end if end sub
like other code, add handlers:
'remove existing handler removehandler txtedit.textchanged, addressof txtdgvdiaryedit_textchanged addhandler txtedit.textchanged, addressof txtdgvdiaryedit_textchanged removehandler txtedit.keypress, addressof txtdgvdiaryedit_keypress addhandler txtedit.keypress, addressof txtdgvdiaryedit_keypress
alternatively, can check see if textbox has 1 character, , if keypress passing in number, send tab key then. remove textchanged event code in case:
private sub txtdgvdiaryedit_keypress(sender object, e keypresseventargs) 'test numeric value or backspace if isnumeric(e.keychar.tostring()) _ or e.keychar = chrw(keys.back) e.handled = false 'if numeric else e.handled = true 'if non numeric end if if directcast(sender, textbox).text.length = 1 andalso char.isnumber(e.keychar) sendkeys.send("{tab}") end if end sub
Comments
Post a Comment