【问题标题】:I want to position two forms right next to each other in C#我想在 C# 中将两个表单彼此相邻放置
【发布时间】:2011-11-04 17:34:44
【问题描述】:

我有一个表单可以打开另一个表单。

我想要做的是将新打开的表单放在已经可见的表单旁边(右侧)。

所以我需要将新表单定位到当前表单结束的位置(如果有错误请纠正我)。

所以我需要做类似的事情:

newform.Left = this.endposition.right;

endposition 属性是我刚刚编的(伪代码)。

如何获取当前表单右侧的结束位置?

编辑

我尝试了几种解决方案,但直到现在都没有奏效。

我总是得到相同的结果:

我尝试了以下代码:

newform.Left = this.Right + SystemInformation.BorderSize.Width;

newform.Left = this.Right + (SystemInformation.BorderSize.Width * 2);

newform.SetDesktopLocation(this.Location.X + this.Size.Width, this.Location.Y);

Eclyps19 建议手动添加一些像素来定位新表单,尽管我不确定边框是否在每个系统上都相同。

【问题讨论】:

    标签: c# winforms positioning


    【解决方案1】:

    我知道这个问题很老,但是对于任何来这里寻找答案的人,您需要考虑两个表单边框的大小。边框大小会因FormBorderStyle而异。

    var newForm = new Form2();
    newForm.Show();
    var form1Border = (this.Width - this.ClientSize.Width) / 2;
    var newFormBorder = (newForm.Width - newForm.ClientSize.Width) / 2;
    newForm.Left = this.Left + this.Width - form1Border - newFormBorder;
    

    【讨论】:

      【解决方案2】:

      您的代码运行良好,但您只需将其放在 newForm.Show() 方法之后。

      【讨论】:

        【解决方案3】:

        这对我有用:

                Form1 nForm = new Form1();
                nForm.Show();
                nForm.SetDesktopLocation(this.Location.X + this.Size.Width, this.Location.Y);
        

        【讨论】:

        • 没有用,我没有其他设置需要切换吗?
        • 我通过调用“ int _left = this.Parent.Location.X + this.Parent.Size.Width; int _top = this.Parent.Location.Y;”解决了我的问题
        【解决方案4】:

        试试

        newform.Left = oldform.Right + SystemInformation.BorderSize.Width;
        

        这应该将边框的宽度(以像素为单位)添加到 oldform.Right 值。您可以将 SystemInformation.BorderSize.Width 替换(或添加到)任何您喜欢的整数。

        【讨论】:

        • @Eclypse19:好的接近了:但是它没有考虑表单的边框。
        • 您可以随时为其添加附加值 - 尝试 newform.left = oldform.right + oldform.BorderSize
        • @Eclypse19: .Right = 同样的问题。并且没有.BorderSize 属性。
        • 糟糕 - 忽略我最后的评论。尝试将 SystemInformation.Bordersize 添加到 oldform.Right。这将拉取系统的边框大小值(以像素为单位)并将其添加到 .Right 值中。
        • @Eclypse19:我想过。但是我担心系统之间的差异会有所不同。如果你确定不是这样,我会沿着这条路走。
        猜你喜欢
        • 2022-01-20
        • 2015-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-05
        • 1970-01-01
        • 2010-12-09
        • 2013-06-17
        相关资源
        最近更新 更多