Solved

Setting location of a form

Posted on 2016-11-04
4
32 Views
Last Modified: 2016-11-07
Hello,

How to set a location of a form just above its parent form.

Cheers
0
Comment
Question by:RIAS
  • 2
  • 2
4 Comments
 
LVL 33

Expert Comment

by:it_saige
ID: 41874410
You could use the StartPosition property to Center the child form over the parent form; e.g. -

Parent.vb -
Public Class Parent
	Private Sub OnClick(sender As Object, e As EventArgs) Handles Button1.Click
		Using child = New Child()
			child.StartPosition = FormStartPosition.CenterParent
			child.ShowDialog(Me)
		End Using
	End Sub
End Class

Open in new window

Parent.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Parent
	Inherits System.Windows.Forms.Form

	'Form overrides dispose to clean up the component list.
	<System.Diagnostics.DebuggerNonUserCode()> _
	Protected Overrides Sub Dispose(ByVal disposing As Boolean)
		Try
			If disposing AndAlso components IsNot Nothing Then
				components.Dispose()
			End If
		Finally
			MyBase.Dispose(disposing)
		End Try
	End Sub

	'Required by the Windows Form Designer
	Private components As System.ComponentModel.IContainer

	'NOTE: The following procedure is required by the Windows Form Designer
	'It can be modified using the Windows Form Designer.  
	'Do not modify it using the code editor.
	<System.Diagnostics.DebuggerStepThrough()> _
	Private Sub InitializeComponent()
		Me.Button1 = New System.Windows.Forms.Button()
		Me.SuspendLayout()
		'
		'Button1
		'
		Me.Button1.Font = New System.Drawing.Font("Microsoft Sans Serif", 16.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
		Me.Button1.Location = New System.Drawing.Point(77, 91)
		Me.Button1.Name = "Button1"
		Me.Button1.Size = New System.Drawing.Size(130, 78)
		Me.Button1.TabIndex = 1
		Me.Button1.Text = "Open" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "Child"
		Me.Button1.UseVisualStyleBackColor = True
		'
		'Parent
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 261)
		Me.Controls.Add(Me.Button1)
		Me.Name = "Parent"
		Me.Text = "Parent"
		Me.ResumeLayout(False)

	End Sub
	Friend WithEvents Button1 As System.Windows.Forms.Button

End Class

Open in new window

Child.vb -
Public Class Child

End Class

Open in new window

Child.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Child
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.Text = "Child"
    End Sub
End Class

Open in new window


Which produces the following output -


Initial load -Capture.JPGAfter pressing the button -Capture.JPG
-saige-
0
 

Author Comment

by:RIAS
ID: 41875573
Thanks Sir, but needed the form to be just above main form. Sorry about the typo. Also it is not a mdi form. They are two seperate form. Needed to display two forms one below and the other above it.
Thanks
0
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 41877121
The forms demonstrated above are not MDI forms either.  But in order to have the child form show above the parent, you will have to manually calculate the position of the parent and set the child location accordingly; e.g. -

Parent.vb -
Public Class Parent
	Private Sub OnClick(sender As Object, e As EventArgs) Handles Button1.Click
		Using child = New Child()
			child.ShowDialog(Me)
		End Using
	End Sub
End Class

Open in new window

Parent.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Parent
	Inherits System.Windows.Forms.Form

	'Form overrides dispose to clean up the component list.
	<System.Diagnostics.DebuggerNonUserCode()> _
	Protected Overrides Sub Dispose(ByVal disposing As Boolean)
		Try
			If disposing AndAlso components IsNot Nothing Then
				components.Dispose()
			End If
		Finally
			MyBase.Dispose(disposing)
		End Try
	End Sub

	'Required by the Windows Form Designer
	Private components As System.ComponentModel.IContainer

	'NOTE: The following procedure is required by the Windows Form Designer
	'It can be modified using the Windows Form Designer.  
	'Do not modify it using the code editor.
	<System.Diagnostics.DebuggerStepThrough()> _
	Private Sub InitializeComponent()
		Me.Button1 = New System.Windows.Forms.Button()
		Me.SuspendLayout()
		'
		'Button1
		'
		Me.Button1.Font = New System.Drawing.Font("Microsoft Sans Serif", 16.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
		Me.Button1.Location = New System.Drawing.Point(77, 91)
		Me.Button1.Name = "Button1"
		Me.Button1.Size = New System.Drawing.Size(130, 78)
		Me.Button1.TabIndex = 1
		Me.Button1.Text = "Open" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10) & "Child"
		Me.Button1.UseVisualStyleBackColor = True
		'
		'Parent
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 261)
		Me.Controls.Add(Me.Button1)
		Me.Name = "Parent"
		Me.Text = "Parent"
		Me.ResumeLayout(False)

	End Sub
	Friend WithEvents Button1 As System.Windows.Forms.Button

End Class

Open in new window

Child.vb -
Public Class Child
	Private Sub OnLoad(sender As Object, e As EventArgs) Handles MyBase.Load
		If Owner IsNot Nothing AndAlso TypeOf Owner Is Form Then
			Dim frm = CType(Owner, Form)
			Location = New Point(frm.Location.X, frm.Location.Y - frm.Height)
		End If
	End Sub
End Class

Open in new window

Child.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Child
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
		Me.SuspendLayout()
		'
		'Child
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(284, 261)
		Me.Name = "Child"
		Me.Text = "Child"
		Me.ResumeLayout(False)

	End Sub
End Class

Open in new window

Assuming that there is enough space on the screen to show the child fully above the parent, pressing the button produces the following output -Capture.JPG-saige-
0
 

Author Closing Comment

by:RIAS
ID: 41877171
Can't thank you enough! Worked perfectly!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now