• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1140
  • Last Modified:

Fill circle with color

I've drawn many circles in a form. When the user click any circle that particular circle has to be colored. How can i do this?

Please any one help me.....

Thanks in advance....
0
nandithaa
Asked:
nandithaa
1 Solution
 
chinawalCommented:
What type of Controls these Circles are? Don't these Circle controls have OnClick events?
On OnClick event, you should be able to change the color.
0
 
AshokCommented:
If you use "e.Graphics.DrawEllipse(Pens.Red, 150, 50, 100, 100);", I do not think it is easy to fill color.  It may be possible.

Also, DrawEllipse does not return an Object so there is no way to refer to particular circle.

HTH
Ashok
0
 
AshokCommented:
Just a note.

In Web based application (Silverlight) it is easy to do this kind of stuff.
See how it can be done at http://blog.esponjasoft.com/  (MouseEnter, MouseLeave and MouseClick, etc.).

Ashok
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
For WinForms you typically develop what is known as a "persistent graphics" system.

Essentially you store the data about all of your "objects" in a class and then store instances of that class in a list.

This allows you to paint all of them when necessary, perform "hit testing" and change each item individually.

Here is a simple example:
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;
using System.Drawing.Drawing2D;

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

        private List<Circle> circles = new List<Circle>();

        private void Form1_Load(object sender, EventArgs e)
        {
            circles.Add(new Circle(Color.Black, Color.Yellow, new Rectangle(10,10,25,25)));
            circles.Add(new Circle(Color.Blue, Color.LightGreen, new Rectangle(100,75,125,200)));
            circles.Add(new Circle(Color.Red, Color.LightBlue, new Rectangle(10, 300, 75, 25)));
            circles.Add(new Circle(Color.Green, Color.Orange, new Rectangle(500, 325, 250, 125)));
            this.Size = new Size(800, 600);
            this.Paint += new PaintEventHandler(Form1_Paint);
            this.MouseDown += new MouseEventHandler(Form1_MouseDown);
        }

        void Form1_Paint(object sender, PaintEventArgs e)
        {
            foreach (Circle c in this.circles)
            {
                c.Paint(e.Graphics);
            }
        }

        void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            Point pt = new Point(e.X, e.Y);
            foreach (Circle c in this.circles)
            {
                if (c.Hit(pt))
                {
                    c.ClickedOn = !c.ClickedOn;
                    this.Refresh();
                    break;
                }
            }
        }

    }

    public class Circle
    {

        public Color LineColor = Color.Black;
        public Color FillColor = Color.Yellow;
        public GraphicsPath gp = new GraphicsPath();
        public bool ClickedOn = false;

        public Circle(Color Line, Color Fill, Rectangle circle)
        {
            this.LineColor = Line;
            this.FillColor = Fill;
            this.gp.AddEllipse(circle);
        }

        public void Paint(Graphics e)
        {
            if (ClickedOn)
            {
                using (SolidBrush sb = new SolidBrush(this.FillColor))
                {
                    e.FillPath(sb, this.gp);
                }
            }
            using (Pen p = new Pen(this.LineColor))
            {
                e.DrawPath(p, this.gp);
            }
        }

        public bool Hit(Point pt)
        {
            return this.gp.IsVisible(pt);
        }

    }

}

Open in new window

CircleHitTest.JPG
0
 
nandithaaAuthor Commented:
Thank you so much....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now