计算新的表单位置

时间:2013-09-05 08:11:33

标签: vb.net

我的表格带有按钮“btn_email”。

按下此按钮,我希望在该按钮下面打开新的(非模态)表格并与他对齐。

Dim eform As New frm_iemail
With eform
    .Location = ?
    .Show(Me)
End With

计算新表格(描述)位置的最佳方法是什么? 这个计算应该如何?

在Maurice的解决方案后编辑:

Dim eform As New frm_iemail
With eform
    .StartPosition = FormStartPosition.Manual
    .Location = New Point((Me.Left + btn_email.Left + Button1.Width), (Me.Top + btn_email.Top))
    .Show(Me)
End With

Approach2:

Dim BorderWidth As Integer = (Me.Width - Me.ClientSize.Width) / 2
Dim TitlebarHeight As Integer = Me.Height - Me.ClientSize.Height - 2 * BorderWidth
.DesktopLocation = New Point((Me.Left + Button1.Left + Button1.Width + BorderWidth), (Me.Top + TitlebarHeight + BorderWidth + Button1.Top))

我的解决方案:

Dim BorderWidth As Integer = SystemInformation.FrameBorderSize.Width
Dim TitlebarHeight As Integer = SystemInformation.CaptionHeight + BorderWidth
Dim distance As Integer = 3

Dim eform As New frm_iemail
With eform
    .StartPosition = FormStartPosition.Manual
    .FormBorderStyle = Windows.Forms.FormBorderStyle.None
    .Location = New Point(Me.Location.X + btn_email.Location.X + btn_email.Width + BorderWidth - .Width, TitlebarHeight + Me.Location.Y + btn_email.Location.Y + btn_email.Height + distance)
    .Show(Me)
End With

最终解决方案:

.Location = New Point(Me.Location.X + btn_email.Right + BorderWidth - .Width, TitlebarHeight + Me.Location.Y + btn_email.Bottom + distance)

3 个答案:

答案 0 :(得分:2)

下面的代码找到按钮下方的表格,右对齐两个控件。我对右对齐的理解是:eform的右侧Y值与btn_email的右侧相同:

With eform
    .Show(Me)
    .Location = New Point(Me.Left + btn_email.Right - .Width, Me.Top + btn_email.Bottom + btn_email.Height)
    .BringToFront()
End With

注意:根据表格的类型(例如边框),可能会有一个小的差距;但是已经通过另一个答案(以及你问题中的最后一次更新)来处理这个问题。

答案 1 :(得分:1)

请注意,它是C#,但它应该足以实现您的vb.net解决方案。

    private void button1_Click(object sender, EventArgs e)
    {
        // Create and show the form
        Form1 form = new Form1();            
        form.Show();

        // Caculate thicknesses of border and titlebar
        int borderWidth = (this.Width - this.ClientSize.Width) / 2;
        int titlebarHeight = this.Height - this.ClientSize.Height - 2 * borderWidth;

        // Calculate the form position
        var x = this.Left + button1.Left + button1.Width + borderWidth - form.Width;
        var y = this.Top + titlebarHeight + borderWidth + button1.Top + button1.Height;

        // Position the form
        form.DesktopLocation = new Point(x, y);
    }

答案 2 :(得分:0)

试试这个..

Dim eform As New frm_iemail
With eform
    .Location = new point(Me.Location.X + btn_email.Location.X + btn_email.Width, Me.Location.Y + btn_email.Location.Y + btn_email.Height)
    .Show(Me)
End With