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:
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.