Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Things are not working as expected

Things are not working as expected

Scheduled Pinned Locked Moved C#
csharpgraphicshelpc++html
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gUrM33T
    wrote on last edited by
    #1

    hey guys, can any1 please tell me wtf is wrong with the following code :((:

    private void panelRightView_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
    System.Drawing.Graphics oGfx = this.panelRightView.CreateGraphics();
    System.Drawing.Rectangle rect = this.picTitleBar.Bounds;
    oGfx.DrawLine(_penLine, rect.Right, rect.Bottom, this.panelRightView.ClientSize.Width - rect.Right, rect.Bottom);
    }

    The scenario is like this: I've two panels on my Windows form panelLeftView (LEFT docking) and panelRightView (FILL docking). In my right panel's PAINT event handler, I need to draw a f@#king line. The line draws ok, works exactly as I want, it expands when I increase the width of the form, but the problem is that this code doesn't work if I decrease the width of the form :mad:. The line previously drawn is not cleared when i reduce the form width. Can any .Net guru (especially of C#) tell me why is this happening? Am I doing something wrong :~:confused: ? Or is that problem occuring only to remind me that I'm working on Microsoft's technology where nothing seems to work as expected X| :mad::mad:??? Please Help. Thanks Gurmeet S. Kochar


    If you believe in God, it's because of the Devil

    My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control

    J G 2 Replies Last reply
    0
    • G gUrM33T

      hey guys, can any1 please tell me wtf is wrong with the following code :((:

      private void panelRightView_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
      {
      System.Drawing.Graphics oGfx = this.panelRightView.CreateGraphics();
      System.Drawing.Rectangle rect = this.picTitleBar.Bounds;
      oGfx.DrawLine(_penLine, rect.Right, rect.Bottom, this.panelRightView.ClientSize.Width - rect.Right, rect.Bottom);
      }

      The scenario is like this: I've two panels on my Windows form panelLeftView (LEFT docking) and panelRightView (FILL docking). In my right panel's PAINT event handler, I need to draw a f@#king line. The line draws ok, works exactly as I want, it expands when I increase the width of the form, but the problem is that this code doesn't work if I decrease the width of the form :mad:. The line previously drawn is not cleared when i reduce the form width. Can any .Net guru (especially of C#) tell me why is this happening? Am I doing something wrong :~:confused: ? Or is that problem occuring only to remind me that I'm working on Microsoft's technology where nothing seems to work as expected X| :mad::mad:??? Please Help. Thanks Gurmeet S. Kochar


      If you believe in God, it's because of the Devil

      My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control

      J Offline
      J Offline
      Jon G
      wrote on last edited by
      #2

      When resizing the form , you NEED to redraw the control, otherwise it will only display what is in the video memory. My terminology is probably not 100% accurate, but all you really need to know is that you need to refresh your form, and it will work. just do: this.refresh();

      G 1 Reply Last reply
      0
      • G gUrM33T

        hey guys, can any1 please tell me wtf is wrong with the following code :((:

        private void panelRightView_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
        System.Drawing.Graphics oGfx = this.panelRightView.CreateGraphics();
        System.Drawing.Rectangle rect = this.picTitleBar.Bounds;
        oGfx.DrawLine(_penLine, rect.Right, rect.Bottom, this.panelRightView.ClientSize.Width - rect.Right, rect.Bottom);
        }

        The scenario is like this: I've two panels on my Windows form panelLeftView (LEFT docking) and panelRightView (FILL docking). In my right panel's PAINT event handler, I need to draw a f@#king line. The line draws ok, works exactly as I want, it expands when I increase the width of the form, but the problem is that this code doesn't work if I decrease the width of the form :mad:. The line previously drawn is not cleared when i reduce the form width. Can any .Net guru (especially of C#) tell me why is this happening? Am I doing something wrong :~:confused: ? Or is that problem occuring only to remind me that I'm working on Microsoft's technology where nothing seems to work as expected X| :mad::mad:??? Please Help. Thanks Gurmeet S. Kochar


        If you believe in God, it's because of the Devil

        My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control

        G Offline
        G Offline
        gUrM33T
        wrote on last edited by
        #3

        BTW, I'm issuing this.panelRightView.CreateGraphics() instead of e.Graphics because in the latter case, the line only draw once and then doesn't expand at all when form's width is increased. I dunno the reason but .... Gurmeet S. Kochar


        If you believe in God, it's because of the Devil

        My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control

        H 1 Reply Last reply
        0
        • J Jon G

          When resizing the form , you NEED to redraw the control, otherwise it will only display what is in the video memory. My terminology is probably not 100% accurate, but all you really need to know is that you need to refresh your form, and it will work. just do: this.refresh();

          G Offline
          G Offline
          gUrM33T
          wrote on last edited by
          #4

          First of all, I want to thank you for a super quick reply. And second, I again want to thank you for giving me the perfect answer. Yes, it worked. Thank you very much Gurmeet S. Kochar


          If you believe in God, it's because of the Devil

          My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control

          1 Reply Last reply
          0
          • G gUrM33T

            BTW, I'm issuing this.panelRightView.CreateGraphics() instead of e.Graphics because in the latter case, the line only draw once and then doesn't expand at all when form's width is increased. I dunno the reason but .... Gurmeet S. Kochar


            If you believe in God, it's because of the Devil

            My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            You need to force the control to refresh when resized and DO NOT create a new Graphics object in the Paint event handler. Besides, even if you do, you must call Graphics.Dispose on it or you're going to find your memory consumption increasing and performance decreasing. To force your control to repaint itself when it resizes, you can do this a number of ways. The easiest is just to call SetStyle(ControlStyles.ResizeRedraw, true); in your constructor. You could also set the protected ResizeRedraw property to true in your constructor. These result in the same effect.

            Microsoft MVP, Visual C# My Articles

            G 1 Reply Last reply
            0
            • H Heath Stewart

              You need to force the control to refresh when resized and DO NOT create a new Graphics object in the Paint event handler. Besides, even if you do, you must call Graphics.Dispose on it or you're going to find your memory consumption increasing and performance decreasing. To force your control to repaint itself when it resizes, you can do this a number of ways. The easiest is just to call SetStyle(ControlStyles.ResizeRedraw, true); in your constructor. You could also set the protected ResizeRedraw property to true in your constructor. These result in the same effect.

              Microsoft MVP, Visual C# My Articles

              G Offline
              G Offline
              gUrM33T
              wrote on last edited by
              #6

              Heath Stewart wrote: Besides, even if you do, you must call Graphics.Dispose on it or you're going to find your memory consumption increasing and performance decreasing Hey, what about that Garbage Collection thing? :confused: Heath Stewart wrote: To force your control to repaint itself when it resizes, you can do this a number of ways Well, I tried using SetStyle but it says "Cannot access protected member 'System.Windows.Forms.Control.SetStyle(System.Windows.Forms.ControlStyles, bool)' via a qualifier of type 'Panel'; the qualifier must be of type 'FormPackages' (or derived from it)". Gurmeet S. Kochar


              If you believe in God, it's because of the Devil

              My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control

              H 1 Reply Last reply
              0
              • G gUrM33T

                Heath Stewart wrote: Besides, even if you do, you must call Graphics.Dispose on it or you're going to find your memory consumption increasing and performance decreasing Hey, what about that Garbage Collection thing? :confused: Heath Stewart wrote: To force your control to repaint itself when it resizes, you can do this a number of ways Well, I tried using SetStyle but it says "Cannot access protected member 'System.Windows.Forms.Control.SetStyle(System.Windows.Forms.ControlStyles, bool)' via a qualifier of type 'Panel'; the qualifier must be of type 'FormPackages' (or derived from it)". Gurmeet S. Kochar


                If you believe in God, it's because of the Devil

                My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control

                H Offline
                H Offline
                Heath Stewart
                wrote on last edited by
                #7

                Gurmeet S. Kochar wrote: Hey, what about that Garbage Collection thing? The Graphics class - like many other class in the .NET FCL - use native resources (unmanaged) so the GC cannot collect them. Read Programming for Garbage Collection[^] in the .NET Framework SDK to learn more. Gurmeet S. Kochar wrote: Well, I tried using SetStyle but it says... As I mentioned, you should call this in your constructor - the constructor for the control you're writing, which means you must extend the base class you want and add it to your constructor:

                public class MyPanel : Panel
                {
                public MyPanel()
                {
                SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer
                | ControlStyles.UserPaint, true);
                // ...
                }
                // ..
                }

                Microsoft MVP, Visual C# My Articles

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups