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

  • SEARCH
  • Home
  • 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 8907161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:50:28+00:00 2026-06-15T02:50:28+00:00

What would be the best way to draw 25 rectangles (5*5) in c#? I

  • 0

What would be the best way to draw 25 rectangles (5*5) in c#?
I later need to be able to reach a specific rectangle and change its color, for instance change the color to red if the user inputs the incorrect word.
Would it be more suitable to create an array of rectangles in this case?

This is what i have so far

Graphics g = pictureBox1.CreateGraphics();

int x =0;
int y= 0;
int width = 20;
int height = 20;
for (int i = 0; i < 25; i++)
{
    if (i <= 4)
    {
        g.FillRectangle(Brushes.Blue, x, y, width, height);
        x += 50;
    }
    else if (i > 4)
    {
        y = 50;
        g.FillRectangle(Brushes.Blue, x, y, width, height);
        x += 50;
    }
}
  • 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-15T02:50:29+00:00Added an answer on June 15, 2026 at 2:50 am

    This should get you started, not the complete code. You will need to add a PictureBox control and use the default name (picurebox1). EDIT: Need to add a button too 🙂

    public partial class Form1 : Form
    {
        public List<Rectangle> listRec = new List<Rectangle>();
        Graphics g;
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            Rectangle rect = new Rectangle();
            rect.Size = new Size(100,20);
            for (int x = 0; x < 5; x++)
            {
                rect.X = x * rect.Width;
                for (int y = 0; y < 5; y++)
                {
                    rect.Y = y * rect.Height;
                    listRec.Add(rect);
                }
            }
    
            foreach (Rectangle rec in listRec)
            {
                g = pictureBox1.CreateGraphics();
                Pen p = new Pen(Color.Blue);
                g.DrawRectangle(p, rec);
            }
        }
    
        public void ChangeColor(Rectangle target, Color targetColor)
        {
            Pen p = new Pen(targetColor);
            g.DrawRectangle(p, target.X, target.Y, target.Width, target.Height);
        }
    
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.D0: ChangeColor(listRec[0], Color.Red);
                    break;
                case Keys.D1: ChangeColor(listRec[1], Color.Red);
                    break;
                //..more code to handle all keys..
            }
        }    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would be the best way to draw a circle of tick marks in
I would like to know what the best way would be to draw a
postgres 9.2 supports json columns. what would be best way to extend postgres to
I want to make a code snippet database web application. Would the best way
What would be the best way to something like this where a timestamp is
What would be the best way to display a dialog whenever my app receives
What would be the best way to version a rails application? We want to
What would be the best way to properly concatenate string with an url inside?
What would be the best way and more idiomatic to break a string into
What would be the best way to convert a 50-digit String to a BigInteger

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.