Rectangle/OnPaint Error in 2005

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Unlhbg==?=

    Rectangle/OnPaint Error in 2005

    Why do I sometimes get this error; works fine most of the time:
    Rectangle '{X=0,Y=0,Width =0,Height=0}' cannot have a width or height equal
    to 0


    void OnPaint(Object sender, PaintEventArgs e)
    {
    Graphics g = e.Graphics;
    Rectangle r = ClientRectangle ;
    LinearGradientB rush gradientBrush = new LinearGradientB rush(r,
    Color.Navy, Color.Chocolate , LinearGradientM ode.ForwardDiag onal);
    g.FillRectangle (gradientBrush, r);
    }
  • Peter Duniho

    #2
    Re: Rectangle/OnPaint Error in 2005

    On Fri, 15 Aug 2008 17:47:12 -0700, Ryan <Ryan@discussio ns.microsoft.co m>
    wrote:
    Why do I sometimes get this error; works fine most of the time:
    Rectangle '{X=0,Y=0,Width =0,Height=0}' cannot have a width or height
    equal
    to 0
    When do you get the error? What line of code causes it? Can you post a
    concise-but-complete code sample that reliably reproduces the problem?

    AFAIK Graphics.FillRe ctangle has no limitation on using an empty
    Rectangle. But as near as I can tell, LinearGradientB rush does. If you
    pass it an empty rectangle, it will throw the exception you describe.

    I don't know why you have a control instance that is empty when it tries
    to paint itself, but the solution would be to either not let it have an
    empty rectangle as its dimensions, or to check the rectangle in the
    OnPaint() method and just not try to draw anything if the ClientRectangle
    is empty.

    Pete

    Comment

    • =?Utf-8?B?Unlhbg==?=

      #3
      Re: Rectangle/OnPaint Error in 2005

      Peter,
      The following line produces the error

      LinearGradientB rush gradientBrush = new LinearGradientB rush(r, Color.Navy,
      Color.Chocolate , LinearGradientM ode.ForwardDiag onal);

      Complete code:

      void OnPaint(Object sender, PaintEventArgs e)
      {
      Graphics g = e.Graphics;
      Rectangle r = ClientRectangle ;
      LinearGradientB rush gradientBrush = new LinearGradientB rush(r,
      Color.Navy, Color.Chocolate , LinearGradientM ode.ForwardDiag onal);
      g.FillRectangle (gradientBrush, r);
      }

      "Peter Duniho" wrote:
      On Fri, 15 Aug 2008 17:47:12 -0700, Ryan <Ryan@discussio ns.microsoft.co m>
      wrote:
      >
      Why do I sometimes get this error; works fine most of the time:
      Rectangle '{X=0,Y=0,Width =0,Height=0}' cannot have a width or height
      equal
      to 0
      >
      When do you get the error? What line of code causes it? Can you post a
      concise-but-complete code sample that reliably reproduces the problem?
      >
      AFAIK Graphics.FillRe ctangle has no limitation on using an empty
      Rectangle. But as near as I can tell, LinearGradientB rush does. If you
      pass it an empty rectangle, it will throw the exception you describe.
      >
      I don't know why you have a control instance that is empty when it tries
      to paint itself, but the solution would be to either not let it have an
      empty rectangle as its dimensions, or to check the rectangle in the
      OnPaint() method and just not try to draw anything if the ClientRectangle
      is empty.
      >
      Pete
      >

      Comment

      • Peter Duniho

        #4
        Re: Rectangle/OnPaint Error in 2005

        On Mon, 18 Aug 2008 07:24:04 -0700, Ryan <Ryan@discussio ns.microsoft.co m>
        wrote:
        Peter,
        The following line produces the error
        >
        LinearGradientB rush gradientBrush = new LinearGradientB rush(r,
        Color.Navy,
        Color.Chocolate , LinearGradientM ode.ForwardDiag onal);
        Well, since I'd already guessed that, I can't say that adds much.
        Complete code:
        >
        void OnPaint(Object sender, PaintEventArgs e)
        {
        Graphics g = e.Graphics;
        Rectangle r = ClientRectangle ;
        LinearGradientB rush gradientBrush = new LinearGradientB rush(r,
        Color.Navy, Color.Chocolate , LinearGradientM ode.ForwardDiag onal);
        g.FillRectangle (gradientBrush, r);
        }
        That's not complete code. Please see Jon Skeet's discussion on the topic
        of code samples, both complete and incomplete:



        In any case, I believe that my previous reply included enough information
        to help you fix the problem. Was there something about that reply you
        didn't understand?

        Pete

        Comment

        • =?Utf-8?B?Unlhbg==?=

          #5
          Re: Rectangle/OnPaint Error in 2005

          Peter,
          Your previous reply had given me enough information but I posted the OnPaint
          method for your reference to get moer info.
          The error is interminent and very difficult to reproduce; I have added the
          follo
          Thanks for your help.

          I have modified the code as follows:
          void OnPaint(Object sender, PaintEventArgs e)
          {
          Graphics g = e.Graphics;
          Rectangle r = ClientRectangle ;
          if (!r.IsEmpty)
          {
          LinearGradientB rush gradientBrush = new
          LinearGradientB rush(r, Color.Navy, Color.Chocolate ,
          LinearGradientM ode.ForwardDiag onal);
          g.FillRectangle (gradientBrush, r);
          }
          }


          "Peter Duniho" wrote:
          On Mon, 18 Aug 2008 07:24:04 -0700, Ryan <Ryan@discussio ns.microsoft.co m>
          wrote:
          >
          Peter,
          The following line produces the error

          LinearGradientB rush gradientBrush = new LinearGradientB rush(r,
          Color.Navy,
          Color.Chocolate , LinearGradientM ode.ForwardDiag onal);
          >
          Well, since I'd already guessed that, I can't say that adds much.
          >
          Complete code:

          void OnPaint(Object sender, PaintEventArgs e)
          {
          Graphics g = e.Graphics;
          Rectangle r = ClientRectangle ;
          LinearGradientB rush gradientBrush = new LinearGradientB rush(r,
          Color.Navy, Color.Chocolate , LinearGradientM ode.ForwardDiag onal);
          g.FillRectangle (gradientBrush, r);
          }
          >
          That's not complete code. Please see Jon Skeet's discussion on the topic
          of code samples, both complete and incomplete:


          >
          In any case, I believe that my previous reply included enough information
          to help you fix the problem. Was there something about that reply you
          didn't understand?
          >
          Pete
          >

          Comment

          Working...