Results 1 to 31 of 31

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

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    313

    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
    11,753

    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?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    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
    313

    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
    11,753

    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?"
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    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
    313

    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
    313

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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
    313

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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
    313

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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
    313

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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
    313

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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
    313

    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
    313

    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.

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    313

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

    Had to revisit this again today.

    Microsoft Visual Studio Community 2022 (64-bit)
    Version 17.0.5

    Renamed Form1 to frmMain and got a build error.
    Changed Startup Form from Form1 to frmMain and it worked.

  24. #24
    New Member
    Join Date
    Apr 2022
    Posts
    4

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

    Hi. Stumbled in this stuff today in VS 2022. Noticed that Core 6 have "sub main" as startup object.
    And if i change it to Form1, all controls start looking classic, like from Windows 98.
    Also changed it back, and they still look classic, until i check/uncheck/check "enable application framework"

    After reading post #19, and while writing it, i found out that, to change what form Sub Main i should start, i should change Me.MainForm
    but also here in sub new, enablevisualstyles=false for some reason
    if i manually change it to true, controls become ok

    this forum only thing i found about this "feature", and at least i know that it's not bug, and how to access application.designer.vb without clicking on error

    i thought Enable XP visual styles do something with this, but it's feel like it do nothing

  25. #25
    New Member
    Join Date
    Apr 2022
    Posts
    4

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

    (cant edit post apparently)
    also it seems Me.MainForm= is constantly resetting to Form1. I'm not sure after what exactly.
    It's like i renamed form file, changed mainform = to FMain
    then i change target CPU to x64, and it's error again, and mainform once again is Form1
    i fixed it again, and then after some time, i think after i edited some resources (like images for picture box), it's again become Form1
    i don't get it.

  26. #26
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

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

    If you're changing stuff in the .designer file(s) ... stop... that's the code file that is automatically written to by VS... it's not meant to be edited directly. The stuff you're changing should be done through the Application Properties dialog... which is what writes out to that designer file.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  27. #27
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

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

    Quote Originally Posted by techgnome View Post
    If you're changing stuff in the .designer file(s) ... stop... that's the code file that is automatically written to by VS... it's not meant to be edited directly. The stuff you're changing should be done through the Application Properties dialog... which is what writes out to that designer file.

    -tg
    That's generally true but there is an issue with .NET Core projects that requires some manual attention, as per post #19.

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

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

    Quote Originally Posted by SilentPrayerCG View Post
    (cant edit post apparently)
    also it seems Me.MainForm= is constantly resetting to Form1. I'm not sure after what exactly.
    It's like i renamed form file, changed mainform = to FMain
    then i change target CPU to x64, and it's error again, and mainform once again is Form1
    i fixed it again, and then after some time, i think after i edited some resources (like images for picture box), it's again become Form1
    i don't get it.
    I've seen that in VS2019, but only when targeting .NET 5, not framework. It seems a trivial bug, so I'm surprised that it has persisted into VS2022. Sounds like it has, though.
    My usual boring signature: Nothing

  29. #29
    New Member
    Join Date
    Apr 2022
    Posts
    4

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

    Quote Originally Posted by Shaggy Hiker View Post
    I've seen that in VS2019, but only when targeting .NET 5, not framework. It seems a trivial bug, so I'm surprised that it has persisted into VS2022. Sounds like it has, though.
    Well I'm targeting NET 6, and yeah, i don't remember such problems in Net Framework. Also in framework you can just select startup object and it didn't acted weird (like broken visual style for controls)
    2022 generally feels under cooked - I'm used to double click form file in solution explorer to open form designer, but in new Studio it often become broken, and i need close form tab and open it again, or reopen whole studio. Sometimes designer opens but it's broken, i need to hover mouse over buttons and stuff so they appear on screen.

  30. #30
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

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

    Keep in mind that everything had to be reimplemented in .NET Core and some of the VB-specific features were problematic. They obviously still haven't got this part right so you should report it to Microsoft. The more people complain about an issue, the more likely it is to get fixed. If you're just creating small VB WinForms projects for fun and/or learning, I'd recommend sticking with .NET Framework for now as there's little to be gained from using .NET Core right now.

  31. #31
    New Member
    Join Date
    Apr 2022
    Posts
    4

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

    jmcilhinney I've read a little about difference between 4.8 and net, as i understand net allow you to port your stuff for phones and such.
    do net even have any advantages in speed or something in comparison with framework 4.8?
    i decided to try out net 6, just because i have feeling that net framework was abandoned, it's 4.8 for long time, and only net getting new updates.

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