1

C# .Net 4.5 Winform

On my Form, I have a SplitContainer, and in the right panel, my custom UserControl with a TableLayoutPanel.

As I draw UserControl items in each cell of the TableLayoutPanel, I want to use DrawString to display text on the division of the current cell and the cell above it.

Currently:

The cells' UserControls are docked as 'fill' in each cell of the TableLayoutPanel.

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        AutoScroll = false;
        AutoSize = false;
        Dock = DockStyle.Fill;
        Margin = new Padding(0);
    }

I offset the draw rectangle to be half it's height higher:

var topOffset = ClientRectangle.Top - (ClientRectangle.Height / 2);

paintRect = new Rectangle(ClientRectangle.X + Padding.Left,
                        topOffset + Padding.Top,
                        ClientRectangle.Width - Padding.Left - Padding.Right,
                        ClientRectangle.Height - Padding.Top - Padding.Bottom);

I create a new clipping region for graphics, and print out the text:

using (Graphics newGraphics = this.CreateGraphics())
{
    newGraphics.SetClip(paintRect);

    e.Graphics.SetClip(newGraphics);

    e.Graphics.DrawString(
        "ABCD 1234",
        Font,
        new SolidBrush(ForeColor),
        paintRect, style);


}

The text gets cut off - see image enter image description here

The cell UserControl that needs to display text (at it's top, halfway sticking into the control, and the TableLayoutPanel cell, above it. I think I need to change the clipping of those cells to not clip the text, but am unsuccessful.

How can I display the text 'outside' or 'on top' of the 'layer' that the TableLayoutPanel cell containing the UserControl is in, from within the UserControl OnPaint?

Robert Achmann
  • 1,986
  • 3
  • 40
  • 66

0 Answers0