views:

180

answers:

3

Hello.

I have a label label1 in the middle left of a large custom panel panel2 that is scrolled in a parent panel panel1.

alt text

I would keep the label1 always in the visible left middle of the panel2, even when scrolling.

In the real example, my panel is a user control that generates the some labels in its left side. The panel scrolls, but I need to keep the labels always visible.

How could it be achieved?

this is my code:

public partial class Form1 : Form
{
    public Form1()
    {
        this.InitializeComponent();
    }

    protected Point clickPosition;
    protected Point scrollPosition;

    private void panel2_MouseDown(object sender, MouseEventArgs e)
    {
        this.clickPosition = e.Location;
    }

    private void panel2_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            this.SuspendLayout();
            this.scrollPosition += (Size)clickPosition - (Size)e.Location;
            this.panel1.AutoScrollPosition = scrollPosition;
            this.ResumeLayout(false);
        }
    }
}
A: 

Hai serhio,

Have a look at this it is related to ur question.. But i have no idea whether it would solve your prob anyhow give a try http://stackoverflow.com/questions/1919997/maintaining-the-size-and-position-of-the-control-in-the-form

and this one http://stackoverflow.com/questions/332788/maintain-scroll-position-of-treeview

Pandiya Chendur
Thanks, Pandiya. Unfortunately, any of these threads does not facilitate my problem. I can't use a TableLayoutPanel, the *label1* should be strongly inside the *panel2*. Nor the ThreeView case is not the mine.
serhio
+1  A: 

Well, it is technically possible, you just need to adjust the control position when the panel scrolls. For example:

  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      mPicturePos = pictureBox1.Location;
    }
    Point mPicturePos;
    private void panel1_Scroll(object sender, ScrollEventArgs e) {
      pictureBox1.Location = mPicturePos;
    }
  }

However, you'll see that the control will start doing the pogo when you scroll the panel. The problem here is that Windows is being too helpful. It scrolls the content of the window itself with a fast bitblt, then sends a paint request for only the parts of the window that needs to be repainted.

It does this directed by a system option named "Show window contents while dragging", available in Appearance + Effects dialog of the Display applet in Control Panel. You cannot reasonably turn this option off, it has system-wide effects. On Win7 it isn't even exposed anymore.

There is no good workaround for this, other than a simple one: don't put the control in the panel. Just make sure it is located on top of the panel. That can be a bit tricky in the designer, put it next to the panel (Bring To Front if necessary) and edit the Location property by hand.

Hans Passant
I think instead of pictureBox1 in my example I should use panel2... but. I tried your code - any change. I need to keep the label not the panel2(PictureBox in your code)
serhio
Erm, my advice was to *not* put the control in the panel.
Hans Passant
In the real example, my panel is a user control that *generates* (creates and Controls.Add) some labels in its left side. The panel scrolls, but I need to keep the labels always visible. I will complete the question with this information.
serhio
Simple: don't add the labels to the panel, add them to the UserControl.
Hans Passant
@nobugs: :) my panel **is** the user control that generate labels...maybe I need to 'retrieve' the panel(UsCtrl)'s *parent* but I never can be sure what the parent is and what is located in the place where I would like to place my labels..
serhio
Simple: add a panel to the user control.
Hans Passant
is no so simple. I draw a custom background in my UserControl so adding a new panel first of all will cover my drawing, and secondly, I will need to add the labels in the child panel, not in the userControl itself - this will change my logic, third, even if I will add one more panel to the existing panel(UsrCtrl) I will remain with the same problem: the panel should be "immovable" to parent panel scrolling.
serhio
Draw the background in the panel, not the UC. Put everything that should scroll in the panel. Everything that should stay put in the UC. The panel should be scrollable and docked. The UC should not be scrollable.
Hans Passant
A: 
    public Form1()
    {
        this.InitializeComponent();
        panel2.Paint += new PaintEventHandler(panel2_Paint);
    }

    void panel2_Paint(object sender, PaintEventArgs e)
    {
        label1.Location = 
            new Point(-panel1.AutoScrollPosition.X, label1.Location.Y);
    }
serhio