views:

527

answers:

4
+1  Q: 

Repaint of panel

My problem is that I have a panel in panel. Inside i have the AutoScroll property set to true. When I open a new window this panel is scrolled to begining.

I do that, I save the position before open new window, and I set it after close it. It works but it jumps to the beginning and then back to my position.

A: 

Have you tried setting autoscroll to false?

pianoman
+2  A: 

The AutoScrollPosition property is a bit funny. When you read it, it will return the current scroll offset, but when you assign it you will need to invert the values:

private static Point GetAutoScrollPosition(Panel panel)
{
    return panel.AutoScrollPosition;
}

private static void SetAutoScrollPosition(Panel panel, Point position)
{
    panel.AutoScrollPosition = new Point(-position.X, -position.Y);
}

Now you can retrieve the current position and set it like so:

Point pos = GetAutoScrollPosition(myPanel);
SetAutoScrollPosition(myPanel, pos);
Fredrik Mörk
A: 

I do something lik You wrote

_scrollPozition = -(pnlMain.AutoScrollPosition.Y); DialogResult result = MessageBox.Show("Delete: ", MessageBoxButtons.YesNo); dgvClendar.Focus();

private void pnlMain_Paint(object sender, PaintEventArgs e) {

        if (pnlMain.AutoScrollPosition.Y == 0)
        {
            pnlMain.AutoScrollPosition = new Point(0, _scrollPozition);
            _scrollPozition = 0;
        }
    }

on paint it is set, but if you look everything is moved for a moment. I need to block this scroll to begin, or block painting, and repaint after scroll to current position.

You would not want to do this in the Paint event; it is raised over and over again as soon as the some part of the panel needs to be repainted. Assign the position during Form_Load or some other event that occurs before the form is visible.
Fredrik Mörk
A: 

I change to that:

dgvClendar.SuspendLayout();

_scrollPozition = -(pnlMain.AutoScrollPosition.Y);

 DialogResult result = MessageBox.Show("Delete: ", "Deleting", MessageBoxButtons.YesNo);

pnlMain.AutoScrollPosition = new Point(0, _scrollPozition);

dgvClendar.ResumeLayout();

It set good position, bot there is two repaint, on position 0 and on seted position. have you any idea how to suspend this paint? That what i done dont give any effect.