I would like to create a table like control where a number of top rows stay in place when scrolling. I paint the control in an override of the OnPaint method.
When scrolling some partial repaint happens automatically, but this creates artefacts and looks weird. To solve that I call Invalidate(); or Refresh(); in an eventhandler attached to the scroll event and take the scrollposition in to account when painting the part that is supposed to scroll in / out of view. However when I do this the part that is supposed to stay in place gets scrolled out and sent back again and again. (so it is sort of flickering) I am trying to make this work smoothly without the flickering.
To simplify the issue I have made the following code where I draw a line that is supposed to stay in place when scrolling. As you can see it starts flickering when scrolling.
I have already thought of doublebuffering and such as you can see in the code.
Does anyone know how to make the line stay in place? Thanks in advance.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace ColumnReaderUITryOut
{
public partial class CustomPaintScrollTest : UserControl
{
public CustomPaintScrollTest()
{
InitializeComponent();
AutoScrollMinSize = new Size(500, 600);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawLine(Pens.Black, 0, 0, Width, Height);
}
private void CustomPaintScrollTest_Scroll(object sender, ScrollEventArgs e)
{
Invalidate();
}
}
}