Adding new objects using another form

ambokiko

Member
Joined
Nov 9, 2006
Messages
11
Programming Experience
Beginner
say i want to add new labels to form2 using form1. anyone got an idea how i can do this?
 
Example code somewhere in Form1:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] f [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Form2[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] l [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Label[/SIZE]
[SIZE=2]l.Name = [/SIZE][SIZE=2][COLOR=#800000]"mylabel1"[/COLOR][/SIZE]
[SIZE=2]l.Text = [/SIZE][SIZE=2][COLOR=#800000]"hello"[/COLOR][/SIZE]
[SIZE=2]l.Location = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Point(10, 10)[/SIZE]
[SIZE=2]f.Controls.Add(l)[/SIZE]
[SIZE=2]l.BringToFront()[/SIZE]
[SIZE=2]f.Show()[/SIZE]
You may also want to consider letting the 'Form2' handle this by itself by providing a public method in this class you can access and which tell 'Form2' to create this label and everything else it may want to set up inside. Especially for adding dynamic event handlers this is preferred. Also in general OOP each object should best possible take care of its own inner workings, the above code example is kind of intrusive on this class behalf.

This topic has nothing to do with Deployment, so I move the thread to Windows Forms forum.
 
Back
Top