0

I have form with button "btn_email".

By pressing to this button I would like new (non modal) form to be opened below that button and right aligned with him.

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

Which is the best way to calculate this (described) position of new form?
How this calculation should look like?

EDIT after Maurice's solution:

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))

My solution:

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

Final solution:

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

3 回答 3

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

注意:根据表格的类型(例如边框),可能会有一个小的间隙;但这已经被另一个答案(以及您问题的最后更新)处理了。

于 2013-09-05T08:40:57.413 回答
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);
    }
于 2013-09-05T08:21:14.257 回答
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
于 2013-09-05T08:34:11.177 回答