Dynamic Form Components at Design Time

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?
 
M

MN

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.
 
G

Guest

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.
 
C

Chris, Master of All Things Insignificant

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top