tags:

views:

615

answers:

2

I need to draw a line grid on a windows form that contains 4 rows and 4 columns.

A: 

Have a look at: How to: Draw a Line on a Windows Form

If you want something for controlling layout, rather than a simple line grid, you can use a TableLayoutPanel.

In response to your comment, you can achieve what you want using the TableLayoutPanel and anchoring and docking. There is also the FlowLayoutPanel, but be careful not to overuse this control, as form load speed appears to suffer.

Mitch Wheat
Needs to be a line grid
burntsugar
@burntsugar: a line grid is just multiple lines, right? So all you need to do is draw horizontal and vertical lines at your required spacing.
Mitch Wheat
I was hoping to find something similar to the java layout manager but it does not appear to exist in the .NET libraries.
burntsugar
The closest thing is the TableLayoutPanel or FlowLayoutPabel (careful using too mmany of the last one)
Mitch Wheat
A: 

I would override the OnPaint method and add in some code to draw the lines in the background. Then, after I finish the drawing I want to do, I would call the base.OnPaint to let the form continue drawing any other controls that may be on the form. This way, the lines are in the background and will not be painted on top of any other controls. Also, be sure the add a graphics.clear() call to avoid the screen tearing effect.

Perhaps you can try a variant of this code:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Pen linePen = new Pen(System.Drawing.Color.CornflowerBlue);
            Graphics grphx = this.CreateGraphics();
            grphx.Clear(this.BackColor);

            for(int i=1; i<5; i++)
            {
             //Draw verticle line
             grphx.DrawLine(linePen, 
              (this.ClientSize.Width/4)*i,
              0,
                    (this.ClientSize.Width / 4) * i,
                    this.ClientSize.Height);

             //Draw horizontal line
             grphx.DrawLine(linePen, 
              0,
                    (this.ClientSize.Height / 4) * i,
                    this.ClientSize.Width,
                    (this.ClientSize.Height / 4) * i);
            }
            linePen.Dispose();

            //Continues the paint of other elements and controls
            base.OnPaint(e);
        }
    }
}
Mark Ewer