简体   繁体   中英

How to determine if control is outside of Form C#

I am trying to determine if a dynamically added control is outside of the form.

At first, I thought it might be possible to calculate it with getting the height of the form, and location of the dynamically added control.

But I noticed that the Control.Location and Form.Height have "nothing" in common.

I don't think I really understand what the correlation is between Height and Location.

For example:
I thought that if your form has a height of 500, and I put the control at the bottom of the form, it should give the Location: X, 500 (X is width, not relevant here). But this is not correct, it shows me for example: X, 465. Am I missing something?

So I need to be able to recognize if the control is outside of the form, even if it's just one pixel.

I've found several similiar questions here on SO, yet this does not really give me the answer that I need unfortunately.

So, is there any way to do this? Is it possible to calculate it?

The Height of the form also includes the height of the title bar and borders. You can use the ClientSize of the form:

From the documentation on MSDN :

The size of the client area of the form is the size of the form excluding the borders and the title bar. The client area of a form is the area within a form where controls can be placed. You can use this property to get the proper dimensions when performing graphics operations or when sizing and positioning controls on the form. To get the size of the entire form, use the Size property or use the individual properties Height and Width.

The position of the control is relative to its container, so (0,0) is the left upper corner inside the form.

带控件的插图

I know this is an older thread, but you can try using this method:

public static bool IsOutofBounds(Form form, Control control)
    {
        int controlEnd_X = control.Location.X + control.ClientSize.Width;
        int controlEnd_Y = control.Location.Y + control.ClientSize.Height;
        if (form.ClientSize.Width < controlEnd_X || form.ClientSize.Height < controlEnd_Y)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

It works for checking whether a control is out of bounds of its parent form.

您可以使用此代码来检查控件是否在表单内:

var Inside = frm.ClientRectange.Intersect(ctrl.Bounds) == ctrl.Bounds;

the top left corner of a form is (0,0) lower right corner is (formHeight, fromWidth). to check this place two text boxes on a form and write this code in the mouse move event to see how x and y change.

  private void Form1_MouseMove(object sender, MouseEventArgs e)
  {
      textBox1.Text = e.X.ToString();
      textBox2.Text = e.Y.ToString();
  }

Note that there is an difference between the number returned from the edge of the form and the size chosen by you. In my 500*500 form it is actually 460*483. the difference is always the same for any border style and any resolution.

To place a control on your form use the location structure in the form or use the top and left properties for the control; top = x, left = y. Remember your offset from the actual height and width you measured and the dimension of the control. To add a button with the following dimensions 80*30 in the bottom right corner I would right something like this:

button1.Location = new System.Drawing.Point(402, 430);

bottom left corner:

button1.Location = new System.Drawing.Point(0, 430);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM