Dosen不在我的绘图表中显示标题文本

时间:2017-01-09 11:36:49

标签: c#

我创建了一个以窗体形式绘制表格的类。在我的班级中,首先绘制一个标题并在绘制文本后绘制单元格。我的代码是:

  public class TableH : System.Windows.Forms.Panel
{
    Color clr1, clr2;
    private Color color1 = Color.Blue;
    private Color color2 = Color.Aqua;
    private Color m_hovercolor1 = Color.Yellow;
    private Color m_hovercolor2 = Color.DarkOrange;
    private int color1Transparent = 150;
    private int color2Transparent = 150;
    private Color clickcolor1 = Color.DarkOrange;
    private Color clickcolor2 = Color.Red;
    private int angle = 90;
    private int textX = 100;
    private int textY = 25;
    private String text = "";

    public String DisplayText
    {
        get { return text; }
        set { text = value; Invalidate(); }
    }
    public Color StartColor
    {
        get { return color1; }
        set { color1 = value; Invalidate(); }
    }
    public Color EndColor
    {
        get { return color2; }
        set { color2 = value; Invalidate(); }
    }
    public Color MouseHoverColor1
    {
        get { return m_hovercolor1; }
        set { m_hovercolor1 = value; Invalidate(); }
    }
    public Color MouseHoverColor2
    {
        get { return m_hovercolor2; }
        set { m_hovercolor2 = value; Invalidate(); }
    }
    public Color MouseClickColor1
    {
        get { return clickcolor1; }
        set { clickcolor1 = value; Invalidate(); }
    }
    public Color MouseClickColor2
    {
        get { return clickcolor2; }
        set { clickcolor2 = value; Invalidate(); }
    }

    public int Transparent1
    {
        get { return color1Transparent; }
        set
        {
            color1Transparent = value;
            if (color1Transparent > 255)
            {
                color1Transparent = 255;
                Invalidate();
            }
            else
                Invalidate();
        }
    }

    public int Transparent2
    {
        get { return color2Transparent; }
        set
        {
            color2Transparent = value;
            if (color2Transparent > 255)
            {
                color2Transparent = 255;
                Invalidate();
            }
            else
                Invalidate();
        }
    }

    public int GradientAngle
    {
        get { return angle; }
        set { angle = value; Invalidate(); }
    }

    public int TextLocation_X
    {
        get { return textX; }
        set { textX = value; Invalidate(); }
    }
    public int TextLocation_Y
    {
        get { return textY; }
        set { textY = value; Invalidate(); }
    }
    public TableH()                  // a constructor
    {
        Width = 200;
        Height = 200;
        DoubleBuffered = true;
        lines = 9;
    }

    public int lines { get; set; }  // a property
    protected override void OnMouseEnter(EventArgs e)
    {
        base.OnMouseEnter(e);
        clr1 = color1;
        clr2 = color2;
        color1 = m_hovercolor1;
        color2 = m_hovercolor2;
    }
    //method mouse leave
    protected override void OnMouseLeave(EventArgs e)
    {
        base.OnMouseLeave(e);
        color1 = clr1;
        color2 = clr2;
    }
    //method mouse click
    protected override void OnMouseClick(MouseEventArgs e)
    {
        if (e.Clicks == 1)
        {
            base.OnMouseClick(e);
            color1 = clickcolor1;
            color2 = clickcolor2;
        }
    }
    int txtx, txty;
    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;

        //header color
        for (int i = 0; i < lines + 1; i++)
        {
            Color c1 = Color.FromArgb(color1Transparent, color1);
            Color c2 = Color.FromArgb(color2Transparent, color2);
            angle = 180;
            Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, linearGradientMode: LinearGradientMode.Vertical);
            e.Graphics.FillRectangle(b, x, y, xSpace + x, yspace);
            x += xSpace;
            //   linearGradientMode:LinearGradientMode.Horizontal
        }
        //create cells
        x = 0;
        y = 0;
        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;
        }
        //header text
        x = 0;
        y = 0;
        for (int i = 0; i < lines + 1; i++)
        {

            txtx += Convert.ToInt32((xSpace / 2) + x);
            txty = Convert.ToInt32(yspace / 2);
            Point p = new Point(txtx, txty);
            x += xSpace;
            switch (i)
            {
                case 1:
                    drawText("نام", g, p);
                    break;
                case 2:
                    drawText("نام خانوادگی", g, p);
                    break;
                case 3:
                    drawText("شماره شناسنامه", g, p);
                    break;
                case 4:
                    drawText("نام پدر", g, p);
                    break;
                case 5:
                    drawText("مدرک تحصیلی", g, p);
                    break;
                case 6:
                    drawText("آدرس منزل", g, p);
                    break;
                case 7:
                    drawText("شماره تماس منزل", g, p);
                    break;
                case 8:
                    drawText("شماره همراه", g, p);
                    break;
                case 9:
                    drawText("ایمیل", g, p);
                    break;
            }
        }

    }

    void drawText(string txt, Graphics g, Point p)
    {
        Font myfont = new Font("tahoma", 10);
        Brush myBrush = new SolidBrush(Color.Black);
        g.DrawString(txt, myfont, myBrush, p);
    }
}

我的表格是here。 但不会在标题中显示文字。我该如何解决?

1 个答案:

答案 0 :(得分:1)

发现错误:

在OnPaint()中,

txtx和txty vars未设置为0。每次调用OnPaint()后,它们变得越来越大。

做一个修复:

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

    txtx = 0; txty = 0;  // <- this is my fix
    Graphics g = e.Graphics;
    ...