繁体   English   中英

如何创建在面板中绘制表格的控件

[英]How to create control that draws a table in panel

我想要创建一个在panel中绘制表格的控件。 我的代码是:

 public class PanelZ : System.Windows.Forms.Panel
{

    public static void Draw()
    {
        Panel p = new Panel();
        p.Width = 200;
        p.Height = 200;
        Graphics g = p.CreateGraphics();
        Pen mypen = new Pen(Brushes.Black, 1);
        Font myfont = new Font("tahoma", 10);
        int lines = 9;
        float x = 0;
        float y = 0;
        float xSpace = p.Width / lines;
        float yspace = p.Height / lines;
        for (int i = 0; i < lines + 1; i++)
        {
            g.DrawLine(mypen, x, y, x, p.Height);
            x += xSpace;
        }
        x = 0f;
        for (int i = 0; i < lines + 1; i++)
        {
            g.DrawLine(mypen, x, y, p.Width, y);
            y += yspace;
        }

    }

..但是没有画一张桌子; 所以我该怎么做?

这将起作用。 但是数字应该是属性,笔应该是属性,然后是一些。.另外:属性应该以大写字母开头。

 public class PanelZ : System.Windows.Forms.Panel
 {
    public PanelZ()                  // a constructor
    {
        Width = 200;
        Height = 200;
        DoubleBuffered = true;
        lines = 9;
    }

    public   int lines { get; set; }  // a property

    protected override void OnPaint(PaintEventArgs e)  // the paint event
    {
        base.OnPaint(e);

        Graphics g = e.Graphics;
        Pen mypen = new Pen(Brushes.Black, 1);
        Font myfont = new Font("tahoma", 10);
        float x = 0;
        float y = 0;
        float xSpace = Width / lines;
        float yspace = Height / lines;
        for (int i = 0; i < lines + 1; i++)
        {
            g.DrawLine(mypen, x, y, x, Height);
            x += xSpace;
        }
        for (int i = 0; i < lines + 1; i++)
        {
            g.DrawLine(mypen, 0, y, Width, y);
            y += yspace;
        }
    }
}

在VS中工作:

在此处输入图片说明

请注意,这只会为像素着色。 那里没有有用的网格,只有带有颜色的像素。.因此,如果您实际上要使用定义的Font,则必须计算坐标和边界框。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM