Control position inside Windows.Forms.Panel with autoscroll
Control.Location doesn't take into account the slider position of the parent panel control and returns the value like if there was no scrollbar. So how to obtain the real position of some control inside scrollable panel? For example here I never get the real location of my button:
Button button;
public Form1()
{
InitializeComponent();
开发者_运维知识库 panel1.Height = 200;
panel1.AutoScrollMinSize = new Size(0, 2000);
button = new Button();
panel1.Controls.Add(button);
button.Top = 1500;
button.Click += new EventHandler(button_Click);
}
void button_Click(object sender, EventArgs e)
{
MessageBox.Show(button.Location.Y.ToString());
}
It does. If I put a panel inside a scrolling panel, the location position is changing as I scroll:
Private Sub Panel1_Scroll(ByVal sender As Object, ByVal e As ScrollEventArgs) Handles Panel1.Scroll
Me.Text = Panel2.Location.ToString
End Sub
Make sure you don't have your control inside another panel that is getting scrolled, then the location property doesn't change.
Otherwise, look at the PointToScreen and PointToClient functions or adjust your values according to the AutoScrollPosition value of the scrolling parent.
精彩评论