Click here to Skip to main content
15,908,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I need to draw a rectangle on the window form application(say about 5 rectangles) different sizes. I need to sort the rectangle from smallest to biggest when a button is pressed. Please help.
Posted
Updated 9-Dec-19 11:10am
Comments
lukeer 17-Oct-13 5:44am    
Where exactly is your problem?
Is it the drawing?
Is it the sorting?
Is it how to start altogether?
5Sazi 17-Oct-13 6:37am    
Hi Lukeer,

Please could you help me how to start that is where I am confuse!!!please
5Sazi 17-Oct-13 17:49pm    
Thank you so much, my rectangle is displayed on panel. I need to sort them and display five rectangles on panel. Could you also write steps to achieve this goal.
lukeer 18-Oct-13 2:25am    
Be careful what to reply to. Your last question was a reply to your own comment. Therefore you have been informed of it, not me.

Nevertheless you've been lucky and I checked by. I updated my answer.
5Sazi 18-Oct-13 4:43am    
Thank you so much for your kindness. I will try this solution tonight

Drawing a rectangle is easy.
Handle the Paint event (it's probably a good idea to put a Panel on the form, then handle the Paint event for that - that way you will draw the rectangles within the panel, rather than trying to draw over any other controls you may have, which is a lot harder to do).
Then in the handler:
C#
private void myDrawingPanel_Paint(object sender, PaintEventArgs e)
    {
    Graphics g = e.Graphics;
    g.DrawRectangle(Pens.Red, new Rectangle(10, 10, 100, 75));
    }
Where (10, 10) is the location of the top left corner of the rectangle, and the with is 100, the height 75.

If you need 5 rectangles, then call DrawRectangle 5 times, with different locations and sizes.
 
Share this answer
 
Starting the project involves this:

1) Fire up Visual Studio.
2) Create a new Project.
My VS shows a start page that offers a shortcut to create a project.
Or look in the menu where you would most likely expect that functionality.
3) A dialogue appears. In that choose to create a Windows Forms Application.
4) VS creates some files for you and opens the designer view for "Form1".
5) Drag a panel out of the toolbox onto Form1. It becomes "Panel1".
6) Edit Panel1's properties:
Switch from properties tab to events tab.
Double-click on Panel1's "Paint" event.
7) VS creates an event handler for you and switches to its code view.
8) Apply OriginalGriff's solution.

[Edit]
Changing the rectangles' locations as a result of sorting is possible, but needs another layer of indirection: You have to change the rectangle data from being hard-coded into Paint() to a data structure that you can change according to circumstances and change Paint() itself so it can work with that data structure.

Create a List<Size> on class level that holds your data.
Change Paint() to use the list.
C#
List<System.Drawing.Size> _rectangleData = new List<System.Drawing.Size>;

private void Panel1_Paint(object sender, PaintEventArgs e)
{
    for(int i = 0; i < _rectangleData.Count; i++)
    {
        System.Drawing.Point location = new System.Drawing.Point(10, 20 + 50 * i);
        System.Drwaing.Size size = _rectangleData[i];
        e.Graphics.DrawRectangle(Pens.Red, new System.Drawing.Rectangle(location, size));
    }
}


For now, this will draw nothing because there is no data in _rectangleData. Since I have no idea where you'll get it from, here's an example of filling the list right on creation:
C#
List<System.Drawing.Size> _rectangleData = new List<System.Drawing.Size>
{
    new System.Drawing.Size(10, 20),
    new System.Drawing.Size(30, 40)
}


Now play around with the location algorithm until it fits your needs.

Sorting the list can be done via the List.Sort(Comparison<T>)[^] method. For that you need to implement your custom comparison. An example is given on MSDN's page on Comparison<T>[^]. Keep in mind that T means Size for now.
[/Edit]
 
Share this answer
 
v5
using System;
using System.Drawing;
using System.Windows.Forms;

