1

I am getting conflicting Google results and I wondered if this can be clarified please?

I have:

typeof(TableLayoutPanel)
   .GetProperty("DoubleBuffered",
      System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
    .SetValue(tableLayoutPanel, true, null);

typeof(TableLayoutPanel)
    .GetMethod("SetStyle",
      System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
      .Invoke(tableLayoutPanel, new object[] { ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true });

But I have been told that if I set DoubleBuffered to true that I do not need to manually set the 3 styles as the system will do that internally. At the moment I am calling both.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • Can't you just test it and see? Also, this whole experiment would be made simpler if you get rid of the reflection code. Let us know how you go :) –  Jun 05 '16 at 11:21
  • Well, going off-topic here, but when I derive my own class from TableLayoutPanel and then use that on my form, I get errors with displaying the form. That is why I used reflection code. – Andrew Truckle Jun 05 '16 at 11:25
  • @AndrewTruckle I'd think that reflection only hides the errors, while not attacking the cause. The `DoubleBuffered` property is protected, so when you derive your class you should be able to access it. – MicroVirus Jun 05 '16 at 11:35
  • @MicroVirus All I know is that when I derive a class, and then drag that into my form, the designer IDE is not happy. – Andrew Truckle Jun 05 '16 at 11:41
  • Well it usually doesn't act up as long as you don't code the `OnPaint` event with funny dependencies in it. If you do and need that look into the DesignMode property! A simple `class DrawPanel : Panel { public DrawPanel() { DoubleBuffered = true;}}` should work just fine.. – TaW Jun 05 '16 at 12:00
  • 2
    Control.SetStyle() is not a method with a high user-friendliness rating, to put it mildly. So they added the DoubleBuffered property to make it a lot easier. ResizeRedraw is another one. Always favor deriving your own class to access protected properties over hacking them with reflection. You can never start early enough with inheritance when you use Winforms, it is a crucial technique to make the object model work for you. – Hans Passant Jun 05 '16 at 12:26

1 Answers1

2

From the Reference Source here's the implementation of the DoubleBuffered property:

protected virtual bool DoubleBuffered {
    get {
        return GetStyle(ControlStyles.OptimizedDoubleBuffer);
    }
    set {
        if (value != DoubleBuffered) {
            if (value) {
                SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, value);
            }
            else {
                SetStyle(ControlStyles.OptimizedDoubleBuffer, value);
            }
        }
    }
}

(Note that the property is inherited, so you have to go back to the Control class to find it.)

theB
  • 6,450
  • 1
  • 28
  • 38