Click here to Skip to main content
15,892,298 members
Home / Discussions / C#
   

C#

 
AnswerRe: Hashtable vs Dictionary. Pin
Matthew Cuba6-Sep-07 4:47
Matthew Cuba6-Sep-07 4:47 
GeneralRe: Hashtable vs Dictionary. Pin
Jason Hanford-Smith6-Sep-07 7:13
Jason Hanford-Smith6-Sep-07 7:13 
GeneralRe: Hashtable vs Dictionary. Pin
Matthew Cuba6-Sep-07 7:32
Matthew Cuba6-Sep-07 7:32 
GeneralRe: Hashtable vs Dictionary. Pin
Jason Hanford-Smith6-Sep-07 7:43
Jason Hanford-Smith6-Sep-07 7:43 
GeneralRe: Hashtable vs Dictionary. Pin
Matthew Cuba6-Sep-07 7:53
Matthew Cuba6-Sep-07 7:53 
GeneralRe: Hashtable vs Dictionary. Pin
Jason Hanford-Smith6-Sep-07 9:12
Jason Hanford-Smith6-Sep-07 9:12 
GeneralRe: Hashtable vs Dictionary. Pin
Matthew Cuba6-Sep-07 9:18
Matthew Cuba6-Sep-07 9:18 
QuestionFlickering PictureBoxes in C# Pin
Ermak866-Sep-07 1:59
Ermak866-Sep-07 1:59 
Hi.
I've written a small program in which are 150 pictureBoxes. After clicking the button, boxes locations are randomly changed. But everything is flickering. Is there a way to make it smooth?

A have set form style to double buffering.

It works fine when the program updates form every 10-30 changed boxes but then it lasts long. (code in comment in button1_Click).

Problem is that Box background is firstly drawn in form background color and after is drawn in PictureBox.BackColor.

Thanks for reply

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Flickering
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.SetStyle( 
                ControlStyles.UserPaint | 
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.OptimizedDoubleBuffer, true);
            this.UpdateStyles();

            CreatePictureBoxes();
        }

        private void CreatePictureBoxes()
        {
            Random rand = new Random();

            for (int i = 0; i < 150; i++)
            {
                PictureBox pictureBox = new PictureBox();
                pictureBox.BackColor = Color.Red;
                pictureBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                pictureBox.Location = new Point(rand.Next(200), rand.Next(200));
                pictureBox.Name = "pictureBox" + i;
                pictureBox.Size = new System.Drawing.Size(20,20);
                pictureBox.TabIndex = 0;
                pictureBox.TabStop = false;

                this.Controls.Add(pictureBox);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Random rand = new Random();

            for (int i = 0; i < 150; i++)
            {
                if (Controls[i] != (Control)button1)
                {
                    this.Controls[i].Location = new Point(rand.Next(200), rand.Next(200));
                    //if( i % 10 == 0 ) this.Update();
                }
            }
        }
    }
}

namespace Flickering
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(207, 234);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(71, 26);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 264);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
    }
}

AnswerRe: Flickering PictureBoxes in C# Pin
Christian Graus6-Sep-07 2:11
protectorChristian Graus6-Sep-07 2:11 
GeneralRe: Flickering PictureBoxes in C# Pin
Ermak866-Sep-07 2:14
Ermak866-Sep-07 2:14 
GeneralRe: Flickering PictureBoxes in C# Pin
Christian Graus6-Sep-07 2:39
protectorChristian Graus6-Sep-07 2:39 
QuestionCash for Help! Pin
MicealG6-Sep-07 1:42
MicealG6-Sep-07 1:42 
JokeRe: Cash for Help! Pin
Sandeep Akhare6-Sep-07 1:46
Sandeep Akhare6-Sep-07 1:46 
GeneralRe: Cash for Help! Pin
MicealG6-Sep-07 1:50
MicealG6-Sep-07 1:50 
GeneralRe: Cash for Help! [modified] Pin
Sandeep Akhare6-Sep-07 2:00
Sandeep Akhare6-Sep-07 2:00 
GeneralRe: Cash for Help! Pin
MicealG6-Sep-07 3:37
MicealG6-Sep-07 3:37 
AnswerRe: Cash for Help! Pin
Ravi Bhavnani6-Sep-07 1:54
professionalRavi Bhavnani6-Sep-07 1:54 
GeneralStill No Answer Pin
MicealG6-Sep-07 2:01
MicealG6-Sep-07 2:01 
AnswerRe: Cash for Help! Pin
J4amieC6-Sep-07 3:53
J4amieC6-Sep-07 3:53 
GeneralRe: Cash for Help! Pin
MicealG6-Sep-07 3:57
MicealG6-Sep-07 3:57 
GeneralRe: Cash for Help! Pin
J4amieC6-Sep-07 4:13
J4amieC6-Sep-07 4:13 
GeneralRe: Cash for Help! Pin
MicealG6-Sep-07 4:43
MicealG6-Sep-07 4:43 
GeneralRe: Cash for Help! Pin
J4amieC6-Sep-07 4:49
J4amieC6-Sep-07 4:49 
GeneralRe: Cash for Help! Pin
MicealG6-Sep-07 4:58
MicealG6-Sep-07 4:58 
GeneralRe: Cash for Help! Pin
J4amieC6-Sep-07 5:06
J4amieC6-Sep-07 5:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.