namespace testkhat
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
      
        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        public void Gosheha()
        {
            int a1 = 0;
            int b1 = 0;
            Graphics G4g = this.CreateGraphics();

            Color myRgbColor70 = new Color();
            Color myRgbColor80 = new Color();
            Color myRgbColor90 = new Color();

            myRgbColor70 = Color.FromArgb(63, 72, 204);
            myRgbColor80 = Color.FromArgb(0, 162, 232);
            myRgbColor90 = Color.FromArgb(153, 217, 234);

            SolidBrush Brosh70 = new SolidBrush(myRgbColor70);
            SolidBrush Brosh80 = new SolidBrush(myRgbColor80);
            SolidBrush Brosh90 = new SolidBrush(myRgbColor90);
            /////////////////////////////////////////////////////
            G4g.FillRectangle(Brosh70, (a1 + 0), (b1 + 0), 1, 1);
            G4g.FillRectangle(Brosh70, (a1 + 1), (b1 + 0), 1, 1);
            G4g.FillRectangle(Brosh70, (a1 + 2), (b1 + 0), 1, 1);
            G4g.FillRectangle(Brosh70, (a1 + 3), (b1 + 0), 1, 1);
            G4g.FillRectangle(Brosh70, (a1 + 4), (b1 + 0), 1, 1);
            G4g.FillRectangle(Brosh70, (a1 + 5), (b1 + 0), 1, 1);
            G4g.FillRectangle(Brosh70, (a1 + 0), (b1 + 1), 1, 1);
            G4g.FillRectangle(Brosh80, (a1 + 1), (b1 + 1), 1, 1);
            G4g.FillRectangle(Brosh80, (a1 + 2), (b1 + 1), 1, 1);
            G4g.FillRectangle(Brosh80, (a1 + 3), (b1 + 1), 1, 1);
            G4g.FillRectangle(Brosh80, (a1 + 4), (b1 + 1), 1, 1);
            G4g.FillRectangle(Brosh80, (a1 + 5), (b1 + 1), 1, 1);
            G4g.FillRectangle(Brosh70, (a1 + 0), (b1 + 2), 1, 1);
            G4g.FillRectangle(Brosh80, (a1 + 1), (b1 + 2), 1, 1);
            G4g.FillRectangle(Brosh90, (a1 + 2), (b1 + 2), 1, 1);
            G4g.FillRectangle(Brosh90, (a1 + 3), (b1 + 2), 1, 1);
            G4g.FillRectangle(Brosh90, (a1 + 4), (b1 + 2), 1, 1);
            G4g.FillRectangle(Brosh90, (a1 + 5), (b1 + 2), 1, 1);
            G4g.FillRectangle(Brosh70, (a1 + 0), (b1 + 3), 1, 1);
            G4g.FillRectangle(Brosh80, (a1 + 1), (b1 + 3), 1, 1);
            G4g.FillRectangle(Brosh90, (a1 + 2), (b1 + 3), 1, 1);
            G4g.FillRectangle(Brosh90, (a1 + 3), (b1 + 3), 1, 1);
            G4g.FillRectangle(Brosh90, (a1 + 4), (b1 + 3), 1, 1);
            G4g.FillRectangle(Brosh90, (a1 + 5), (b1 + 3), 1, 1);
            G4g.FillRectangle(Brosh70, (a1 + 0), (b1 + 4), 1, 1);
            G4g.FillRectangle(Brosh80, (a1 + 1), (b1 + 4), 1, 1);
            G4g.FillRectangle(Brosh90, (a1 + 2), (b1 + 4), 1, 1);
            G4g.FillRectangle(Brosh90, (a1 + 3), (b1 + 4), 1, 1);
            G4g.FillRectangle(Brosh80, (a1 + 4), (b1 + 4), 1, 1);
            G4g.FillRectangle(Brosh80, (a1 + 5), (b1 + 4), 1, 1);
            G4g.FillRectangle(Brosh70, (a1 + 0), (b1 + 5), 1, 1);
            G4g.FillRectangle(Brosh80, (a1 + 1), (b1 + 5), 1, 1);
            G4g.FillRectangle(Brosh90, (a1 + 2), (b1 + 5), 1, 1);
            G4g.FillRectangle(Brosh90, (a1 + 3), (b1 + 5), 1, 1);
            G4g.FillRectangle(Brosh80, (a1 + 4), (b1 + 5), 1, 1);
            G4g.FillRectangle(Brosh70, (a1 + 5), (b1 + 5), 1, 1);
            /////////////////////////////////////////////////////////
            int a2 = this.Width - 6;
            int b2 = 0;

            G4g.FillRectangle(Brosh70, (a2 + 0), (b2 + 0), 1, 1);
            G4g.FillRectangle(Brosh70, (a2 + 1), (b2 + 0), 1, 1);
            G4g.FillRectangle(Brosh70, (a2 + 2), (b2 + 0), 1, 1);
            G4g.FillRectangle(Brosh70, (a2 + 3), (b2 + 0), 1, 1);
            G4g.FillRectangle(Brosh70, (a2 + 4), (b2 + 0), 1, 1);
            G4g.FillRectangle(Brosh70, (a2 + 5), (b2 + 0), 1, 1);
            G4g.FillRectangle(Brosh80, (a2 + 0), (b2 + 1), 1, 1);
            G4g.FillRectangle(Brosh80, (a2 + 1), (b2 + 1), 1, 1);
            G4g.FillRectangle(Brosh80, (a2 + 2), (b2 + 1), 1, 1);
            G4g.FillRectangle(Brosh80, (a2 + 3), (b2 + 1), 1, 1);
            G4g.FillRectangle(Brosh80, (a2 + 4), (b2 + 1), 1, 1);
            G4g.FillRectangle(Brosh70, (a2 + 5), (b2 + 1), 1, 1);
            G4g.FillRectangle(Brosh90, (a2 + 0), (b2 + 2), 1, 1);
            G4g.FillRectangle(Brosh90, (a2 + 1), (b2 + 2), 1, 1);
            G4g.FillRectangle(Brosh90, (a2 + 2), (b2 + 2), 1, 1);
            G4g.FillRectangle(Brosh90, (a2 + 3), (b2 + 2), 1, 1);
            G4g.FillRectangle(Brosh80, (a2 + 4), (b2 + 2), 1, 1);
            G4g.FillRectangle(Brosh70, (a2 + 5), (b2 + 2), 1, 1);
            G4g.FillRectangle(Brosh90, (a2 + 0), (b2 + 3), 1, 1);
            G4g.FillRectangle(Brosh90, (a2 + 1), (b2 + 3), 1, 1);
            G4g.FillRectangle(Brosh90, (a2 + 2), (b2 + 3), 1, 1);
            G4g.FillRectangle(Brosh90, (a2 + 3), (b2 + 3), 1, 1);
            G4g.FillRectangle(Brosh80, (a2 + 4), (b2 + 3), 1, 1);
            G4g.FillRectangle(Brosh70, (a2 + 5), (b2 + 3), 1, 1);
            G4g.FillRectangle(Brosh80, (a2 + 0), (b2 + 4), 1, 1);
            G4g.FillRectangle(Brosh80, (a2 + 1), (b2 + 4), 1, 1);
            G4g.FillRectangle(Brosh90, (a2 + 2), (b2 + 4), 1, 1);
            G4g.FillRectangle(Brosh90, (a2 + 3), (b2 + 4), 1, 1);
            G4g.FillRectangle(Brosh80, (a2 + 4), (b2 + 4), 1, 1);
            G4g.FillRectangle(Brosh70, (a2 + 5), (b2 + 4), 1, 1);
            G4g.FillRectangle(Brosh70, (a2 + 0), (b2 + 5), 1, 1);
            G4g.FillRectangle(Brosh80, (a2 + 1), (b2 + 5), 1, 1);
            G4g.FillRectangle(Brosh90, (a2 + 2), (b2 + 5), 1, 1);
            G4g.FillRectangle(Brosh90, (a2 + 3), (b2 + 5), 1, 1);
            G4g.FillRectangle(Brosh80, (a2 + 4), (b2 + 5), 1, 1);
            G4g.FillRectangle(Brosh70, (a2 + 5), (b2 + 5), 1, 1);
            /////////////////////////////////////////////////////////
            int a3 = 0;
            int b3 = this.Height - 6;

            G4g.FillRectangle(Brosh70, (a3 + 0), (b3 + 0), 1, 1);
            G4g.FillRectangle(Brosh70, (a3 + 5), (b3 + 0), 1, 1);
            G4g.FillRectangle(Brosh70, (a3 + 0), (b3 + 1), 1, 1);
            G4g.FillRectangle(Brosh70, (a3 + 0), (b3 + 2), 1, 1);
            G4g.FillRectangle(Brosh70, (a3 + 0), (b3 + 3), 1, 1);
            G4g.FillRectangle(Brosh70, (a3 + 0), (b3 + 4), 1, 1);
            G4g.FillRectangle(Brosh70, (a3 + 0), (b3 + 5), 1, 1);
            G4g.FillRectangle(Brosh70, (a3 + 1), (b3 + 5), 1, 1);
            G4g.FillRectangle(Brosh70, (a3 + 2), (b3 + 5), 1, 1);
            G4g.FillRectangle(Brosh70, (a3 + 3), (b3 + 5), 1, 1);
            G4g.FillRectangle(Brosh70, (a3 + 4), (b3 + 5), 1, 1);
            G4g.FillRectangle(Brosh70, (a3 + 5), (b3 + 5), 1, 1);

            G4g.FillRectangle(Brosh80, (a3 + 1), (b3 + 4), 1, 1);
            G4g.FillRectangle(Brosh80, (a3 + 2), (b3 + 4), 1, 1);
            G4g.FillRectangle(Brosh80, (a3 + 3), (b3 + 4), 1, 1);
            G4g.FillRectangle(Brosh80, (a3 + 4), (b3 + 4), 1, 1);
            G4g.FillRectangle(Brosh80, (a3 + 5), (b3 + 4), 1, 1);
            G4g.FillRectangle(Brosh80, (a3 + 1), (b3 + 3), 1, 1);
            G4g.FillRectangle(Brosh80, (a3 + 1), (b3 + 2), 1, 1);
            G4g.FillRectangle(Brosh80, (a3 + 1), (b3 + 1), 1, 1);
            G4g.FillRectangle(Brosh80, (a3 + 1), (b3 + 0), 1, 1);
            G4g.FillRectangle(Brosh80, (a3 + 4), (b3 + 0), 1, 1);
            G4g.FillRectangle(Brosh80, (a3 + 4), (b3 + 1), 1, 1);
            G4g.FillRectangle(Brosh80, (a3 + 5), (b3 + 1), 1, 1);

            G4g.FillRectangle(Brosh90, (a3 + 2), (b3 + 0), 1, 1);
            G4g.FillRectangle(Brosh90, (a3 + 3), (b3 + 0), 1, 1);
            G4g.FillRectangle(Brosh90, (a3 + 2), (b3 + 1), 1, 1);
            G4g.FillRectangle(Brosh90, (a3 + 3), (b3 + 1), 1, 1);
            G4g.FillRectangle(Brosh90, (a3 + 2), (b3 + 2), 1, 1);
            G4g.FillRectangle(Brosh90, (a3 + 3), (b3 + 2), 1, 1);
            G4g.FillRectangle(Brosh90, (a3 + 4), (b3 + 2), 1, 1);
            G4g.FillRectangle(Brosh90, (a3 + 5), (b3 + 2), 1, 1);
            G4g.FillRectangle(Brosh90, (a3 + 2), (b3 + 3), 1, 1);
            G4g.FillRectangle(Brosh90, (a3 + 3), (b3 + 3), 1, 1);
            G4g.FillRectangle(Brosh90, (a3 + 4), (b3 + 3), 1, 1);
            G4g.FillRectangle(Brosh90, (a3 + 5), (b3 + 3), 1, 1);
            //////////////////////////////////////////////////////
            int a4 = this.Width - 6;
            int b4 = this.Height - 6;

            G4g.FillRectangle(Brosh70, (a4 + 0), (b4 + 0), 1, 1);
            G4g.FillRectangle(Brosh70, (a4 + 5), (b4 + 4), 1, 1);
            G4g.FillRectangle(Brosh70, (a4 + 0), (b4 + 5), 1, 1);
            G4g.FillRectangle(Brosh70, (a4 + 1), (b4 + 5), 1, 1);
            G4g.FillRectangle(Brosh70, (a4 + 2), (b4 + 5), 1, 1);
            G4g.FillRectangle(Brosh70, (a4 + 3), (b4 + 5), 1, 1);
            G4g.FillRectangle(Brosh70, (a4 + 4), (b4 + 5), 1, 1);
            G4g.FillRectangle(Brosh70, (a4 + 5), (b4 + 5), 1, 1);
            G4g.FillRectangle(Brosh70, (a4 + 5), (b4 + 0), 1, 1);
            G4g.FillRectangle(Brosh70, (a4 + 5), (b4 + 1), 1, 1);
            G4g.FillRectangle(Brosh70, (a4 + 5), (b4 + 2), 1, 1);
            G4g.FillRectangle(Brosh70, (a4 + 5), (b4 + 3), 1, 1);

            G4g.FillRectangle(Brosh80, (a4 + 1), (b4 + 0), 1, 1);
            G4g.FillRectangle(Brosh80, (a4 + 4), (b4 + 0), 1, 1);
            G4g.FillRectangle(Brosh80, (a4 + 0), (b4 + 1), 1, 1);
            G4g.FillRectangle(Brosh80, (a4 + 1), (b4 + 1), 1, 1);
            G4g.FillRectangle(Brosh80, (a4 + 4), (b4 + 1), 1, 1);
            G4g.FillRectangle(Brosh80, (a4 + 4), (b4 + 2), 1, 1);
            G4g.FillRectangle(Brosh80, (a4 + 4), (b4 + 3), 1, 1);
            G4g.FillRectangle(Brosh80, (a4 + 0), (b4 + 4), 1, 1);
            G4g.FillRectangle(Brosh80, (a4 + 1), (b4 + 4), 1, 1);
            G4g.FillRectangle(Brosh80, (a4 + 2), (b4 + 4), 1, 1);
            G4g.FillRectangle(Brosh80, (a4 + 3), (b4 + 4), 1, 1);
            G4g.FillRectangle(Brosh80, (a4 + 4), (b4 + 4), 1, 1);

            G4g.FillRectangle(Brosh90, (a4 + 2), (b4 + 0), 1, 1);
            G4g.FillRectangle(Brosh90, (a4 + 3), (b4 + 0), 1, 1);
            G4g.FillRectangle(Brosh90, (a4 + 2), (b4 + 1), 1, 1);
            G4g.FillRectangle(Brosh90, (a4 + 3), (b4 + 1), 1, 1);
            G4g.FillRectangle(Brosh90, (a4 + 0), (b4 + 2), 1, 1);
            G4g.FillRectangle(Brosh90, (a4 + 1), (b4 + 2), 1, 1);
            G4g.FillRectangle(Brosh90, (a4 + 2), (b4 + 2), 1, 1);
            G4g.FillRectangle(Brosh90, (a4 + 3), (b4 + 2), 1, 1);
            G4g.FillRectangle(Brosh90, (a4 + 0), (b4 + 3), 1, 1);
            G4g.FillRectangle(Brosh90, (a4 + 1), (b4 + 3), 1, 1);
            G4g.FillRectangle(Brosh90, (a4 + 2), (b4 + 3), 1, 1);
            G4g.FillRectangle(Brosh90, (a4 + 3), (b4 + 3), 1, 1);
            //////////////////////////////////////////////////////
        }

