Click here to Skip to main content
15,936,395 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This code allows you to select a rectangular and square areas.

But I do not want these areas to interfere.

It means you can not re-select the region.
pleas help me.

C#
using System;
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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Random r = new Random();
        int x1, y1, tag = 0;
        int bLeft, bTop;
        int sLeft, sTop;
        Button[] q = new Button[84];
        Color bc;
        Color col;
        private void Form1_Load(object sender, EventArgs e)
        {
            int scale = 28;
            pictureBox1.Width = scale * 35;
            pictureBox1.Height = scale * 24;
            Width = pictureBox1.Width + pictureBox1.Left + 17;
            Height = pictureBox1.Height + pictureBox1.Top + 40;
            int c = 0;
            for (int a = 0; a <= 22 * scale; a += 2 * scale)
            {
                for (int b = 0; b <= 30 * scale; b += 5 * scale)
                {
                    q[c] = new Button();
                    q[c].Left = b;
                 
                    q[c].Top = a;
                    q[c].Width = 5 * scale;
                    q[c].Height = 2 * scale;
                    q[c].MouseDown += new MouseEventHandler(down);
                    q[c].MouseMove += new MouseEventHandler(move);
                    q[c].MouseUp += new MouseEventHandler(up);
                    q[c].Text = (c + 1).ToString();
                    pictureBox1.Controls.Add(q[c]);
                    c++;
                }
            }
           
        }
        void down(Object s, MouseEventArgs e)
        {
            col = Color.FromArgb(r.Next(256), r.Next(256), r.Next(256));
            Button b = (Button)s;
            x1 = b.Left;
            y1 = b.Top;
        }

        void move(Object s, MouseEventArgs e)
        {


        }
        void up(object s, MouseEventArgs e)
        {
            Button b = (Button)s;
            int State = 0;
            if (e.X > 0 && e.Y > 0 )
            {
                State = 1;
            }
            else if (e.X < 0 && e.Y > 0)
            {
                State = 2;
            }
            else if (e.X > 0 && e.Y < 0)
            {
                State = 3;
            }
            else if (e.X < 0 && e.Y < 0)
            {
                State = 4;
            }

            if (e.X > 0)
            {
                bLeft = b.Left + e.X;
                sLeft = x1;
            }
            else
            
            {
                bLeft = x1;
                sLeft = b.Left + e.X;
            }
            if (e.Y > 0)
            {
                bTop = b.Top + e.Y;
                sTop = y1;
            }
            else
            {
                bTop = y1;
                sTop = b.Top + e.Y;
            }
            foreach (Button item in q)
            {
               

                    switch (State)
                    {
                        case 1:

                            if (item.Left >= sLeft && item.Left <= bLeft && item.Top >= sTop && item.Top <= bTop)
                            {
                                item.BackColor = col;
                            }

                            break;
                        case 2:

                            if (item.Left + item.Width >= sLeft && item.Left <= bLeft && item.Top >= sTop && item.Top <= bTop)
                            {
                                item.BackColor = col;

                            }
                            break;
                        case 3:

                            if (item.Left >= sLeft && item.Left <= bLeft && item.Top + item.Height >= sTop && item.Top <= bTop)
                            {
                                item.BackColor = col;
                            }

                            break;
                        case 4:


                            if (item.Left + item.Width >= sLeft && item.Left <= bLeft && item.Top + item.Height >= sTop && item.Top <= bTop)
                            {
                                item.BackColor = col;
                            }

                            break;
                           
                        default:
                            break;
                            
                   
                   
                }

            }

            Button d = s as Button;
            Text = (d.Left / 28 * 10).ToString() + " " + (d.Top / 28 * 10).ToString();
            int m = Convert.ToInt32((d.Left / 28 * 10));
            int n = Convert.ToInt32((d.Top / 28 * 10));

        }
    }
}


What I have tried:

This code allows you to select a rectangular and square areas.
But I do not want these areas to interfere.
It means you can not re-select the region.
Posted
Updated 22-Mar-18 22:01pm
v4
Comments
BillWoodruff 23-Mar-18 13:07pm    
Your code shows you creating 84 Buttons and adding them to the Controls Collection of a PictureBox. That is very strange, and I bet there is a much simpler method to accomplish your goal.

If you share, in detail, what the goal is, I'll respond further.

1 solution

Use the Region class[^] and it's Intersect method - if the resulting region is non-empty, they "interfere"
 
Share this answer
 
Comments
CPallini 23-Mar-18 4:05am    
5.
shahriar1996 23-Mar-18 18:54pm    
pleas edite my code. tnx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900