How to delete/remove/undo DrawString in C# WinForm -
my form has background image. have pictureboxes using buttons. attempting have mouseenter/mouseleave event display label or picturebox.
i have been trying various approaches. getting similar results- label or picturebox appears fine, on mouseleave, label1.visible = false; causes temporary blank box on background image of form. while functional, seems slight lag, makes program bad.
i experimented drawstring method. seems option, cannot figure out how remove object on mouseleave event.
is possible? if not, there better option accomplish trying accomplish?
here how drawing string (in buttonclick event testing):
graphics g = this.creategraphics(); string letter = "yo dawg!"; g.drawstring(letter, new font(fontfamily.genericsansserif, 20, fontstyle.regular), new solidbrush(color.black), 100, 100);
you draw in paint event, in mouseleave
set flag, cause paint invalidate()
within paint if flag not set don't draw anything.
public partial class theform : form { private font _font = new font(fontfamily.genericsansserif, 20, fontstyle.regular); private bool _hovering = false; public theform() { initializecomponent(); picbox.paint += new painteventhandler(picbox_paint); picbox.mouseenter += (sender, e) => updatetext(true); picbox.mouseleave += (sender, e) => updatetext(false); } private void picbox_paint(object sender, painteventargs e) { if (_hovering) e.graphics.drawstring("yo dawg!", _font, brushes.black, 100, 100); } private void updatetext(bool show) { _hovering = show; picbox.invalidate(); } }
Comments
Post a Comment