2014-10-21

How to draw a grid in c#

Grids are often needed when we design or measure something. Here is example of a such grid.


Code for it:


using System;
using System.Drawing;
using System.Windows.Forms;
 
namespace WinFormDemo
{
    public partial class FormGrid : Form
    {
        public FormGrid()
        {
            InitializeComponent();
        }
 
 
        private void OnPanelPaint(object sender, PaintEventArgs e)
        {
            e.Graphics.PageUnit = GraphicsUnit.Millimeter;
            DrawHelpGrid(e.Graphics);
        }
 
 
        private void DrawHelpGrid(Graphics graphic, int width = 100int height = 100)
        {
            int max = Math.Max(width, height);
            var pen1 = new Pen(Brushes.LightGray, 0.1f);
            var pen2 = new Pen(Brushes.LightGreen, 0.1f);
            bool mainLine = true;
 
            for (int index = 0; index < max; index += 5)
            {
                Pen pen = mainLine ? pen2 : pen1;
 
                // Make main line little longer
                int ext = mainLine ? 2 : 0;
 
                if (index <= height)
                {
                    graphic.DrawLine(pen, 0, index, width + ext, index);
                    if (mainLine)
                    {
                        graphic.DrawString(index + "", 
                            SystemFonts.MenuFont, 
                            Brushes.Blue, 
                            width + 5, index - 2);
                    }
                }
 
                if (index <= width)
                {
                    graphic.DrawLine(pen, index, 0, index, height + ext);
                    if (mainLine)
                    {
                        graphic.DrawString(index + "", 
                            SystemFonts.MenuFont, 
                            Brushes.Blue, 
                            index - 3, height + 5);
                    }
                }
                mainLine = !mainLine;
            }
        }
    }
}
And Designer conde

namespace WinFormDemo
{
    partial class FormGrid
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.Color.White;
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(00);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(590428);
            this.panel1.TabIndex = 1;
            this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPanelPaint);
            // 
            // FormGrid
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(590428);
            this.Controls.Add(this.panel1);
            this.Name = "FormGrid";
            this.Text = "Grid";
            this.ResumeLayout(false);
 
        }
 
        #endregion
 
        private System.Windows.Forms.Panel panel1;
    }
}