Reputation: 4655
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)
Upvotes: 0
Views: 204
Reputation: 5831
Note that it is C# but it should be enough to implement your vb.net solution.
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);
}
Upvotes: 1
Reputation:
The code below locates the form below the button and right-aligns both controls. What I understand with right-aligning is: right side of eform
with same Y value than right side of 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
NOTE: there might be a small gap depending upon the type of form (e.g., borders); but this has already been taken care of by another answer (and by the last update in your question).
Upvotes: 2
Reputation: 5719
Try this ..
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
Upvotes: 0