# 在C#中连续创建圆

Creating Circles in a row in C#
2021-09-14
•  译文(汉语)
•  原文(英语)

``````// Create bounding rectangle.
Rectangle rect = new Rectangle(0, 0, 200, 200);

// Draw rectangle to screen.
e.Graphics.DrawRectangle(blackPen, rect);
``````

``````// Create bounding rectangle.
Rectangle rect = new Rectangle(0, 0, 200, 200);

// Draw circle to screen.
e.Graphics.DrawEllipse(blackPen, rect);
``````

I am very new to programming and have a quick question regarding a practical exercise i am undergoing for my studies. I am trying to create an application where the user can enter the number of rows and the nuber of columns in which circles will be drawn next to each other (to make a grid of circles essentially)

I have been able to do this in C# by using a rectangle object and shifting it's X axis value over by an amount but i cannot find out how to do this with a circle. Does this work in the same way? I have seen on MSDN that there is a circle class but i can't get this to work. Do i need to do some thing with a fill ellipse? As this is the only way i can currently figure out how to draw a circle.

I am not looking for a complete solve, just a few basic pointers to help a newbie leearn the ropes.

Thanks!!

Solutions1

Assuming that you have desktop winforms application, and using System.Drawing.Graphics object to draw inside window.

To draw a rectangle, you probably use one of DrawRectangle methods:

``````// Create bounding rectangle.
Rectangle rect = new Rectangle(0, 0, 200, 200);

// Draw rectangle to screen.
e.Graphics.DrawRectangle(blackPen, rect);
``````

To draw a circle instead of rectangle, just use DrawEllipse method, and move bounding rectangle the same way you did, by shifting x and y coordinates.

``````// Create bounding rectangle.
Rectangle rect = new Rectangle(0, 0, 200, 200);

// Draw circle to screen.
e.Graphics.DrawEllipse(blackPen, rect);
``````