Click here to Skip to main content
15,920,650 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
image

The buttons are created dynamically.Boxes indicate a specific situation. I want to state from the list by selecting the Filter box.But when I create a screen flashing box again. Each case is canceled, the gap must be filled. re going slow when I change position.
Posted
Updated 16-Apr-14 19:55pm
v6
Comments
BillWoodruff 17-Apr-14 11:16am    
You need to give much more detail about what you are doing. Is this a WinForms app, an ASP.NET web-site ? What exactly is a "box" ?
sonercelix 17-Apr-14 12:22pm    
Windows application. Width:100px and Height:100px Panel. A form needs to be refreshed every 10 seconds. For example, a condition of the box is canceled, then the situation needs to be deleted.
New cases of status box is selected, it needs to be created.
gggustafson 17-Apr-14 16:31pm    
Why the 10 second refresh rate? Why not refresh the form when a change occurs?
sonercelix 17-Apr-14 12:23pm    
http://s1.postimg.org/mjv1vlgqn/soner.png
gggustafson 17-Apr-14 16:32pm    
I see 7 rows of 10 buttons each. Are there other controls that I do not see?


The solution that I provide here is the minimal needed to explain the solution to your problem. The complete solution can be downloaded from DynamicUpdate.



As I mentioned earlier, one way to eliminate flicker is to use double buffering. Because I do a lot of graphics programming, I've developed a class called GraphicsBuffer. It basically does pretty much everything you need. If you use the GraphicsBuffer class, you will not be faced with cross thread errors. The entry points in GraphicsBuffer that will be of interest to you are:



  • GraphicsBuffer - the class constructor
  • CreateGraphicsBuffer - completes the creation of the GraphicsBuffer object
  • DeleteGraphicsBuffer - deletes the current GraphicsBuffer instance
  • Graphic - returns the current instance's Graphics object
  • RenderGraphicsBuffer - renders the current GraphicsBuffer instance to the specified Graphics object


If you are interested in the complete GraphicsBuffer class, make a comment to this solution.



Here is the solution:


C#
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Timers;
using System.Windows.Forms;

