Solved

Setting location of a form

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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