ok. HVis jeg nu har en panel i min Mainform som jeg vil kunne tilføje usercontroller i fra alle steder i min application. Hvordan skulle jeg så definere den?
en måde er at sende en refernce til formen med rundt til de objekter som skal bruge formen
her kommer et simpelt eksempel:
Imports System Imports System.Drawing Imports System.Windows.Forms
Namespace E
Public Class MainForm Inherits Form Private button1 As Button Private textBox1 As TextBox
Public Sub New() InitializeComponent End Sub
Private Sub InitializeComponent() textBox1 = New TextBox button1 = New Button SuspendLayout textBox1.Multiline = True textBox1.Location = New Point(50, 50) textBox1.Size = New Size(200, 50) button1.Location = New Point(50, 150) button1.Size = New Size(200, 50) button1.Text = "Åben sub form" AddHandler button1.Click, AddressOf Button1Click ClientSize = New Size(300, 250) Controls.Add(button1) Controls.Add(textBox1) Text = "Main Form" ResumeLayout(False) End Sub
Public ReadOnly Property TB() As String Get Return textBox1.Text End Get End Property
Sub Button1Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim sf As SubForm = New SubForm(Me) sf.ShowDialog End Sub End Class
Public Class SubForm Inherits Form Private mf As MainForm Private textBox1 As TextBox
Public Sub New(ByVal mf As MainForm) Me.mf = mf InitializeComponent End Sub
Private Sub InitializeComponent() textBox1 = New TextBox SuspendLayout textBox1.Multiline = True textBox1.Location = New Point(50, 50) textBox1.Size = New Size(200, 50) textBox1.Text = mf.TB ClientSize = New Size(300, 150) Controls.Add(textBox1) Text = "Sub Form" ResumeLayout(False) End Sub End Class
Public Class TestClass
<STAThread()> _ Public Shared Sub Main(ByVal args As String()) Application.Run(New MainForm) End Sub End Class End Namespace
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.