Click here to Skip to main content
14,978,337 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Could you please help me fix that bad performance? My form has an image as its BackgroundImage, I use a timer to increase the size of the form gradually to a certain size. Then it will stop and recover the size as before starting the timer. But during increasing the size, my form's BackgroundImage was flickering. How to make it not like that?
Thank you so much!
Posted
Updated 9-Oct-16 5:36am

You could try setting the Forms DoubleBuffered Property to true.
   
Comments
Manfred Rudolf Bihy 13-May-11 4:51am
   
Sounds plausible! 5+
   
A follow-up question is also answered.
--SA
[no name] 13-May-11 5:15am
   
But I can't access to that protected property. I should tell you more about my code. I have the main form named "Form1" (added by the designer) and the form I mentioned in the OP, it names "F", it is created by code (Form F = new Form()). The form F is a data member of the class Form1. Now anywhere in the class Form1, I can access to DoubleBuffered (this.DoubleBuffered), but that's not I want, I want to do the same to the form F but I can't access to the DoubleBuffered of the form F, what should I do?
Thank you so much!
   
Yes you can!

Wayne, you're right, my 5, but King Boy is confused about access to this feature, which needs explanation.
I answered, please see my "add-on" solution.
--SA
BobJanova 13-May-11 5:33am
   
Short answer (read SAK's solution for the long): make your 'F' form an instance of a derived class, and set the ControlStyle in the constructor.
udhayaCSE from Chennai 20-Jan-14 7:06am
   
Works fine for me
[no name] 13-May-11 6:52am
   
Thank you! I think I may have some confusion with "protected", in Cpp, I remember that it sets up a direct relation of based and derived classes. I emphasize the word "direct" and I have assumed that to C#, if so we can't access DoubleBuffered (a protected property of the class Control) in a class not derived from the class Control, but in fact we can do it. That's why I may think "direct relation" is not abode in C#, is it?
Thank you so much!
Answering follow-up question:

But I can't access to that protected property.
You can always access and set double buffer feature.

For controls (including Form): do not use predefined control. Create a derived class and use it in your form instead of original one. Use System.Windows.Forms.Control.SetStyle. Add the style System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer | System.Windows.Forms.ControlStyles.AllPaintingInWmPaint.

As to the Form — what are you talking about? You already use the class derived from System.Windows.Forms.Form, so any protected members are accessible to you.

That said — consider your flicker problem solver. Try it!

—SA
   
v2
Comments
[no name] 13-May-11 6:14am
   
Thank you SA, once again you help me out ! Your trick was mainly to help me access to the protected method "Control.SetStyle", once I can do that, the flicker should disappear. But I really can't differentiate "private" and "protected" in this case. They seems to be the same. As I have thought before, "protected" means any objects (like my form "F") of the directly inherited class (like the class Form) will be able to access to the protected members (like DoubleBuffered or SetStyle) of the based class (like the class Control). I think the class Form doesn't inherit Control directly (but through ScrollableControl and ContainerControl first), so we should've never been able to access to DoubleBuffered anywhere (but I can do that in the class Form1 inherited Form, by "this.DoubleBuffered"), I tried creating a new object of Form1 and accessing to DoubleBuffered outside the class but I couldn't. That's why I think "protected" is rather like "private" in this case? Or I lack of some knowledge about the modifier "protected"?
Thank you so much for solving my problem, also to the Solution1 giver!
If you can't get what I want to mean, please let me know, I will post some code to make the problem clearer!
   
No. Just read about it.

Private -- only this class, Internal -- adds all derived classes, public -- access from everywhere, internal -- same as public but no access from other assemblies, internal protected -- same as protected but no access from other assemblies. 5 of them, all different.

Now, "Form doesn't inherit Control directly". Directly or not -- it is derived from Control. For accessibility directly==indirectly.

Is this clear? I think I understood all of your concerns here and addressed them all. Is it clear now?
--SA
[no name] 13-May-11 8:53am
   
All modifiers are clear to me except "protected" (of course internal protected is a combined use). To confirm what I have learnt, I would like to give a example. Suppose that I have 4 classes D derived from C, C derived from B, B derived from A and "a" is a protected member of A. In C++, I have learnt that we can only access to "a" in the classes A (of course) and B. We can't access it in the classes C and D. But In C#, we can access "a" in all 4 classes.
If you don't mind, please affirm the above for me?
Thank you so much!
Sergey Alexandrovich Kryukov 13-May-11 11:49am
   
Correct. Write those few lines of code and you will see that "a" is accessible in all classes. Not only in C# and not only in .NET, but probably in all OOP. Direct or indirectly inherited -- does not matter. If you think about it, it's practically reasonable. As I say, for accessibility, directly==indirectly.
--SA
You can also print your control into some image cache in RAM and then export it onto form's drawing surface ... that would decrease flickering
   
v2
Comments
Adra Fkja 22-Oct-12 13:16pm
   
how ?
Add the code
this.ResizeRedraw = true;
to the Load event and in the ResizeEnd event add the following code
this.Refresh();
   
i use code like below

VB.NET
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize
      Dim bgfile = Me.BackgroundImage
      Me.SuspendLayout()
      Me.BackgroundImage = Nothing
      Me.BackgroundImage = bgfile
      Me.ResumeLayout()
  End Sub
   
Comments
[no name] 9-Oct-16 11:40am
   
There is no real reason to answer FIVE year old already answered questions.
Wrangly 24-Nov-20 11:13am
   
yes, there are some reasons ! have the same problem in 2020 and found this reply which working fine (after converting to c#) so thanks for this answer !!!
satheeshkumar2022 6-Aug-17 4:45am
   
Its working well

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900