I drew a table by my self which shows a header of the table and the body contents (all the other lines which are no header). The table is drawing on a Form or a Panel.
If the table is too long and I'm scrolling down, the header is drawn again on the top of the control and in that way I can always see the header. it works good except the problem is that while scrolling I see the header moving down if I scroll up, or disappearing if I scroll down. (it happened for milliseconds, but it still bothers me)
here is the coding for the painting event:
void TheControl_Paint(object sender, PaintEventArgs e)
{
DrawTable(e.Graphics);
}
public void DrawTable(Graphics dc)
{
try
{
FontMeasureHeight = dc.MeasureString("ABCD", FontForTable).Height;
Rectangle? ClientArea = null;
if (isTableConactedToControl)
ClientArea = TheControl.ClientRectangle;
if (ClientArea != null)
{
lock (Rows)
{
foreach (TableRow Row in Rows)
{
if (Row.isRowToShow || Row.IsHeaderRow)
{
if (ClientArea != dc.ClipBounds)
if (Row.Cells.Count > 0 && !GetColumnToHide(0))
if (Row.Cells[0].CellPoint.Y + Row.Cells[0].CellSize.Height + TheControl.DisplayRectangle.Location.Y >= dc.ClipBounds.Top)
{
if (Row.Cells[0].CellPoint.Y + TheControl.DisplayRectangle.Location.Y > dc.ClipBounds.Bottom)
break;
}
else
continue;
foreach (TableCell Cell in Row.Cells)
{
Cell.DrawCell(dc, TheControl.DisplayRectangle.Location, ClientArea);
}
}
}
}
if (isFreezingHeader)
{
lock (Rows)
{
foreach (TableRow Row in Rows)
{
if (!Row.IsHeaderRow)
break;
foreach (TableCell Cell in Row.Cells)
{
Cell.DrawCell(dc, TheControl.DisplayRectangle.Location, ClientArea, true);
}
}
}
}
if (TheControl is Form)
ScrolledPosition = (TheControl as Form).AutoScrollPosition;
else if (TheControl is Panel)
ScrolledPosition = (TheControl as Panel).AutoScrollPosition;
CurrentWindowHeight = TheControl.Size.Height;
}
}
catch { }
}
I captured an example of the problem that I want to fix (as I said, it appears for few milliseconds, and than fixes itself) the first picture is the problematic one, and the second is the fixed one:
My problem would be solved if I could scroll just the body part of the table.
How can I do that?
Why the duplicate explanation doesn't work for me?
One of the options was to use LockWindowUpdate like in the following code:
void TheControl_Scroll(object sender, ScrollEventArgs e)
{
LockWindowUpdate(TheControl.Handle);
TheControl.Invalidate();
LockWindowUpdate(IntPtr.Zero);
TheControl.Update();
}
It suppose to be perfect unless the scroll event was being called after the scrolling part actually done, so the freezing part happens after scrolling. If someone can tell me how can I catch the scroll event before it happens, it will solve me problem. So I'll freeze the control before the scrolling and release it after it happens.