namespace DynamicUpdate
    {

    // requires that Form1 be defined in the Designer

    // *************************************************** class Form1

    public partial class Form1 : Form
        {

        // ************************************************* constants

        static Color    BACKGROUND_COLOR = SystemColors.Control;

        const int       BOXES_PER_ROW = 10;
        const int       ROWS_IN_DISPLAY = 10;

        const int       BOX_WIDTH = 50;
        const int       BOX_HEIGHT = BOX_WIDTH;

        const int       INTER_BOX_SPACING = 5;

        const int       LABEL_OFFSET = 1;

        const int       BITMAP_OFFSET = BOX_WIDTH / 5;

        const int       BITMAP_WIDTH = ( BOXES_PER_ROW *
                                         ( INTER_BOX_SPACING  +
                                           BOX_WIDTH ) ) +
                                       INTER_BOX_SPACING ;
        const int       BITMAP_HEIGHT = BITMAP_WIDTH;

        const int       FORM1_WIDTH = BITMAP_WIDTH +
                                      ( 2 * BITMAP_OFFSET );

        const int       FORM1_HEIGHT = BITMAP_HEIGHT +
                                      ( BOX_HEIGHT );

        const double    TIMER_INTERVAL = 10000.0; // ms

        // ************************************************* variables

        GraphicsBuffer      display = null;
                                        // following two variables 
                                        // simulate a change; do not 
                                        // include them in your final
                                        // version
        Random              random = new Random ( );
        int                 random_box = 0;
                                        // without qualification the 
                                        // declaration would conflict 
                                        // with System.Windows.Forms
        System.Timers.Timer timer = null; 

        // ********************************************* OnFormClosing

        // cleanup resources

        protected override void OnFormClosing ( FormClosingEventArgs e )
            {

            base.OnFormClosing ( e );

            if ( display != null )
                {
                display = display.DeleteGraphicsBuffer ( );
                }

            if ( timer != null )
                {
                if ( timer.Enabled )
                    {
                    timer.Stop ( );
                    }
                timer.Dispose ( );
                timer = null;
                }
            }

        // ***************************************************** Form1

        public Form1 ( )
            {

            InitializeComponent ( );

            this.Width = FORM1_WIDTH;
            this.Height = FORM1_HEIGHT;
                                        // order important here!!
            this.SetStyle ( ( ControlStyles.DoubleBuffer |
                              ControlStyles.UserPaint |
                              ControlStyles.AllPaintingInWmPaint ),
                            true );
            this.UpdateStyles ( );

            timer = new System.Timers.Timer ( TIMER_INTERVAL );
            timer.Elapsed += new ElapsedEventHandler ( tick );
            timer.Start ( );

            this.Invalidate ( );
            }

        // ****************************************************** tick

        private void tick ( object    sender, 
                            EventArgs e )
            {

            this.Invalidate ( );
            }

        // ************************************ create_display_graphic

        void create_display_graphic ( )
            {

            if ( display != null )
                {
                display = display.DeleteGraphicsBuffer ( );
                }
            display = new GraphicsBuffer ( );
            display.CreateGraphicsBuffer ( BITMAP_WIDTH,
                                           BITMAP_HEIGHT );
            display.Graphic.SmoothingMode = SmoothingMode.HighQuality;

            random_box = random.Next ( 0, 100 );
            }

        // ************************************** draw_display_graphic

        void draw_display_graphic ( Graphics graphics )
            {
            int     box_count = 0;
            int     x = BITMAP_OFFSET;
            int     y = BITMAP_OFFSET;

            for ( int i = 0; ( i < ROWS_IN_DISPLAY ); i++ )
                {
                for ( int j = 0; ( j < BOXES_PER_ROW ); j++ )
                    {
                    Brush   brush = new SolidBrush ( Color.Cyan );
                    Pen     pen = new Pen ( Color.Cyan );

                    Rectangle rectangle;

                    if ( box_count == random_box )
                        {
                        brush = new SolidBrush ( Color.Pink );
                        pen = new Pen ( Color.Pink );
                        }

                    rectangle = new Rectangle ( 
                                    new Point ( x, y ),
                                    new Size ( BOX_WIDTH,
                                               BOX_HEIGHT ) );
                    graphics.DrawRectangle ( pen, rectangle );
                    graphics.FillRectangle ( brush, rectangle );
                    graphics.DrawString ( box_count.ToString ( ),
                                          SystemFonts.IconTitleFont,
                                          Brushes.Black,
                                          x + LABEL_OFFSET,
                                          y + LABEL_OFFSET );

                    pen.Dispose ( );
                    brush.Dispose ( );

                    box_count++;

                    x += ( BOX_WIDTH + INTER_BOX_SPACING );
                    }

                x = BITMAP_OFFSET;
                y += ( BOX_HEIGHT + INTER_BOX_SPACING );
                }
            }

        // *************************************************** OnPaint

        protected override void OnPaint ( PaintEventArgs e )
            {

            base.OnPaint ( e );

            e.Graphics.Clear ( BACKGROUND_COLOR );

            create_display_graphic ( );
            draw_display_graphic ( display.Graphic );

            display.RenderGraphicsBuffer ( e.Graphics );
            }

        } // class Form1

    } // namespace DynamicUpdate


Although I disagree with your method of updating your UI (I think you should use an event handler), the code uses a timer. The following describes the basic function of the various methods.



  • After the declaration of the constants and variables, appears a method override of OnFormClosing. This is required to dispose of objects created in the application that, if not disposed of, would cause a memory leak.
  • In the Form1 constructor we set the size of the form, the form's styles, declare the timer, and invoke Invalidate (causing the form to paint the first time).
  • Each time that the timer ticks, Invalidate is invoked and the OnPaint event handler is triggered.
  • The bulk of processing occurs in the OnPaint event handler that:

    1. Clears the form
    2. Causes the GraphicsBuffer to be recreated
    3. Causes the GraphicsBuffer to be drawn
    4. Renders the GraphicsBuffer to the screen



Note that the program simulates an external event by randomly causing one of the boxes to paint in Pink. This is probably where you may need additional help.



Hope that helps.

 
Share this answer
 
@gggustafson thanks for answer,

But I have problem with this Form, I create Dynamic Control(Panel).

Problem is When people click panel, color of panel is change and I update database table, and when panel color is change, I drop(remove) panel box. when I drop box, I re create all Panel controls , so screen is refresing. If I develop this project in asp.net, I use Ajax Update panel, and is not be screen refresing. Now I have always screen refresing. How can I reduce this problem?

And Do you know grapich object click events?

Thanks for answers.
 
Share this answer
 
Comments
gggustafson 16-May-14 13:22pm    
Sorry, I didn't see this until today. You should have responded through the "Have a Question or Comment" button associated with my solution. That way I would have received an email.

Let me know if you still have a problem.
gggustafson 16-May-14 13:44pm    
A Graphics does not have a click event (as a matter of fact it has no events). The objects that are drawn by the Graphics have events.

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