Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8178237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:42:50+00:00 2026-06-06T23:42:50+00:00

I am developing a very rudimentary drawing program: A 2D grid comprised of multiple

  • 0

I am developing a very rudimentary drawing program: A 2D grid comprised of multiple RectangleShapes, around 20×30 pixels each, which when clicked change color based on user RGB input, which works just fine:

Color SelectedColor = new Color();
private void Pixel_1_1_Click(object sender, EventArgs e)    // on Rectangle click
{
    Pixel_1_1.FillColor = SelectedColor;    // change to currently desired color.
}

Since the number of squares is rising dramatically, I’m looking for a way to arrange the “pixel” rectangles into a 2D array. (I really don’t want to have to make a Pixel_Click method for every single Rectangle on the screen!) Hoping eventually to be able to call something like:

private void Pixel_[x]_[y]_Click(object sender, EventArgs e)
{
    Pixel_[x]_[y].FillColor = SelectedColor;
}

My friends suggest the use of an anonymous delegate, but I don’t understand how to fully use one to solve my problem.

What would be the best way to generate a 2D array of rectangles in a C# Windows Form? And once generated, how can I access them with a single method for variant values of x and y?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:42:51+00:00Added an answer on June 6, 2026 at 11:42 pm

    While you are probably correct in thinking of each rectangle as an object, it probably isn’t correct to think of each rectangle as a windows control, especially since you have so many of them.

    So try creating your own rectangle object:

    public class MyRect {
      public Color FillColor { get; set; }
      public Rectangle Rectangle { get; set; }
    
      public MyRect(Rectangle r, Color c) {
        this.Rectangle = r;
        this.FillColor = c;
      }
    }
    

    Now you just need to keep a list of your objects and paint on a single Panel control (or PictureBox) all of your rectangles:

    private List<MyRect> myRectangles = new List<MyRect>();
    
    public Form1() {
      InitializeComponent();
    
      myRectangles.Add(new MyRect(new Rectangle(10, 10, 64, 16), Color.Blue));
      myRectangles.Add(new MyRect(new Rectangle(20, 48, 16, 64), Color.Red));
    }
    
    private void panel1_Paint(object sender, PaintEventArgs e) {
      foreach (MyRect mr in myRectangles) {
        using (SolidBrush sb = new SolidBrush(mr.FillColor)) {
          e.Graphics.FillRectangle(sb, mr.Rectangle);
        }
      }
    }
    

    To handle the “click” event of the rectangles, you just handle the MouseDown or MouseClick event of your container control and determine yourself which rectangle is being clicked on:

    void panel1_MouseDown(object sender, MouseEventArgs e) {
      if (e.Button == MouseButtons.Left) {
        foreach (MyRect mr in myRectangles) {
          if (mr.Rectangle.Contains(e.Location)) {
            ChangeColor(mr, Color.Green);
          }
        }
        panel1.Invalidate();
      }
    }
    
    private void ChangeColor(MyRect mr, Color newColor) {
      mr.FillColor = newColor;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im Developing a web application which is very critical. So authentication has to be
I am very new to android and I am developing this app which allows
I´m developing a very memory-consuming app and want to use the largeHeap-Tag, which should
Im at the moment developing a very heavy website which i need to optimize
I'm developing a very simple program in VB2010 Express that will create folders based
I'm developing a site which is heavily loaded with ajax. Not very practical in
I'm developing an application with very intense image processing where I have multiple ListFragments
I am currently developing a very simple website in Orchard, which however required me
I've been developing a very large LOB app using my flavor of M-V-VM which
I'm developing this app which as a very basic Ajax form and I'm currently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.