0

I am creating a program where users are able to use a GUI to create configuration files that bind commands to certain keys for a different unrelated program. In this program, I have a checkbox that allows the user to chose to use the DVORAK layout or the QWERTY layout. I have PictureBox-es being used for the image of each key. When the user checks the checkbox for the DVORAK layout, the program rearranges these pictureboxes so that they are now in the DVORAK layout instead of the QWERTY format. When this happens, about 90% of the time a few of the keys are drawn before they have moved, leaving 1 frame where there are keys overlapping or missing keys, causing a sort of "flicker".

I was wondering if there is any way to wait until right after the form's draw call has finished and then rearrange the keys to give the maximum possible amount of time for them to rearrange. I would need to be able to know the time until the next draw call (or if there is a constant amount of time inbetween every draw call, the time since the last draw call would also work) wait that amount of time, and then rearrange the pictureboxes.

Thank you for any and all help!

1 Answers1

0

I had a similar problem with TreeViews. And, like TreeViews, PictureBox doesn't have a DoubleBuffered property. So I made a new control, using TreeView as the base.

Here's a PictureBox version of that class:

public class PictureBoxNoFlicker : PictureBox {
    public PictureBoxNoFlicker() {
        this.SetStyle(ControlStyles.DoubleBuffer, true);
        this.SetStyle(ControlStyles.UserPaint, true);
        this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    }
} 

This code replicates double-buffering. Simply use this control instead of the standard PictureBox.

Unfortunately, I can't reproduce your problem, so I am taking a bit of a punt and hoping this resolves your issue.

Ulric
  • 826
  • 5
  • 16
  • Thank you very much! This eliminated the problem with two controls being in the same spot and both being drawn at once (one for part of the draw frame, one for the other part) causing some of the flickering. It still however left the flickering in terms of "absent spots" where a picturebox would be gone because it had moved, and thus there was a blank spot. However as Hans Passant pointed out, the form needed to be double buffered as well, so I made a new class that extended the current form and implemented the same methods that you had, and everything works perfect! Thank you both!:) – Nicholas Ellingson Apr 02 '15 at 00:21
  • 1
    Glad I could help. :) Regarding your form extension, Forms have their DoubleBuffered property exposed. So you can just set DoubleBuffered to 'true' in the form designer's Properties window. – Ulric Apr 02 '15 at 00:32
  • The PictureBox has the DoubleBuffer property set to true internally by default. – LarsTech Apr 02 '15 at 14:21
  • If that is true, then how come it flickers? – Ulric Apr 02 '15 at 15:21