Macin Macin - 1 year ago 100
C# Question

How to draw a table using System.Drawing

I would like to draw a table using System.Drawings, and then fill cells with some text. This text will change every few seconds, in various moments of time. It's a game, where there is a grid and every few seconds, random cell displays a number for a split of a second, then the user has to type the answer in the text box below it. Also, user has to click the cell, which has just showed the number. I have little experience in working with Graphics in window so any help will be greatly appreciated.

Answer Source

Have you considered using the DataGridView Control instead?

If you prefer to use a more low-level approach, drawing a table is not that hard. Subdivide the x and y coordinates to obtain points for drawing (System.Drawing.Point)

Draw lines using a pen (System.Drawing.Pen) and two points as arguments to Graphics.DrawLine method. You can position your numbers at midpoint of cells because you already know all of the points (and their coordinates) used to draw table lines.

You can determine which cell was clicked also by coordinates.