به نام خدا
سلام؛
در اینجا می خوایم یک فرم رو تبدیل کنیم به Child Control و اون رو در یک Panel که یک Container Control هستش در یک فرم دیگر نمایش دهیم، و برای مثال یک خصوصیت داریم که مقدار اون رو در فرم والد یا Parent فراخوانی می کنیم:
این تصویر و کدهای فرمی هست که می خوایم شیء فرزند بشه:
Child Form.jpg
Public Class Form1
Private Expr As New Dictionary(Of String, String)
Public Property SelectedItem() As KeyValuePair(Of String, String)
Get
Return Me.ComboBox1.SelectedItem
End Get
Set(ByVal value As KeyValuePair(Of String, String))
Me.ComboBox1.SelectedItem = value
End Set
End Property
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
With Expr
.Add("Item1", "Item1 Is selected.")
.Add("Item2", "Item2 Is selected.")
.Add("Item3", "Item3 Is selected.")
.Add("Item4", "Item4 Is selected.")
End With
With Me.ComboBox1.Items
.Clear()
For Each Item As KeyValuePair(Of String, String) In Expr
.Add(Item)
Next
End With
Me.ComboBox1.SelectedIndex = 0
End Sub
Private Sub ComboBox1_DrawItem(sender As System.Object, e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
Dim G As Graphics = e.Graphics
Dim V As String = CType(Me.ComboBox1.Items(e.Index), KeyValuePair(Of String, String)).Key
e.DrawBackground()
G.DrawString(V, e.Font, New SolidBrush(e.ForeColor), 2, e.Bounds.Y + 2)
End Sub
End Class
و این هم فرم اصلی و کد فراخوانی فرم اول در یک Panel:
Parent Form.jpg
Public Class HelloWorld
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
With Form1.SelectedItem
MsgBox(String.Format("Selected Item Key is {0} and Value is: {1}", .Key, .Value))
End With
End Sub
Private Sub HelloWorld_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
With Form1
.TopLevel = False
.WindowState = FormWindowState.Maximized
.FormBorderStyle = Windows.Forms.FormBorderStyle.None
.Visible = True
End With
Me.Panel1.Controls.Add(Form1)
End Sub
End Class
این هم حالت RunTime:
Parent Form RunTime.jpg