I have a tsak which is let the user draw any number of rectangles in a panel and if he want to move one of them use the mouse How can I do that please the problem faces me:

1. How let him draw many of them
2. How can he select certain one and move it

Please help me please

Recommended Answers

All 4 Replies

What have you done so far? Can we see some code so we know how to help.

Without anything I'd say learn how to handle mouse event. Having a click handler on a form would allow you to create rectangles.

this is the code I wrote it about allow the user to draw lines and polygons not rectange but in this code all figures are continue the code is:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace GDI1
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.RadioButton lineOption;
        private System.Windows.Forms.RadioButton polygonOption;
        private System.Windows.Forms.RadioButton filledPolygonOption;
        private System.Windows.Forms.Button clearButton;
        private System.Windows.Forms.Button colorButton;
        private System.Windows.Forms.Panel drawPanel;
        private ArrayList points=new ArrayList();
        Pen pen=new Pen(Color.DarkBlue);
        SolidBrush brush=new SolidBrush(Color.DarkBlue);


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }


        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }
        private void drawPanel_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
//if(e.Button==MouseButtons.Right)
            //{
                points.Add(new Point(e.X,e.Y));
                drawPanel.Invalidate();
            //}

        }

        private void clearButton_Click(object sender, System.EventArgs e)
        {
            points =new ArrayList();
            drawPanel.Invalidate();

        }

        private void lineOption_CheckedChanged(object sender, System.EventArgs e)
        {
            drawPanel.Invalidate();

        }

        private void polygonOption_CheckedChanged(object sender, System.EventArgs e)
        {
        drawPanel.Invalidate();
        }

        private void filledPolygonOption_CheckedChanged(object sender, System.EventArgs e)
        {
            drawPanel.Invalidate();
        }

        private void colorButton_Click(object sender, System.EventArgs e)
        {
        ColorDialog dialogcolor=new ColorDialog();
        DialogResult result= dialogcolor.ShowDialog();
     if(result== DialogResult.Cancel)
         return;
            pen.Color=dialogcolor.Color;
            brush.Color=dialogcolor.Color;
            drawPanel.Invalidate();


        }

        private void drawPanel_Paint_1(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Graphics graphicsobject= e.Graphics;

            if(points.Count>1)
            {
                Point[] pointarray= (Point[])points.ToArray(points[0].GetType());
                if(polygonOption.Checked)
                    graphicsobject.DrawPolygon(pen,pointarray);
                else if(lineOption.Checked)
                    graphicsobject.DrawLines(pen,pointarray);
                else if(filledPolygonOption.Checked)
                    graphicsobject.FillPolygon(brush,pointarray);


            }

        }
    }
}

I guess that u can solve the problem by create a user control and display it as a rectangle in your project u emplement it's events to achive the goal

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.