Question Creating a new form at runtime

pjfatboy

Active member
Joined
Nov 30, 2012
Messages
35
Programming Experience
Beginner
I have never created a form at runtime but I need to in the project I am working on. My question is, when I create an instance of a form, will that new form have all of the controls of the original form I am creating the new form from without adding them through code?
 
Hi,

The simple answer is to give it a go and see what happens. Try this from a button on any form (Change Form1 to a form in your Project):-

VB.NET:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    Dim myRunTimeForm As New Form1
    myRunTimeForm.Show()
End Sub

Hope that helps and Merry Christmas.

Cheers,

Ian
 
Please forgive my ignorance as I try to learn.

I googled "what is runtime for visual basic" and got some information about runtime being a thing of the past because VB6 is a part of Windows.

I don't get what's going on here with the myRunTimeForm

I was trying to figure out how to kick of events when my form opens, and I had a hard time digging through the Google results because I wasn't asking the question properly. I finally came across the following which was helpful to me:

VB.NET:
Public Class Form1


    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
         'put events here that occur when the form loads
    End Sub

End Class

Will you please help me understand the difference between this and what you are describing as "at runtime"?

Thank you
 
Hi,

Just to clarify, "Runtime" is a term that is used to refer to something that happens when you Run your application either by pressing F5 within your project or running your application from your compiled executable file.

In this case, I used the variable name myRunTimeForm because I created a "New Instance" of a form at runtime that had been created within the IDE during design time. This is just a name and could have been anything but I chose that name since it described what I was doing at the time.

The next thing to consider in your post is event handling. If you want something to happen when your form loads then you would code the "Load" event handler of the form. To access this handler, just double click the form in design mode and it will automatically create the subroutine for you which can then be coded as you wish. i.e:-

VB.NET:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  'Put your code here that you need in the load sequence
End Sub

To summarise your post however, I get the feeling that you need some structured guidance in learning VB.NET so have a look here to see if this can help you better:-

Microsoft Visual Basic .NET tutorials for Beginners

Hope that helps,

Cheers,

Ian
 
Wow, #1 THANK YOU for the detailed explanation! That really, really helps!

And, #2 THANK YOU for explaining the Form1_Load to me. While I was researching that I read some stuff that caused me to (INCORRECTLY) think that the stuff that happens when the application starts for Form1 was controlled "behind the scenes" (although any other form you create in Visual Studio allows you the option to control every aspect of its behavior). It made me nervous about jacking with that behind the scenes stuff -especially when I found that InitializeComponents Method while digging around, and it said:

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.

And, #3 THANK YOU for Microsoft Visual Basic .NET tutorials for Beginners. It made me laugh when I opened it and saw that I had already opened half of the links from that page -in a seemingly random order, but I really only read the ones as I found them useful while I was learning via trial-and-error (the hard way...). That is a GREAT, GREAT site. I should have taken the time to read them all in order. It's difficult traversing the mountains of available information to find such gems. If you have any other recommendations I will be grateful. I have been jacking with this thing for about 18 hours a day for the last week... It's bittersweet... Four weeks off from medical school and I'm so freaking used to studying 18 hours a day I just can't break the behavior, and I need a break from medicine so I used it to create, of all things a flash card application to learn medicine! (just for "fun"!!)

It is painfully obvious I would have been much more efficient if I had taken the time to learn more basic stuff in the beginning instead of trudging through it...

THANK YOU for this and all the other help you and others have provided. This forum is awesome, and it has a great resource for me as I learn.
 
Back
Top