Child form always in front of parent in Win Forms

Hello, how about the idea is something simple if I wanted to open a second form within the same form how could it be done?


image

I don’t think this is Possible through the Data-Shapes package, you would have to look into Windows Forms. Also, why do you want to do this? Maybe it isn’t really necessary.

Here an example

a child winform

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *


clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')
import System.Drawing
import System.Windows.Forms

from System.Drawing import *
from System.Windows.Forms import *

class MainForm(Form):
	def __init__(self):
		self.InitializeComponent()
	
	def InitializeComponent(self):
		self._button1 = System.Windows.Forms.Button()
		self.SuspendLayout()
		# 
		# button1
		# 
		self._button1.Location = System.Drawing.Point(97, 109)
		self._button1.Name = "button1"
		self._button1.Size = System.Drawing.Size(94, 37)
		self._button1.TabIndex = 0
		self._button1.Text = "button1"
		self._button1.UseVisualStyleBackColor = True
		self._button1.Click += self.Button1Click
		# 
		# Form13
		# 
		self.ClientSize = System.Drawing.Size(284, 261)
		self.Controls.Add(self._button1)
		self.Name = "MainForm"
		self.Text = "MainForm"
		self.ResumeLayout(False)


	def Button1Click(self, sender, e):
		try:
			newMDIChild = ChildForm(self)
			newMDIChild.StartPosition = FormStartPosition.CenterParent
			newMDIChild.ShowDialog(self)
		except Exception as ex:
			print(ex)
			
		
		
class ChildForm(Form):
	def __init__(self, parent):
		self._parent = parent
		self.InitializeComponent()
	
	def InitializeComponent(self):
		self._label1 = System.Windows.Forms.Label()
		self.SuspendLayout()
		# 
		# label1
		# 
		self._label1.Location = System.Drawing.Point(47, 41)
		self._label1.Name = "label1"
		self._label1.Size = System.Drawing.Size(178, 37)
		self._label1.TabIndex = 0
		self._label1.Text = "I'm a child\nMy parent is '{}'".format(self._parent.Name)
		self._label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
		# 
		# Form14
		# 
		self.ClientSize = System.Drawing.Size(284, 128)
		self.Controls.Add(self._label1)
		self.Name = "ChildForm"
		self.Text = "ChildForm"
		self.ResumeLayout(False)
		
MainForm().ShowDialog()
6 Likes

As always a very elaborate answer, thanks for your great contributions.

1 Like

HI @c.poupin

Sorry my language is not good
How can I put STRING from Main Form TO Displays in Child Form self._label1.Text

AS PICTURE


	
	def Button1Click(self, sender, e):
		try:
		    STR="TEST"
			newMDIChild = ChildForm(self , STR)
			newMDIChild.StartPosition = FormStartPosition.CenterParent
			newMDIChild.ShowDialog(self)
		except Exception as ex:
			print(ex)
	

		
class ChildForm(Form):
	def __init__(self, parent):
		self._parent = parent
		STR
		self.InitializeComponent()
	
	def InitializeComponent(self):
		self._label1 = System.Windows.Forms.Label()
		self.SuspendLayout()
		# 
		# label1
		# 
		self._label1.Location = System.Drawing.Point(47, 41)
		self._label1.Name = "label1"
		self._label1.Size = System.Drawing.Size(178, 37)
		self._label1.TabIndex = 0
		self._label1.Text = STR
		self._label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

Annotation 2022-10-05 151622

Hi,
here an example

test child form

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *


clr.AddReference('System.Drawing')
clr.AddReference('System.Windows.Forms')
import System.Drawing
import System.Windows.Forms

from System.Drawing import *
from System.Windows.Forms import *

class MainForm(Form):
	def __init__(self):
		self.InitializeComponent()
	
	def InitializeComponent(self):
		self._button1 = System.Windows.Forms.Button()
		self._textBox1 = System.Windows.Forms.TextBox()
		self._label1 = System.Windows.Forms.Label()
		self.SuspendLayout()
		# 
		# button1
		# 
		self._button1.Location = System.Drawing.Point(98, 148)
		self._button1.Name = "button1"
		self._button1.Size = System.Drawing.Size(94, 37)
		self._button1.TabIndex = 0
		self._button1.Text = "My Button"
		self._button1.UseVisualStyleBackColor = True
		self._button1.Click += self.Button1Click
		# 
		# textBox1
		# 
		self._textBox1.Location = System.Drawing.Point(61, 102)
		self._textBox1.Name = "textBox1"
		self._textBox1.Size = System.Drawing.Size(167, 20)
		self._textBox1.TabIndex = 1
		# 
		# label1
		# 
		self._label1.Location = System.Drawing.Point(61, 73)
		self._label1.Name = "label1"
		self._label1.Size = System.Drawing.Size(167, 23)
		self._label1.TabIndex = 2
		self._label1.Text = "Enter a text"
		# 
		# Form1
		# 
		self.ClientSize = System.Drawing.Size(284, 261)
		self.Controls.Add(self._label1)
		self.Controls.Add(self._textBox1)
		self.Controls.Add(self._button1)
		self.Name = "Form1"
		self.Text = "MainForm"
		self.ResumeLayout(False)
		self.PerformLayout()


	def Button1Click(self, sender, e):
		try:
			newMDIChild = ChildForm(self)
			newMDIChild.StartPosition = FormStartPosition.CenterParent
			newMDIChild.ShowDialog(self)
		except Exception as ex:
			print(ex)
			
		
		
class ChildForm(Form):
	def __init__(self, parent):
		self._parent = parent
		self.InitializeComponent()
	
	def InitializeComponent(self):
		self._label1 = System.Windows.Forms.Label()
		self.SuspendLayout()
		# 
		# label1
		# 
		self._label1.Location = System.Drawing.Point(47, 41)
		self._label1.Name = "label1"
		self._label1.Size = System.Drawing.Size(178, 37)
		self._label1.TabIndex = 0
		self._label1.Text = "the enter text is '{}'".format(self._parent._textBox1.Text)
		self._label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
		# 
		# Form14
		# 
		self.ClientSize = System.Drawing.Size(284, 128)
		self.Controls.Add(self._label1)
		self.Name = "ChildForm"
		self.Text = "ChildForm"
		self.ResumeLayout(False)
		
MainForm().ShowDialog()
2 Likes

@c.poupin
Thank you very much