How to create forms in code...?

ianr900

Member
Joined
Jul 13, 2006
Messages
18
Location
UK
Programming Experience
3-5
Is it possible to display a number of forms using a loop? I can't see how to do it! What I am trying to do is use a loop to place a variable number of forms on the screen, a concept something like:

VB.NET:
for i = 1 to 12
frmInfo(i).show
next
I realise this is not correct syntax, but it shows the concept. So far, I can display the forms individually like this:

VB.NET:
frmZone1new.Text = "Zone " & i + 1
frmZone1new.StartPosition = FormStartPosition.Manual
frmZone1new.Location = New Point(Location(i, 0), Location(i, 1))
frmZone1new.Show()

frmZone2new.Text = "Zone " & i + 1
frmZone2new.StartPosition = FormStartPosition.Manual
frmZone2new.Location = New Point(Location(i, 0), Location(i, 1))
frmZone2new.Show()
But as there may be a large number, 50 or so, there must be a better way!
Any suggestions welcome.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
This simply shows how to use an array of forms for this purpose. First I declare the array at the top of the module/class with the number of forms to be held. Then in a button's click event I simply loop through that array setting all of them to a new instance of the form then setting the properties of the current form in the array:
VB.NET:
Private TheForms(3) As frmMain

Private Sub btnOpenForms_Click (...) Handles btnOpenForms.Click
  For Counter As Integer = 0 To TheForms.GetUpperBound(0)
    TheForms(Counter) = New frmMain
    With TheForms(Counter)
      .Text = "Zone " & CStr(Counter + 1)
      .StartPosition = FormStartPosition.Manual
      .Location = New Point(Location(Counter, 0), Location(Counter, 1))
      .Show
    End With
  Next Counter
End Sub
 
Top Bottom