user969068
user969068

Reputation: 2943

Replacing old values when drawing

I am using Windows Form application and using one button to generate random number and draw on form. when button is clicked, It is adding a random number using Graphics.Drawing method. Problem is when I hit the button first time it works fine and add a random number i.e 11111. When I hit button again it will add a new random number (on next position) but it will also change previous numbers to new generated random number.

Updated: (Added Complete Code)

Edit: I have moved Random outside of scoop so now it does not generate same number but still its changing old random numbers to other ones.

Main Class:

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

namespace DrawingText
{
    public partial class Form1 : Form
    {

        private Point mouseDownPosition = new Point(0, 0);
        private Point mouseMovePosition = new Point(0, 0);
        private int mousePressdDown;
        private ArrayList drawnItemsList;
        Random rnd;

        public Form1()
        {
            InitializeComponent();
            drawnItemsList = new ArrayList();
            this.rnd = new Random();

        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            mouseMovePosition = e.Location;
            if (e.Button == MouseButtons.Left)
                mousePressdDown = 1;

        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                mouseDownPosition = e.Location;
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (mousePressdDown == 1)
            {
                label1.Text = "X: " + mouseMovePosition.X.ToString();
                label2.Text = "Y: " + mouseMovePosition.Y.ToString();
                this.Invalidate();
            }
            DrawingData a = new DrawingData(mouseMovePosition, mouseDownPosition);
            drawnItemsList.Add(a);
            mousePressdDown = 0;

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            foreach (DrawingData a in drawnItemsList)
            {
                draw(e.Graphics, a.old, a.cur);

            }
         draw(e.Graphics, mouseDownPosition, mouseMovePosition);


        }
        private void draw(Graphics e, Point mold, Point mcur)
        {
            Pen p = new Pen(Color.Black, 2);

                    using (Font useFont = new Font("Gotham Medium", 28, FontStyle.Bold))
                    {
                            string header2 = rnd.Next().ToString();
                            RectangleF header2Rect = new RectangleF();
                            int moldX = mold.X - 5;
                            int moldY = mold.Y;

                            header2Rect.Location = new Point(moldX, moldY);
                            header2Rect.Size = new Size(600, ((int)e.MeasureString(header2, useFont, 600, StringFormat.GenericTypographic).Height));
                            e.DrawString(header2, useFont, Brushes.Black, header2Rect);
                        }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}

Drawing Data Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace DrawingText
{
    [Serializable]
    class DrawingData
    {
        private Point mold; // mouseDown position
        private Point mcur; // mouseUp poslition

        public DrawingData()
        {
            mold = new Point(0, 0);
            mcur = new Point(0, 0);
        }
        public DrawingData(Point old, Point cur)
        {
            mold = old;
            mcur = cur;
        }

        public Point old
        {
            get
            {
                return mold;
            }
            set
            {
                mold = value;
            }
        }

        public Point cur
        {
            get
            {
                return mcur;
            }
            set
            {
                mcur = value;
            }
        }


    }
}

3 times button clicked and it replaced old value with new one: enter image description here

Upvotes: 1

Views: 120

Answers (3)

Karl Anderson
Karl Anderson

Reputation: 34846

You need to store the random value with the point values in the DrawingData class, like this:

Main Class:

namespace DrawingText
{
    public partial class Form1 : Form
    {
        private Point mouseDownPosition = new Point(0, 0);
        private Point mouseMovePosition = new Point(0, 0);
        private int mousePressdDown;
        private ArrayList drawnItemsList;
        Random rnd;

        public Form1()
        {
            InitializeComponent();
            drawnItemsList = new ArrayList();
            this.rnd = new Random();
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (mousePressdDown == 1)
            {
                label1.Text = "X: " + mouseMovePosition.X.ToString();
                label2.Text = "Y: " + mouseMovePosition.Y.ToString();
                this.Invalidate();
            }
            DrawingData a = new DrawingData(mouseMovePosition, mouseDownPosition, rnd.Next().ToString());
            drawnItemsList.Add(a);
            mousePressdDown = 0;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            foreach (DrawingData a in drawnItemsList)
            {
                draw(e.Graphics, a);
            }
            draw(e.Graphics, mouseDownPosition, mouseMovePosition);
        }

        private void draw(Graphics e, DrawingData a)
        {
            Pen p = new Pen(Color.Black, 2);

            using (Font useFont = new Font("Gotham Medium", 28, FontStyle.Bold))
            {
                RectangleF header2Rect = new RectangleF();
                int moldX = a.old.X - 5;
                int moldY = a.old.Y;

                header2Rect.Location = new Point(moldX, moldY);
                header2Rect.Size = new Size(600, ((int)e.MeasureString(header2, useFont, 600, StringFormat.GenericTypographic).Height));
                e.DrawString(a.Rand, useFont, Brushes.Black, header2Rect);
            }
        }
    }
}

Drawing Data Class:

namespace DrawingText
{
    [Serializable]
    public class DrawingData
    {
        private Point mold; // mouseDown position
        private Point mcur; // mouseUp poslition
        private string randValue; // random data value

        public DrawingData()
        {
            mold = new Point(0, 0);
            mcur = new Point(0, 0);
            randValue = String.Empty;
        }

        public DrawingData(Point old, Point cur, string rand)
        {
            mold = old;
            mcur = cur;
            randValue = rand;
        }

        public Point old
        {
            get
            {
                return mold;
            }
            set
            {
                mold = value;
            }
        }

        public Point cur
        {
            get
            {
                return mcur;
            }
            set
            {
                mcur = value;
            }
        }

        public sting Rand
        {
            get
            {
                return randValue;
            }
            set
            {
                randValue = value;
            }
     }
}

Upvotes: 1

terrybozzio
terrybozzio

Reputation: 4542

This is made on the fly using graphics path:

    GraphicsPath gp;
    int moldX = 10;
    int moldY = 10;

    public Form1()
    {
        InitializeComponent();
        gp = new GraphicsPath();  
    }


    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        e.Graphics.FillPath(Brushes.Black, gp);
        // if you want the numbers outlined do e.Graphics.DrawPath
    }

    private void button1_Click(object sender, EventArgs e)
    {
        AddToPath();
        Invalidate();   
    }

    private void AddToPath()
    {
        using (Font useFont = new Font("Gotham Medium", 28, FontStyle.Bold))
        {
            Random rnd = new Random();
            string header2 = rnd.Next().ToString();
            int strsize = TextRenderer.MeasureText(header2, useFont).Height;
            StringFormat format = StringFormat.GenericDefault;
            gp.AddString(header2, useFont.FontFamily, 1, 28, new Point(moldX, moldY), format);

            moldX += 5;
            moldY += strsize;
        }
    }

Upvotes: 0

Alan
Alan

Reputation: 7951

You are recreating your random each time in the loop which will cause it to have the same seed, and the same first number. That's why all your numbers are the same. You should.

  1. Move your random outside of the method and loop, and use it instead. Change the line Random rnd = new Random() to rnd = new Random(). You already have a variable in the class to hold the random.

  2. If you want the previous random numbers to remain the same as the last time, you need to store them in a list somewhere and draw them on paint. You are currently creating a new set of random numbers each time.

Upvotes: 0

Related Questions