Results 1 to 22 of 22

Thread: Startup Object is "Sub Main"? Not Form1?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    311

    Startup Object is "Sub Main"? Not Form1?

    I'm running VS 2019
    v.16.8.4

    I created a new project.
    Project -> Properties -> Application -> Startup Object is "Sub Main"

    It always used to be Form1.

    But, if I change startup object to Form1, and then change the name of the form, things break.
    So, I left it as "Sub Main"

    Did this change?

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

    Re: Startup Object is "Sub Main"? Not Form1?

    Does Form1 actually exist in your project or could the name (reference) have been changed with the filename staying Form1?

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,684

    Re: Startup Object is "Sub Main"? Not Form1?

    But, if I change startup object to Form1, and then change the name of the form, things break.
    That doesn't explain much, "things break". Also, how did you change the forms name?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    311

    Re: Startup Object is "Sub Main"? Not Form1?

    Quote Originally Posted by dday9 View Post
    Does Form1 actually exist in your project or could the name (reference) have been changed with the filename staying Form1?
    It is initially called Form1.
    Then I rename it to frmMain in the Properties window.
    The filename is still Form1.vb

    If startup Object was Form1, there are build errors.

    If startup Object was sub main, there are build errors.
    "Sub Main not found"

    In prior versions of VB, startup objects of Form1 would be automatically be updated to frmMain.
    No longer.

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

    Re: Startup Object is "Sub Main"? Not Form1?

    The way that I rename forms is by right-clicking on the file and clicking rename (or shortcut key F2). When you rename it like this, generally a little dialog will come up with something to the effect of "You have changed the form name, do you want to change every instance of Form1 to frmMain?"

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    9,379

    Re: Startup Object is "Sub Main"? Not Form1?

    If you are creating a Forms project is this what you are seeing?

    https://docs.microsoft.com/en-us/vis...t?view=vs-2019
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    311

    Re: Startup Object is "Sub Main"? Not Form1?

    Quote Originally Posted by dbasnett View Post
    If you are creating a Forms project is this what you are seeing?

    https://docs.microsoft.com/en-us/vis...t?view=vs-2019
    Yup, Windows Forms App

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    311

    Re: Startup Object is "Sub Main"? Not Form1?

    Quote Originally Posted by dday9 View Post
    The way that I rename forms is by right-clicking on the file and clicking rename (or shortcut key F2). When you rename it like this, generally a little dialog will come up with something to the effect of "You have changed the form name, do you want to change every instance of Form1 to frmMain?"
    It used to do this automatically

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,927

    Re: Startup Object is "Sub Main"? Not Form1?

    Quote Originally Posted by RipVoidWinkle View Post
    It used to do this automatically
    "It" still does do "that" automatically, but that's not what you're doing. It has never been the case that you can change the name of a type and have the file name changed automatically. It has always been the case that you can change the file name and have the type name changed automatically. You should be and have always been doing what dday9 described but you obviously didn't on this occasion. Do so in future.

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,927

    Re: Startup Object is "Sub Main"? Not Form1?

    As for the issue, you must not have the Application Framework enabled. If you do then the the field is labelled "Startup form" and you can only select a form. If you uncheck the "Enable application framework" box then the field label changes to "Startup object" and "Sub Main" becomes an option. Open the Application page of the project properties, make sure that the application framework is enabled and then select your preferred startup form.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    311

    Re: Startup Object is "Sub Main"? Not Form1?

    Quote Originally Posted by jmcilhinney View Post
    "It" still does do "that" automatically, but that's not what you're doing. It has never been the case that you can change the name of a type and have the file name changed automatically. It has always been the case that you can change the file name and have the type name changed automatically. You should be and have always been doing what dday9 described but you obviously didn't on this occasion. Do so in future.
    I never said anything about the filename.
    I never change the filename. It ALWAYS stays form1.vb
    But, in every project, I rename property name from Form1 to frmMain
    And the NAME cascades automatically to the Startup Object

    This works with 16.8.3
    Now, with 16.8.4, you will get build errors.

    It is related to the recent version update

    16.8.3
    Project Properties Application
    Target Framework = .NET 4.7.2
    Startup object = Form1

    16.8.4
    Project Properties Application
    Target Framework = .NET 5.0
    Startup object = Sub Main

    Enable Application Framework is checked/selected in both versions
    In 16.8.4, if you changed it from Sub Main, you will not get the cascade update
    Last edited by RipVoidWinkle; Feb 8th, 2021 at 07:47 PM.

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,927

    Re: Startup Object is "Sub Main"? Not Form1?

    Quote Originally Posted by RipVoidWinkle View Post
    I never change the filename. It ALWAYS stays form1.vb
    And that is bad so stop doing it. Change the file name and then accept the prompt to change the type name as well.

  13. #13
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,927

    Re: Startup Object is "Sub Main"? Not Form1?

    Quote Originally Posted by RipVoidWinkle View Post
    It is related to the recent version update

    16.8.3
    Project Properties Application
    Target Framework = .NET 4.7.2
    Startup object = Form1

    16.8.4
    Project Properties Application
    Target Framework = .NET 5.0
    Startup object = Sub Main
    So now we're getting somewhere. It's nothing to do with the VS version but the fact that you're targeting .NET Core (.NET 5.0 is .NET Core). While things may look pretty much the same, that's because everything has been rebuilt to provide VB.NET WinForms support in .NET Core. There are some minor changes, some things are still missing and there likely are some bugs.

    Based on my testing, if you change the name of the startup form, that change will not flow through to the MyApplication class and the OnCreateMainForm method. After the name change, a compilation error will be generated. You can double-click on that to be taken to the offending line of code and replace Form1 in the code yourself.

    This issue will likely be fixed in a future update to VS but higher priority issues get fixed first, so the more people who report this issue, the sooner it will be fixed. If you want the issue fixed sooner, you should provided feedback to Microsoft about it. It's for just this sort of thing that VS has a Send Feedback menu in the top, right corner.

    Let me stress again that this ONLY affects .NET Core projects, because everything has been rebuilt from the ground up. .NET Framework projects are unaffected.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    311

    Re: Startup Object is "Sub Main"? Not Form1?

    I am not deploying anything, and just running my programs on my laptop for myself.
    Should I just revert the target to 4.7.2 ?

  15. #15
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,927

    Re: Startup Object is "Sub Main"? Not Form1?

    Quote Originally Posted by RipVoidWinkle View Post
    I am not deploying anything, and just running my programs on my laptop for myself.
    Should I just revert the target to 4.7.2 ?
    That's completely up to you. There are certain improvements to .NET in .NET Core and particularly .NET 5.0 but they would likely be of little, if any, value to you in a basic WinForms application. You will lose little to nothing by reverting to .NET Framework 4.8 or some other recent version.

    Just note that it's not trivial to convert a project from .NET Core to .NET Framework. It can be done but it's not just a case of selecting a version in the properties. You can only change versions within .NET Framework or .NET Core, not between. There are instructions out there on how to do it but, if you have only just started, it may be easier just to create a new project.

    Also note that, in my opinion, the current issues with VB WinForms in .NET Core are fairly minor and there's every chance that they will be addressed in a soon-to-be-released VS update. If you prefer to stick with the latest and greatest then you may prefer to just live with the minor issues for now and enjoy what benefits .NET Core may bring now and in the future.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    311

    Re: Startup Object is "Sub Main"? Not Form1?

    I'll just leave it at 5.0 and keep StartUp Object to Sub Main

  17. #17
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,927

    Re: Startup Object is "Sub Main"? Not Form1?

    Quote Originally Posted by RipVoidWinkle View Post
    I'll just leave it at 5.0 and keep StartUp Object to Sub Main
    Like I said, if you do that and you change the name of the startup form's type, you will just need to make one manual change to the code where the startup form is created. You can get to that line by double-clicking in the Error List window so no big deal.

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    311

    Re: Startup Object is "Sub Main"? Not Form1?

    Quote Originally Posted by jmcilhinney View Post
    Like I said, if you do that and you change the name of the startup form's type, you will just need to make one manual change to the code where the startup form is created. You can get to that line by double-clicking in the Error List window so no big deal.
    What is weird is that this error is not always replicated.
    I just changed the form name and it worked now.
    No error.

    16.8.4
    Project Properties Application
    Target Framework = .NET 5.0
    Startup object = Sub Main

  19. #19
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,927

    Re: Startup Object is "Sub Main"? Not Form1?

    I've done a bit more testing and discovered a few things but I won't bother reporting most of them because they don't really matter. Basically, if you select a form as the startup object then that form has to contain its own Main method that you have written. You don't want that anyway, so you should leave the selection as Sub Main. in which case the auto-generated Main method will be used.

    If you enable Show All Files for the project in the Solution Explorer, you can access the Application.Designer.vb file under My Project -> Application.myapp. In that file, you'll find this method by default:
    vb.net Code:
    1. <Global.System.Diagnostics.DebuggerStepThroughAttribute()>  _
    2. Protected Overrides Sub OnCreateMainForm()
    3.     Me.MainForm = Form1
    4. End Sub
    If you actually select Form1 as the startup object, that changes to this:
    vb.net Code:
    1. <Global.System.Diagnostics.DebuggerStepThroughAttribute()>  _
    2. Protected Overrides Sub OnCreateMainForm()
    3.     Me.MainForm = Global.ProjectName.Form1
    4. End Sub
    If you change the name of your form with the default code, the form name should change there too and everything should work. That's what my testing showed, at least, but I have installed 16.8.5, so maybe that's changed. If you actually want to use a different form as the startup form, you can't select it in the properties without writing your own Main method, it would seem, so you can actually go into that file and edit it to set Me.MainForm using just the form name, e.g.
    vb.net Code:
    1. <Global.System.Diagnostics.DebuggerStepThroughAttribute()>  _
    2. Protected Overrides Sub OnCreateMainForm()
    3.     Me.MainForm = Form2
    4. End Sub

  20. #20
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,927

    Re: Startup Object is "Sub Main"? Not Form1?

    One point to note: don't name your startup form MainForm. If you do, you'll end up with:
    vb.net Code:
    1. Me.MainForm = MainForm
    in that code and it will be interpreted as the property being assigned to itself and fail at run time.

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    311

    Re: Startup Object is "Sub Main"? Not Form1?

    I admire your focus!

    I don't want to deal with any of that overriding main stuff.
    For now, I will leave Form1 as the name until this gets sorted out.

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    311

    Re: Startup Object is "Sub Main"? Not Form1?

    Revisiting this. I started a new project. Renamed Form1 to frmMain. Seems to work. Project compiles and runs. Startup Object is frmMain.

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