Dynamic Form Components at Design Time

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to create dynamic content in a Windows Form using external data
to determine UI elements. The code works fine at runtime. I have tried
adding code in Sub New, after InitializeComponent.

InitializeComponent apparently runs at design time as the design time view
of the Form is created, but the code after the call to InitializeComponent
appears not to run.

Is there a way to get programmer written code (not created by the code
editor) to run at design time from a Form?
 
Hi, I add code in Sub New, after InitializeComponent ans it works :

Public Class Form2

Inherits System.Windows.Forms.Form

Public Sub New()

MyBase.New()

InitializeComponent()

addLabel()

End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

Private components As System.ComponentModel.IContainer


<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container()

Me.Text = "Form2"

End Sub

Protected Sub addLabel()

Dim lab1 As New Label

lab1.Location = New System.Drawing.Point(8, 8)

lab1.Name = "Label1"

lab1.Size = New System.Drawing.Size(284, 40)

lab1.TabIndex = 1

lab1.Text = "Hello World !"

Me.Controls.Add(lab1)

End Sub

End Class


Regards.
 
Thanks for your reply. I pasted your code into my Form exactly as you have
it. Are you saying that this works for you at design time? In my
application, the code in the form's Sub New(), after InitializeComponent,
only works at runtime. I am using the first release of VB.Net.
 
Are you saying that you want the Form Designer to read your code to show the
form? For example, you want the designer to call a routine that looks in a
database for some parameters. No the designer will not so this.

Chris
 
Thanks for your input.

Chris said:
Are you saying that you want the Form Designer to read your code to show the
form? For example, you want the designer to call a routine that looks in a
database for some parameters. No the designer will not so this.

Chris
 
Back
Top