Results 1 to 6 of 6

Thread: [RESOLVED] Where is VB Net Startup code so I can initialze items on the GUI?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    182

    Resolved [RESOLVED] Where is VB Net Startup code so I can initialze items on the GUI?

    I don't see any place, or file, which has a location for me to place any initialization code for my GUI/Program. The only files available are as seen in the screenshot. So what file did I not get from the loader, or in what file do I place my code? I know in some forms I generated for experimental purposes there was a MyApplication page, but none in the Windows Forms App (Net).

    Appreciate your advise.

    Well..... Seems the drag drop/add files does not accept my picture.

    So here is a list of available files.

    Solution Items
    - ClsBrowser
    -ClsEPIQ
    -ClsErrorLog

    EPIQ Interface
    -My Project
    -References
    -App.config
    -Form1

    Did the program get generated without a needed file? I have looked in all of the files for some sore of startup point and have not found one for initializing my items.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,084

    Re: Where is VB Net Startup code so I can initialze items on the GUI?

    Follow these steps:
    1. From the menu bar, click on Project > {Project Name} Properties
    2. In the Application tab, click on View Application Events
    3. In the dropdowns at the top of the file, generate the Startup event
    4. Alternatively, just copy/paste this method:

    Code:
    Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
    
    End Sub
    Anything that you put in the Startup event will fire when the application starts but before the startup form has been created.

    Alternatively, if you want code to run anytime a form is initialized then you can use the constructor of the class:
    Code:
    Public Class Form1
    
        Public Sub New()
            ' controls have not been created yet
    
            InitializeComponent()
    
            ' controls have been created        
        End Sub
    
    End Class
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    182

    Re: Where is VB Net Startup code so I can initialze items on the GUI?

    Quote Originally Posted by dday9 View Post
    Follow these steps:
    1. From the menu bar, click on Project > {Project Name} Properties
    2. In the Application tab, click on View Application Events
    3. In the dropdowns at the top of the file, generate the Startup event
    4. Alternatively, just copy/paste this method:

    Code:
    Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
    
    End Sub
    Anything that you put in the Startup event will fire when the application starts but before the startup form has been created.

    Alternatively, if you want code to run anytime a form is initialized then you can use the constructor of the class:
    Code:
    Public Class Form1
    
        Public Sub New()
            ' controls have not been created yet
    
            InitializeComponent()
    
            ' controls have been created        
        End Sub
    
    End Class
    Thank you!

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    182

    Re: Where is VB Net Startup code so I can initialze items on the GUI?

    Quote Originally Posted by FunkMonkey View Post
    Thank you!
    OK, another question. In the properties DLG box the configuration = N/A, Platform = N/A, and Configuration manager are all disabled. If I use your code method where would I put it? In the Form Class?

    Well wasnt the last question stupid? Sorry. But what does Sub New() do for the issue?
    Last edited by FunkMonkey; Mar 17th, 2025 at 01:55 PM.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,084

    Re: Where is VB Net Startup code so I can initialze items on the GUI?

    Quote Originally Posted by FunkMonkey View Post
    OK, another question. In the properties DLG box the configuration = N/A, Platform = N/A, and Configuration manager are all disabled. If I use your code method where would I put it? In the Form Class?
    The configuration and platform options are properties of the project and I don't think that they're accessible or changeable at runtime. Think of them as metadata that tells the compiler how it should behave. They appear disabled in the Application tab of the project's properties because they are managed at the solution level through the Configuration Manager via Build > Configuration Manager. But you're saying that the Configuration Manager is not accessible?

    Quote Originally Posted by FunkMonkey View Post
    Well wasnt the last question stupid? Sorry. But what does Sub New() do for the issue?
    It wasn't immediately obvious what you were trying to do in your original post, so I wanted to throw that out there in case it was useful. It doesn't sound like you even need the application's startup event either though.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,796

    Re: Where is VB Net Startup code so I can initialze items on the GUI?

    Sub New is the constructor for the class. There is some advantage to that, because it is called at a different time from other code in a form.

    Every form has a constructor. If you don't create one yourself, you will find one in the .designer.vb file associated with the form. It is best not to mess with that file unless you know what is in there, because the designer might overwrite any changes you make, or you might mess up the designer. Best to leave it alone, but it's good to take a look at, because it is just code like all other code. That's where the constructor is, the destructor, and it's where all the controls on the form are declared, created and set up.

    The key about the constructor is that if you were to do this:
    Code:
    Dim nf As New MyForm
    
    nf.ShowDialog
    then the constructor runs on the New MyForm, but things like the Load event, and all subsequent events, only occur when ShowDialog is called. Since there is no particular need for you to show a form immediately after creating it (though that is the typical way to do things), you can use that to some advantage in some rare cases. Slow running things could be launched by putting them in the constructor and creating the form long before you actually need to show it. I've used that at times, but really only a few times, as it is a pretty rare scenario. Still, it's good to know about.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width