Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 阻止窗体移出我的主sdi窗体的一侧_C#_Winforms - Fatal编程技术网

C# 阻止窗体移出我的主sdi窗体的一侧

C# 阻止窗体移出我的主sdi窗体的一侧,c#,winforms,C#,Winforms,假设我有两个sdi表单,当应用程序运行时,会显示一个sdi表单,从那里我会显示另一个sdi表单,但问题是我可以在任何地方drga我不想要的第二个sdi表单。在MDI窗体的情况下,MDI子窗体不能从MDI窗体边界中拖出。因此,在我的情况下,我希望模拟相同的情况。我不希望其他sdi表单不能从我的主sdi表单的边界中拖出。所以请指导我怎么做 我猜我必须处理表单拖动事件,从那里我必须检查表单顶部和左侧,但需要更多建议 private void Form1_Move(object sender, Even

假设我有两个sdi表单,当应用程序运行时,会显示一个sdi表单,从那里我会显示另一个sdi表单,但问题是我可以在任何地方drga我不想要的第二个sdi表单。在MDI窗体的情况下,MDI子窗体不能从MDI窗体边界中拖出。因此,在我的情况下,我希望模拟相同的情况。我不希望其他sdi表单不能从我的主sdi表单的边界中拖出。所以请指导我怎么做

我猜我必须处理表单拖动事件,从那里我必须检查表单顶部和左侧,但需要更多建议

private void Form1_Move(object sender, EventArgs e)
{
    this.Location = defaultLocation;
}
我试着这样做,但不起作用

public partial class Form2 : Form
    {
        Form1 _parent = null;
        public Form2()
        {
            InitializeComponent();
        }

        public Form2(Form1 parent)
        {
            InitializeComponent();
            _parent = parent;
        }

        private void Form2_Move(object sender, EventArgs e)
        {
            if((this.Location.X+this.Width) > _parent.Width)
            {
                this.Location = new System.Drawing.Point(_parent.ClientRectangle.Width-this.Width,this.Location.Y);
            }

            if ((this.Location.Y + this.Height) > _parent.Height)
            {
                this.Location = new System.Drawing.Point(_parent.ClientRectangle.Height - this.Height, this.Location.X);
            }

            if (this.Location.Y < 0)
            {
                this.Location = new System.Drawing.Point(this.Location.X);
            }

            if (this.Location.X < 0)
            {
                this.Location = new System.Drawing.Point(this.Location.Y);
            }
        }
    }
公共部分类表单2:表单
{
Form1\u parent=null;
公共表格2()
{
初始化组件();
}
公共表格2(表格1家长)
{
初始化组件();
_父母=父母;
}
私有void Form2\u移动(对象发送方,事件参数e)
{
如果((this.Location.X+this.Width)>\u parent.Width)
{
this.Location=new System.Drawing.Point(_parent.ClientRectangle.Width this.Width,this.Location.Y);
}
如果((this.Location.Y+this.Height)>\u parent.Height)
{
this.Location=新系统.Drawing.Point(_parent.ClientRectangle.Height-this.Height,this.Location.X);
}
if(该位置Y<0)
{
this.Location=新系统.Drawing.Point(this.Location.X);
}
如果(此.Location.X<0)
{
this.Location=新系统.Drawing.Point(this.Location.Y);
}
}
}
请告诉我哪里出错了。 谢谢

更新
private void Form2\u Move(对象发送方,事件参数e)
{
int left=this.left;
inttop=这个.top;
如果(此左<_父左)
{
左=_parent.left;
}
如果(this.Right>\u parent.Right)
{
left=\u parent.Right-此.Width;
}
如果(this.Top<\u parent.Top)
{
top=\u parent.top;
}
如果(this.Bottom>\u parent.Bottom)
{
top=\u parent.Bottom-此.Height;
}
此位置=新点(左,顶部);
}

我建议您遵循@ProgrammerV5建议。但是,如果您确实需要控制表单移动,请参阅属性的使用

以下是一些信息:


此外,您可能还需要执行一项任务。

对不起,您正试图以艰难的方式完成任务。如果这是一个练习,我理解,但如果不是,你试图重新创建MDI模型,这是不实际的。只需使用MDI父窗口并打开子窗口(您可以将MDI框架设置为几乎相同(UI方面)与任何其他窗口一样。希望它能有所帮助。这似乎是个坏主意。您需要MDI行为,但不使用MDI。这将很困难,并且会创建难闻的代码。我的问题解决了,我用正确的代码更新了我的答案。我指的是光标。相反,我希望我的sdi表单之一不能移出我的主sdi表单。
private void Form2_Move(object sender, EventArgs e)
        {
            int left = this.Left;
            int top = this.Top;

            if (this.Left < _parent.Left)
            {
                left = _parent.Left;
            }
            if (this.Right > _parent.Right)
            {
                left = _parent.Right - this.Width;
            }
            if (this.Top < _parent.Top)
            {
                top = _parent.Top;
            }
            if (this.Bottom > _parent.Bottom)
            {
                top = _parent.Bottom - this.Height;
            }

            this.Location = new Point(left, top);
        }