10 Replies - 469 Views - Last Post: 06 November 2012 - 10:48 AM Rate Topic: -----

#1 hardcoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-November 12

Moving an image in a form

Posted 06 November 2012 - 10:00 AM

I'm trying to move a square in a form, but it keeps flickering and is not smooth.
I have lines drawn using the Form1_Paint, and the flickering seems to subside a bit when I comment it out.
I'm using a timer to move the square, what am I doing wrong?

The square is drawn in an on overriden OnPaint method and I'm using base.OnPaint(e) to paint on top of it
Is This A Good Question/Topic? 0
  • +

Replies To: Moving an image in a form

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,767
  • Joined: 12-June 08

Re: Moving an image in a form

Posted 06 November 2012 - 10:08 AM

There could be an inane number of things you are doing wrong... care to share you code so we can pick it apart?
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: Moving an image in a form

Posted 06 November 2012 - 10:08 AM

Let me just attach my Psychic Debugger to your program.
Was This Post Helpful? 0
  • +
  • -

#4 hardcoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-November 12

Re: Moving an image in a form

Posted 06 November 2012 - 10:12 AM

http://pastebin.com/C1sjjCFx


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

namespace ElevatorFinal
{
    public partial class Form1 : Form
    {
        Timer t = new Timer();
        int x = 185;
        bool isUp = true;
        
        public Form1()
        {
            InitializeComponent();

            FirstFloorText.BackColor = Color.Yellow;
            FirstFloorText.Enabled = false;
            GroundFloorText.BackColor = Color.Yellow;
            GroundFloorText.Enabled = false;

            t.Interval = 50;
            t.Tick += new EventHandler(t_Tick);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics formGraphics = this.CreateGraphics();
            // draw frame with a red pen
            System.Drawing.Pen redPen;
            redPen = new System.Drawing.Pen(System.Drawing.Color.Red);
            formGraphics.DrawLine(redPen, 180, 50, 180, 310);
            formGraphics.DrawLine(redPen, 310, 50, 310, 310);
            formGraphics.DrawLine(redPen, 180, 50, 310, 50);
            formGraphics.DrawLine(redPen, 180, 180, 310, 180);
            formGraphics.DrawLine(redPen, 180, 310, 310, 310);
            redPen.Dispose();

        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            SolidBrush blueBrush = new SolidBrush(Color.Blue);
            g.FillRectangle(blueBrush, 181, x, 129, 125);

            /*if (isUp == true)
            {
                x = 185;
                
            }
            else
            {
                x = 50;
                
            }*/
        }
        void t_Tick(object sender, EventArgs e)
        {
            //t.Start();

            if (isUp == true && x >= 52)
            {
                x -= 2;
                if (x == 52)
                    t.Stop();
            }
            else if (isUp == false && x <= 180)
            {
                x += 2;
                if (x == 180)
                    t.Stop();
            }


            this.Invalidate();
        }

        private void FirstFloor_Click(object sender, EventArgs e)
        {
            t.Start();
            isUp = true;
        }

        private void GroundFloor_Click(object sender, EventArgs e)
        {
            isUp = false;
            t.Start();            
        }
    }
}




This is the code for the designer:

http://pastebin.com/RwEgRykL

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

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        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.FirstFloor = new System.Windows.Forms.Button();
            this.GroundFloor = new System.Windows.Forms.Button();
            this.GroundFloorInside = new System.Windows.Forms.Button();
            this.FirstFloorInside = new System.Windows.Forms.Button();
            this.FirstFloorText = new System.Windows.Forms.TextBox();
            this.GroundFloorText = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // FirstFloor
            // 
            this.FirstFloor.Location = new System.Drawing.Point(12, 35);
            this.FirstFloor.Name = "FirstFloor";
            this.FirstFloor.Size = new System.Drawing.Size(87, 34);
            this.FirstFloor.TabIndex = 0;
            this.FirstFloor.Text = "First Floor";
            this.FirstFloor.UseVisualStyleBackColor = true;
            this.FirstFloor.Click += new System.EventHandler(this.FirstFloor_Click);
            // 
            // GroundFloor
            // 
            this.GroundFloor.Location = new System.Drawing.Point(20, 214);
            this.GroundFloor.Name = "GroundFloor";
            this.GroundFloor.Size = new System.Drawing.Size(87, 34);
            this.GroundFloor.TabIndex = 1;
            this.GroundFloor.Text = "Ground Floor";
            this.GroundFloor.UseVisualStyleBackColor = true;
            this.GroundFloor.Click += new System.EventHandler(this.GroundFloor_Click);
            // 
            // GroundFloorInside
            // 
            this.GroundFloorInside.Location = new System.Drawing.Point(542, 175);
            this.GroundFloorInside.Name = "GroundFloorInside";
            this.GroundFloorInside.Size = new System.Drawing.Size(87, 34);
            this.GroundFloorInside.TabIndex = 2;
            this.GroundFloorInside.Text = "Ground Floor";
            this.GroundFloorInside.UseVisualStyleBackColor = true;
            // 
            // FirstFloorInside
            // 
            this.FirstFloorInside.Location = new System.Drawing.Point(542, 117);
            this.FirstFloorInside.Name = "FirstFloorInside";
            this.FirstFloorInside.Size = new System.Drawing.Size(87, 34);
            this.FirstFloorInside.TabIndex = 3;
            this.FirstFloorInside.Text = "First Floor";
            this.FirstFloorInside.UseVisualStyleBackColor = true;
            // 
            // FirstFloorText
            // 
            this.FirstFloorText.Location = new System.Drawing.Point(40, 103);
            this.FirstFloorText.Name = "FirstFloorText";
            this.FirstFloorText.Size = new System.Drawing.Size(29, 20);
            this.FirstFloorText.TabIndex = 4;
            // 
            // GroundFloorText
            // 
            this.GroundFloorText.Location = new System.Drawing.Point(40, 271);
            this.GroundFloorText.Name = "GroundFloorText";
            this.GroundFloorText.Size = new System.Drawing.Size(29, 20);
            this.GroundFloorText.TabIndex = 5;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(660, 394);
            this.Controls.Add(this.GroundFloorText);
            this.Controls.Add(this.FirstFloorText);
            this.Controls.Add(this.FirstFloorInside);
            this.Controls.Add(this.GroundFloorInside);
            this.Controls.Add(this.GroundFloor);
            this.Controls.Add(this.FirstFloor);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button FirstFloor;
        private System.Windows.Forms.Button GroundFloor;
        private System.Windows.Forms.Button GroundFloorInside;
        private System.Windows.Forms.Button FirstFloorInside;
        private System.Windows.Forms.TextBox FirstFloorText;
        private System.Windows.Forms.TextBox GroundFloorText;
    }
}


This post has been edited by modi123_1: 06 November 2012 - 10:16 AM
Reason for edit:: highlight the text and just click the 'code' button in the text editor

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,767
  • Joined: 12-June 08

Re: Moving an image in a form

Posted 06 November 2012 - 10:15 AM

*sigh* To save everyone the trouble I will update the OP's code in his/her post.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,767
  • Joined: 12-June 08

Re: Moving an image in a form

Posted 06 November 2012 - 10:21 AM

Any solid reason why you have an overridden paint and the regular paint event all customized out?

You do get that timer's interval is in milliseconds, right? 50 milliseconds is pretty quick.. why not make it half a second and go for 500ms?

Off the top of my head - try throwing these at in the load event.

SetStyle(ControlStyles.UserPaint, True)
SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
SetStyle(ControlStyles.AllPaintingInWmPaint, True)

Was This Post Helpful? 0
  • +
  • -

#7 hardcoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-November 12

Re: Moving an image in a form

Posted 06 November 2012 - 10:29 AM

there wasn't any specific reason, but I had to override the onPaint method, otherwise the square wouldn't appear.

500 ms was too slow, I wanted it to be a smooth action, but it still flickers when you put it on 500ms I also noticed that it started to flicker when the square went on top of the red line in the middle
Was This Post Helpful? 0
  • +
  • -

#8 hardcoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-November 12

Re: Moving an image in a form

Posted 06 November 2012 - 10:39 AM

when I tried this:
SetStyle(ControlStyles.UserPaint, True)
2
SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
3
SetStyle(ControlStyles.AllPaintingInWmPaint, True)


The square doesn't flicker anymore but the red lines do
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,767
  • Joined: 12-June 08

Re: Moving an image in a form

Posted 06 November 2012 - 10:43 AM

Probably because you are recreating the graphics object. Try using the graphics object found in the PaintEventArgs variable 'e'.
Was This Post Helpful? 1
  • +
  • -

#10 hardcoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-November 12

Re: Moving an image in a form

Posted 06 November 2012 - 10:47 AM

that fixed it thanks!
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,767
  • Joined: 12-June 08

Re: Moving an image in a form

Posted 06 November 2012 - 10:48 AM

Glad to help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1