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. Form flickering problem

Form flickering problem

Scheduled Pinned Locked Moved C#
helpquestion
11 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.
  • S SimpleData

    Hi, My application has a heavy form (meaning with lots of images etc.) and after I minimize it and then maximize it the form flickers for 2-3 seconds. I've tried to set DoubleBuffered property of my form to true and tried all kinds of SetStyle combinations but nothing helped. Is there anyone out there who has any idea how I can solve this problem or workaround it? Thanks in advance.

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #2

    Hi, please tell us more. 1. what are the Controls on the Form? how many? UserControls? nested UserControls? 2. what is showing the images? are they overlapping? 3. trying some transparency stuff? 4. what code is in the Resize handler? any Controls docked, or anchored on both ends so they stretch? :)

    Luc Pattyn


    I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


    Local announcement (Antwerp region): Lange Wapper? Neen!


    S 1 Reply Last reply
    0
    • L Luc Pattyn

      Hi, please tell us more. 1. what are the Controls on the Form? how many? UserControls? nested UserControls? 2. what is showing the images? are they overlapping? 3. trying some transparency stuff? 4. what code is in the Resize handler? any Controls docked, or anchored on both ends so they stretch? :)

      Luc Pattyn


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      Local announcement (Antwerp region): Lange Wapper? Neen!


      S Offline
      S Offline
      SimpleData
      wrote on last edited by
      #3

      Hi, My form contains 2-3 panels, over 7 pictureboxes, lots of labels and groupboxes, one progressbar and one button. There are no overlapping images but some images have transparent backgrounds. There is no code in resize handler and my form is not resizeable. Form's FormBorderStyle is set to none.

      L 1 Reply Last reply
      0
      • S SimpleData

        Hi, My form contains 2-3 panels, over 7 pictureboxes, lots of labels and groupboxes, one progressbar and one button. There are no overlapping images but some images have transparent backgrounds. There is no code in resize handler and my form is not resizeable. Form's FormBorderStyle is set to none.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #4

        OK, so you don't have list stuff (ListBox, ListView, TreeView, DataGridView), that is good. here are things that help in reducing flicker: - keep the GUI simple (keep the number of Controls below say 50) - make sure you have high-performance code in the paint handlers, if any (don't create too many objects, Pens, Fonts, ...; and if you have those, don't forget to Dispose the ones you created) - use the Graphics from PaintEventArgs, don't use CreateGraphics - all the above should result in fast painting - apply double-buffering, which almost completely hides the painting work These work against you: - PictureBoxes (PB is a stupid Control, I prefer to paint images myself, in a Paint handler) - IIRC: transparancy in general, objects (e.g. Labels) on top of something transparant in particular this normally helps: - make the Form double-buffered by inserting this in its constructor:

        SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer,true);
        SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint,true);
        SetStyle(System.Windows.Forms.ControlStyles.UserPaint,true);
        

        (if such code is outside constructor it also needs a Control.UpdateStyles) - avoid huge images, even when shown in small size (heavy rescale = CPU cycles); maybe calculate the right sized image once and keep it around. - load the image from disk once (outside all Paint handlers) and keep it as an object. :)

        Luc Pattyn


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        Local announcement (Antwerp region): Lange Wapper? Neen!


        modified on Saturday, October 17, 2009 8:16 AM

        S L 2 Replies Last reply
        0
        • L Luc Pattyn

          OK, so you don't have list stuff (ListBox, ListView, TreeView, DataGridView), that is good. here are things that help in reducing flicker: - keep the GUI simple (keep the number of Controls below say 50) - make sure you have high-performance code in the paint handlers, if any (don't create too many objects, Pens, Fonts, ...; and if you have those, don't forget to Dispose the ones you created) - use the Graphics from PaintEventArgs, don't use CreateGraphics - all the above should result in fast painting - apply double-buffering, which almost completely hides the painting work These work against you: - PictureBoxes (PB is a stupid Control, I prefer to paint images myself, in a Paint handler) - IIRC: transparancy in general, objects (e.g. Labels) on top of something transparant in particular this normally helps: - make the Form double-buffered by inserting this in its constructor:

          SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer,true);
          SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint,true);
          SetStyle(System.Windows.Forms.ControlStyles.UserPaint,true);
          

          (if such code is outside constructor it also needs a Control.UpdateStyles) - avoid huge images, even when shown in small size (heavy rescale = CPU cycles); maybe calculate the right sized image once and keep it around. - load the image from disk once (outside all Paint handlers) and keep it as an object. :)

          Luc Pattyn


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          Local announcement (Antwerp region): Lange Wapper? Neen!


          modified on Saturday, October 17, 2009 8:16 AM

          S Offline
          S Offline
          SimpleData
          wrote on last edited by
          #5

          Thank you for your advices. They really helped me and the form. :D It doesn't have to struggle anymore. ;)

          L 1 Reply Last reply
          0
          • S SimpleData

            Thank you for your advices. They really helped me and the form. :D It doesn't have to struggle anymore. ;)

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #6

            you're welcome. :)

            Luc Pattyn


            I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


            Local announcement (Antwerp region): Lange Wapper? Neen!


            1 Reply Last reply
            0
            • L Luc Pattyn

              OK, so you don't have list stuff (ListBox, ListView, TreeView, DataGridView), that is good. here are things that help in reducing flicker: - keep the GUI simple (keep the number of Controls below say 50) - make sure you have high-performance code in the paint handlers, if any (don't create too many objects, Pens, Fonts, ...; and if you have those, don't forget to Dispose the ones you created) - use the Graphics from PaintEventArgs, don't use CreateGraphics - all the above should result in fast painting - apply double-buffering, which almost completely hides the painting work These work against you: - PictureBoxes (PB is a stupid Control, I prefer to paint images myself, in a Paint handler) - IIRC: transparancy in general, objects (e.g. Labels) on top of something transparant in particular this normally helps: - make the Form double-buffered by inserting this in its constructor:

              SetStyle(System.Windows.Forms.ControlStyles.DoubleBuffer,true);
              SetStyle(System.Windows.Forms.ControlStyles.AllPaintingInWmPaint,true);
              SetStyle(System.Windows.Forms.ControlStyles.UserPaint,true);
              

              (if such code is outside constructor it also needs a Control.UpdateStyles) - avoid huge images, even when shown in small size (heavy rescale = CPU cycles); maybe calculate the right sized image once and keep it around. - load the image from disk once (outside all Paint handlers) and keep it as an object. :)

              Luc Pattyn


              I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


              Local announcement (Antwerp region): Lange Wapper? Neen!


              modified on Saturday, October 17, 2009 8:16 AM

              L Offline
              L Offline
              Lyon Sun
              wrote on last edited by
              #7

              Luc Pattyn wrote:

              if you have those, don't forget to Dispose the ones you created

              Hi, What if my app has values need to be passed? does this.Dispose() going to erase them? Sun

              L 1 Reply Last reply
              0
              • L Lyon Sun

                Luc Pattyn wrote:

                if you have those, don't forget to Dispose the ones you created

                Hi, What if my app has values need to be passed? does this.Dispose() going to erase them? Sun

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #8

                Hi, IMO this.Dispose() does not make any sense, an object cannot dispose itself, as it would have to be alive to execute the next line of code. This is what I meant:

                public void Paint(object sender, PaintEventArgs e) {
                Graphics g=e.Graphics;
                Font myFont=new Font(...); <<<<<<<<<<<<<<<<<<<<<<<
                g.DrawString(..., myFont, ...);
                ...
                myFont.Dispose(); <<<<<<<<<<<<<<<<<<<<<<<
                }

                :)

                Luc Pattyn


                I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                Local announcement (Antwerp region): Lange Wapper? Neen!


                L 1 Reply Last reply
                0
                • L Luc Pattyn

                  Hi, IMO this.Dispose() does not make any sense, an object cannot dispose itself, as it would have to be alive to execute the next line of code. This is what I meant:

                  public void Paint(object sender, PaintEventArgs e) {
                  Graphics g=e.Graphics;
                  Font myFont=new Font(...); <<<<<<<<<<<<<<<<<<<<<<<
                  g.DrawString(..., myFont, ...);
                  ...
                  myFont.Dispose(); <<<<<<<<<<<<<<<<<<<<<<<
                  }

                  :)

                  Luc Pattyn


                  I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                  Local announcement (Antwerp region): Lange Wapper? Neen!


                  L Offline
                  L Offline
                  Lyon Sun
                  wrote on last edited by
                  #9

                  Luc Pattyn wrote:

                  IMO this.Dispose() does not make any sense,

                  Hi, Sorry if I said it wrong, but when I mentioned this.Dispose(), this = the main form. But thank you, Luc, I got your point. Again, I have another question, what if I set a new Font for one control, for example, btn_OK, am I doing it rightly by the code below:

                  btn_OK.Font = new Font(....);
                  ...
                  btn_OK.Font.Dispose();

                  Thanks in advance!;) Sun

                  L 1 Reply Last reply
                  0
                  • L Lyon Sun

                    Luc Pattyn wrote:

                    IMO this.Dispose() does not make any sense,

                    Hi, Sorry if I said it wrong, but when I mentioned this.Dispose(), this = the main form. But thank you, Luc, I got your point. Again, I have another question, what if I set a new Font for one control, for example, btn_OK, am I doing it rightly by the code below:

                    btn_OK.Font = new Font(....);
                    ...
                    btn_OK.Font.Dispose();

                    Thanks in advance!;) Sun

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #10

                    Leapsword Sun wrote:

                    btn_OK.Font = new Font(....); ... btn_OK.Font.Dispose();

                    assuming all that is code in a single method, no it does not make sense: assuming the button is visible on some form, the font is in use, so you shouldn't dispose of it at that time. :)

                    Luc Pattyn


                    I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                    Local announcement (Antwerp region): Lange Wapper? Neen!


                    L 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      Leapsword Sun wrote:

                      btn_OK.Font = new Font(....); ... btn_OK.Font.Dispose();

                      assuming all that is code in a single method, no it does not make sense: assuming the button is visible on some form, the font is in use, so you shouldn't dispose of it at that time. :)

                      Luc Pattyn


                      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                      Local announcement (Antwerp region): Lange Wapper? Neen!


                      L Offline
                      L Offline
                      Lyon Sun
                      wrote on last edited by
                      #11

                      Luc Pattyn wrote:

                      assuming the button is visible on some form,

                      Hi, what if I create this button btn_OK dynamically in my codes then? Thanks for your patient so much! ;) Sun

                      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