c# - What is, and how do I determine the cause of this strange exception with drawing a Rectangle? -
i'm trying draw rectangle in onpaint method of custom control. it's not don't know how, it's i'm trying time (as opposed creating new pen everytime onpaint called).
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; namespace somenamespace { public partial class window : form { #region designer properties [browsable(true), displayname("formbordercolor"), category("appearance")] public color formbordercolor { get; set; } [browsable(true), displayname("formborderthickness"), category("appearance")] public float formborderthickness { get; set; } #endregion private pen formborderpen; private brush formborderbrush; private rectangle formborderrectangle; public window() { initializecomponent(); this.doublebuffered = true; this.setstyle(controlstyles.optimizeddoublebuffer, true); this.setstyle(controlstyles.userpaint, true); this.setstyle(controlstyles.allpaintinginwmpaint, true); this.setstyle(controlstyles.resizeredraw, true); this.setstyle(controlstyles.containercontrol, true); this.setstyle(controlstyles.selectable, true); // initialize border properties formborderbrush = new solidbrush(formbordercolor); formborderpen = new pen(formborderbrush, formborderthickness); formborderrectangle = new rectangle(0, 0, this.width - (int)formborderthickness, this.height - (int)formborderthickness); } protected override void onpaint(painteventargs paint) { switch(this.formborderstyle) { // if formborderstyle set none, can either // draw our own custom border, or no border @ all. case formborderstyle.none: // draw form border if necessary. console.writeline(formbordercolor); console.writeline(formborderthickness); console.writeline(formborderbrush); console.writeline(formborderpen); console.writeline(formborderrectangle); if(formborderthickness >= 1) { console.writeline("hooray?"); paint.graphics.drawrectangle(formborderpen, formborderrectangle); } else { console.writeline("can't draw border."); } break; default: break; } base.onpaint(paint); } } }
... but, i've getting bunch of exceptions , believe it's because values not being set when i'm expecting them set. don't know else set values.
i'm going take wild stab in dark here, , assume these exceptions being thrown because values these objects being set in constructor, however, don't know else set these values. , i've seen on msdn, thought setting values in right place.
customendcap exception:
'formborderpen.customendcap' threw exception of type 'system.argumentexception'
customstartcap exception:
'formborderpen.customstartcap' threw exception of type 'system.argumentexception'
dashpattern exception:
'formborderpen.dashpattern' threw exception of type 'system.outofmemoryexception'
what don't understand is, why getting these exceptions? i'm not playing around caps or dashpatterns. , why pen , brushes empty when manually setting them?
your pen size 0 formborderthickness, cause of problems seeing.
also, shouldn't use form's constructor if reference properties width or height since dimensions haven't been determined yet. should use onload override that.
i create pens , dispose of them in paint event, keep them local in scope. same thing rectangle.
Comments
Post a Comment