0

I have 20 usercontrols in the flowlayoutpanel in a windows form.

Each of the user control has a button.

I want to find the position of each button on the flowlayoutpanel. How can I find the X and Y coordinates for the buttons?

I can access the buttons like this:

foreach (Control ctrl in this.pnlContainer.Controls.Find("btnPrint",true))
{
    Button c = ctrl as Button;
    if (c != null)
    {
        logger.Info("x: "+c.Location.X + ",y: "+c.Location.Y,c);
    }
}

However, the x and y coordinates are always the same.

thanks!

Prashant
  • 2,005
  • 3
  • 17
  • 24

1 Answers1

0

if the button is in the same location in all the UC then the x y will be the same for all the buttons in UC's , and the location you get is relative to the UC and not to the form .

i think you can find it here

C# Get a control's position on a form

try this code

foreach (Control ctrl in this.flowLayoutPanel1.Controls)
            {
                foreach (Control item in ctrl.Controls.Find("button1", true))
                {
                    Point pointOnForm = new Point(0, 0);
                    Control Btn = item;
                    for (; Btn.Parent != null && Btn.Parent.GetType() != typeof(Form); Btn = Btn.Parent)
                    {
                        pointOnForm.Offset(Btn.Location);
                    }

                    //label2.Text += pointOnForm + ",";
                }
            }
Community
  • 1
  • 1
Max
  • 156
  • 9
  • Yes, I am aware of it. However, is there a way to get it relative to form? – Prashant Aug 10 '14 at 06:46
  • I found that I was making a mistake. I had to create the form inside the project I required to access the controls for. Thanks anyway! – Prashant Dec 30 '14 at 00:45