none
Rectangle Arrays C# RRS feed

  • Question

  • if I make array of rectangels, why can't I change the location of exact element of it and how to do this? 

    for example I want to use it like this:

        ArrayName[i].Location=e.Location; (doesn't work)

    or

        ArrayName[i].Offset(e.Location); (also doesn't work)

    instead of:

        NameOfRectangle.Location = e.Location; (works)

    (e.Location = Mouse's Location)

    It'd be great for me to solve this question


    • Edited by naruto228 Friday, November 15, 2019 6:32 PM
    Friday, November 15, 2019 6:29 PM

Answers

  • Greetings Naruto228.

    The problem is that every time you do a pictureBox_Paint, you are creating new rectangles at the original locations. So you move a rectangle, and then when the picturebox gets repainted, you get a new rectangle back where you started. You need to move the creating of the rectangles outside the Paint event.

            
            public Form1()
            {
               InitializeComponent();
    
               // Initialise the rectangles here.
                for (int o=1,a=25,b=205,c=80,d=10; o < 9; o++)
                {
                    colA[o - 1] = new Rectangle(a + 2 * o, b - 15 * o, c - 7 * o, d);
    
                }
            }
    
    
    
    
            public void pictureBox1_Paint(object sender, PaintEventArgs e, ref Rectangle []colA)
            {
                //creating 3 columns
                e.Graphics.FillRectangle(new SolidBrush(Color.Blue), col1);
                e.Graphics.FillRectangle(new SolidBrush(Color.Blue), col2);
                e.Graphics.FillRectangle(new SolidBrush(Color.Blue), col3);
                //Don't create rectangles here.
                // Just paint them. 
                for (int o=1; o<=trackBar1.Value; o++)
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.Green), colA[o - 1]);
                }
            }


    • Edited by Ante Meridian Monday, November 18, 2019 10:45 PM loop errors
    • Marked as answer by naruto228 Tuesday, November 19, 2019 5:44 AM
    Monday, November 18, 2019 10:44 PM

All replies

  • if I make array of rectangels, why can't I change the location of exact element of it and how to do this? 

    for example I want to use it like this:

        ArrayName[i].Location=e.Location; (doesn't work)

    or

        ArrayName[i].Offset(e.Location); (also doesn't work)

    instead of:

        NameOfRectangle.Location = e.Location; (works)

    (e.Location = Mouse's Location)


    You may have to give us more information. For example, what *exactly* does
    "doesn't work" mean in this case? Compile error? Run time error? Exception
    thrown?

    Giving a little more code, such that we can copy it and try to reproduce your 
    error would also help. Show how you are creating the array.

    Where is this code located in your actual program? I assume it is in an
    event handler. If so, which one?

    I assume that e.Location is a Point.

    There usually should be no problem changing the Location of a Rectangle in an
    array. For example this code should build and run without issues:

    Rectangle rectangle = new Rectangle(10, 10, 50, 50);
    Point point = new Point(20, 20);
    
    rectangle.Location = point;
    
    Rectangle[] rectangles = new Rectangle[3];
    //rectangles[0] = new Rectangle(10, 10, 50, 50);
    
    rectangles[0].Location = point;
    

    - Wayne

    Friday, November 15, 2019 10:14 PM
  • Maybe the problem is caused by the fact that Rectangle is struct, not class. Show the details.

    Saturday, November 16, 2019 3:25 PM
  • Hi naruto228,
    I defined an array of rectangels. Then use the property “ArrayName[i].Location” to change the location and it works fine.
    Here is my code you can refer to.

    Rectangle[] rectangles = new Rectangle[3];
    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
                rectangles[0] = new Rectangle(10, 10, 10, 10);
                rectangles[1] = new Rectangle(20, 10, 10, 10);
                rectangles[2] = new Rectangle(30, 10, 10, 10);
                rectangles[0].Location = e.Location;
                rectangles[0].Offset(e.Location);
    }

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 18, 2019 9:06 AM
  • public partial class Form1 : Form
        {
            Rectangle col1 = new Rectangle(50, 50, 5, 200);
            Rectangle col2 = new Rectangle(175, 50, 5, 200);
            Rectangle col3 = new Rectangle(300, 50, 5, 200);
    
            Rectangle[] colA = new Rectangle[9];
            Rectangle[] colB = new Rectangle[9];
            Rectangle[] colC = new Rectangle[9];
    
            public Form1()
            {
                InitializeComponent();
            }
            
    
            private void button1_Click(object sender, EventArgs e)
            {
                //auto
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                //play
            }
    
            public void pictureBox1_MouseMove(object sender, MouseEventArgs e, ref Rectangle []colA)
            {
                colA[0].Location = e.Location;
                pictureBox1.Refresh();
            }
    
            public void pictureBox1_Paint(object sender, PaintEventArgs e, ref Rectangle []colA)
            {
                //creating 3 columns
                e.Graphics.FillRectangle(new SolidBrush(Color.Blue), col1);
                e.Graphics.FillRectangle(new SolidBrush(Color.Blue), col2);
                e.Graphics.FillRectangle(new SolidBrush(Color.Blue), col3);
                //creating disks 
                for (int o=1,a=25,b=205,c=80,d=10; o<=trackBar1.Value; o++)
                {
                    colA[o - 1] = new Rectangle(a + 2 * o, b - 15 * o, c - 7 * o, d);
                    e.Graphics.FillRectangle(new SolidBrush(Color.Green), colA[o - 1]);
                }
            }
    
            private void trackBar1_Scroll(object sender, EventArgs e)
            {
    
            }
        }
    I understood, that problem is that I can't use array's elements out of method where it's created. Doesn't work means that location of rectangle doesn't change (no system errors etc). How should I write this code then to get the correct work? 
    Monday, November 18, 2019 4:57 PM
  • Greetings Naruto228.

    The problem is that every time you do a pictureBox_Paint, you are creating new rectangles at the original locations. So you move a rectangle, and then when the picturebox gets repainted, you get a new rectangle back where you started. You need to move the creating of the rectangles outside the Paint event.

            
            public Form1()
            {
               InitializeComponent();
    
               // Initialise the rectangles here.
                for (int o=1,a=25,b=205,c=80,d=10; o < 9; o++)
                {
                    colA[o - 1] = new Rectangle(a + 2 * o, b - 15 * o, c - 7 * o, d);
    
                }
            }
    
    
    
    
            public void pictureBox1_Paint(object sender, PaintEventArgs e, ref Rectangle []colA)
            {
                //creating 3 columns
                e.Graphics.FillRectangle(new SolidBrush(Color.Blue), col1);
                e.Graphics.FillRectangle(new SolidBrush(Color.Blue), col2);
                e.Graphics.FillRectangle(new SolidBrush(Color.Blue), col3);
                //Don't create rectangles here.
                // Just paint them. 
                for (int o=1; o<=trackBar1.Value; o++)
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.Green), colA[o - 1]);
                }
            }


    • Edited by Ante Meridian Monday, November 18, 2019 10:45 PM loop errors
    • Marked as answer by naruto228 Tuesday, November 19, 2019 5:44 AM
    Monday, November 18, 2019 10:44 PM