Solved

Setting location of a form

Posted on 2016-11-04
4
35 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

816 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

12 Experts available now in Live!

Get 1:1 Help Now