繁体   English   中英

我想在 C# 中将两个表单并排放置

[英]I want to position two forms right next to each other in C#

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

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

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

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

newform.Left = this.endposition.right;

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

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

编辑

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

我总是得到相同的结果:

Why does this happen

我试过以下代码:

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 建议手动添加一些像素来定位新表单,尽管我不确定每个系统上的边框是否相同。

这对我有用:

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

尝试

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

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

您的代码工作正常,但您只需要将它放在 newForm.Show() 方法之后。

我知道这个问题很老,但是对于来这里寻找答案的人来说,您需要考虑两种表单边框的大小。 边框大小将根据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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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