Friday 15 August 2014

vb.net - Graphics.DrawRectangle not working in control events -



vb.net - Graphics.DrawRectangle not working in control events -

first of give thanks taking time out of busy schedule assist me.

i developing project (win application) form , 3 textboxes (textbox1, textbox2 , textbox3).

i need draw rectangle around textbox when focused this.

the code is:

private sub textbox123_enter(sender object, e system.eventargs) handles textbox1.enter, textbox2.enter, textbox3.enter using g graphics = me.creategraphics dim r rectangle = sender.bounds r.inflate(4, 4) g.drawrectangle(pens.blue, r) end using end sub

the problem following:

the first time textbox1 gains focus rectangle not drawn. the first time textbox2 gains focus rectangle not drawn.

why not rectangle drawn when first 2 events come in fired?

drawing creategraphics not right approach. if notice also, when move 1 box another, old rectangle not beingness erased. need utilize form_paint event work right. or...perhaps simpler create usercontrols 1-2 pixels larger kid textbox , set backcolor of usercontrol canvas, draw rectangle when command gets focus.

for form paint:

public class form1 private hotcontrol command

if going textboxes, can declare as textbox. way allows same other command types. set/clear tracker:

private sub textbox3_enter(sender object, e eventargs) handles textbox3.enter, textbox2.enter, textbox1.enter hotcontrol = ctype(sender, textbox) me.invalidate() end sub private sub textbox1_leave(sender object, e eventargs) handles textbox1.leave, textbox2.leave, textbox3.leave hotcontrol = nil me.invalidate() end sub

the me.invalidate tells form redraw itself, happens in paint:

private sub form1_paint(sender object, e painteventargs) handles me.paint if hotcontrol isnot nil dim r rectangle = hotcontrol.bounds r.inflate(4, 4) e.graphics.drawrectangle(pens.blue, r) end if end sub

you should turn on alternative strict.

vb.net events graphics enter drawrectangle

No comments:

Post a Comment