//====================

        private void btn4G_Click(object sender, EventArgs e)
        {
            Gosheha();
        }

        private void btnkhotoot_Click(object sender, EventArgs e)
        {
            Graphics g1 = this.CreateGraphics();
            Color myRgbColor70 = new Color();
            Color myRgbColor80 = new Color();
            Color myRgbColor90 = new Color();
            myRgbColor70 = Color.FromArgb(63, 72, 204);
            myRgbColor80 = Color.FromArgb(0, 162, 232);
            myRgbColor90 = Color.FromArgb(153, 217, 234);
            SolidBrush Brosh70 = new SolidBrush(myRgbColor70);
            SolidBrush Brosh80 = new SolidBrush(myRgbColor80);
            SolidBrush Brosh90 = new SolidBrush(myRgbColor90);
            try
            {
                int i;
                for (i = 6; i < this.Width - 6; i++)
                {
                    int a = i;
                    int b = 0;
                   
                    g1.FillRectangle(Brosh70, (a + 0), (b + 0), 1, 1);
                    g1.FillRectangle(Brosh70, (a + 0), (this.Height - 1), 1, 1);
                    g1.FillRectangle(Brosh70, (a + 0), (b + 5), 1, 1);
                    g1.FillRectangle(Brosh70, (a + 0), (this.Height - 6), 1, 1);
                   
                    g1.FillRectangle(Brosh80, (a + 0), (b + 1), 1, 1);
                    g1.FillRectangle(Brosh80, (a + 0), (this.Height - 2), 1, 1);
                    g1.FillRectangle(Brosh80, (a + 0), (b + 4), 1, 1);
                    g1.FillRectangle(Brosh80, (a + 0), (this.Height - 5), 1, 1);

                    g1.FillRectangle(Brosh90, (a + 0), (b + 2), 1, 1);
                    g1.FillRectangle(Brosh90, (a + 0), (this.Height - 3), 1, 1);
                    g1.FillRectangle(Brosh90, (a + 0), (b + 3), 1, 1);
                    g1.FillRectangle(Brosh90, (a + 0), (this.Height - 4), 1, 1);
                }
            }
            catch
            {

            }
            try
            {
                int i;
                for (i = 6; i < this.Height - 6; i++)
                {
                    int a = 0;
                    int b = i;

                    g1.FillRectangle(Brosh70, (a + 0), (b + 0), 1, 1);
                    g1.FillRectangle(Brosh70, (this.Width - 1), (b + 0), 1, 1);
                    g1.FillRectangle(Brosh70, (a + 5), (b + 0), 1, 1);
                    g1.FillRectangle(Brosh70, (this.Width - 6), (b + 0), 1, 1);

                    g1.FillRectangle(Brosh80, (a + 1), (b + 0), 1, 1);
                    g1.FillRectangle(Brosh80, (this.Width - 2), (b + 0), 1, 1);

                    g1.FillRectangle(Brosh80, (a + 4), (b + 0), 1, 1);
                    g1.FillRectangle(Brosh80, (this.Width - 5), (b + 0), 1, 1);

                    g1.FillRectangle(Brosh90, (a + 2), (b + 0), 1, 1);
                    g1.FillRectangle(Brosh90, (this.Width - 3), (b + 0), 1, 1);
                    g1.FillRectangle(Brosh90, (a + 3), (b + 0), 1, 1);
                    g1.FillRectangle(Brosh90, (this.Width - 4), (b + 0), 1, 1);
                }
            }
            catch
            {

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Gosheha();
        }
    }
}
 
Share this answer
 
Comments
Dave Kreskowiak 9-Dec-19 17:30pm    
An unexplained code dump is not an answer to anything.

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