Search code examples
c#.netgriddrawingsystem.drawing

Draw persistent grid in TabPage (.Net, C#)


I need to display a persistent grid in a TabPage. My problems would be instantly solved if I could draw to the entire non-visible portion of the TabPage and prevent graphics from being erased when scrolling.

The only other solution I can think of is tracking the scroll position in the tab and basing the grid drawn from that.

To get this to draw in the first place, I had to create an EventHandler for TabPage.Paint.

    //Code removed

This method draws vertical and horizontal lines to create a grid within the visible tab, but it continues to draw whenever a Paint event occurs (i.e. scrolling), so it creates overlapping lines and aren't aligned to anything but the size of the current visible area of the tab.


Solution

  • Keep in mind that this solution is just pseudo. You also have to respond to scrolling.

    void form_draw()
    {
            spacingX = offsetX % scale * -1;
            spacingY = offsetY % scale * -1;
    
            if (form.HorizontalPosition != lastXPosition && form.VerticalPosition == lastYPosition)
                lastStartX += spacingX;
            else if (tab.HorizontalScroll.Value == lastXPosition && form.VerticalPosition != lastYPosition)
                lastStartY += spacingY;
    
            lastYPosition = form.VerticalPosition;
            lastXPosition = form.HorizontalPosition;
    
            for (int i = lastStartY; i < formHeight; i += scale)
                form.draw(0, i, formWidth, i);
            for (int i = lastStartX; i < formWidth; i += scale)
                form.draw(i, 0, i, formWidth);
    }