Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Fill circle with color

Posted on 2009-12-30
5
Medium Priority
?
1,135 Views
Last Modified: 2013-12-17
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
Comment
Question by:nandithaa
5 Comments
 
LVL 5

Expert Comment

by:chinawal
ID: 26151685
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
 
LVL 13

Expert Comment

by:Ashok
ID: 26151777
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
 
LVL 13

Expert Comment

by:Ashok
ID: 26151787
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 26152016
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
 
LVL 6

Author Closing Comment

by:nandithaa
ID: 31671494
Thank you so much....
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question