Only Form1 in Startup Form in VS2022 - vb.net

I'm coding a project in Visual Basic using Visual Studio 2022 and want to start the program with a particular form. I have currently 8 forms. When I go to Solution Explorer/Properties/Application and select Startup Form, only a form named Form1 is in the dropdown list.
I have tried as much as I'm game as I'm afraid of messing it all up. I'm a new self taught programmer. Hope someone can help with not too much tech jargon.
Leif

I have the same issue. Both "Startup form" and "Enable application framework" are broken in Visual Studio 2022 when using VB.NET, WinForms, and .NET Framework. These both work if you target .NET 6.
My workaround is to close VS 2022, open the solution in VS 2019, set the Startup form, save, close VS 2019, and then re-open the solution in VS 2022.

I think In Visual studio 2022 IDE Designer tool deleted the application.designer.vb file content when startup form need to be change instead of modifying it.
This bug associate with enable application framework.
Create another framework project and make a backup of this file and modify as you want.
Don't stick with IDE...
they also creating IDE HELL (previously DLL l0l)
for example I paste application.designer.vb file content here
Option Strict On
Option Explicit On
Namespace My
Partial Friend Class MyApplication
<Global.System.Diagnostics.DebuggerStepThroughAttribute()> _
Public Sub New()
MyBase.New(Global.Microsoft.VisualBasic.ApplicationServices.AuthenticationMode.Windows)
Me.IsSingleInstance = false
Me.EnableVisualStyles = true
Me.SaveMySettingsOnExit = true
Me.ShutDownStyle = Global.Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses
End Sub
<Global.System.Diagnostics.DebuggerStepThroughAttribute()> _
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = Global.WindowsApp1.Form1
End Sub
End Class
End Namespace
=========================
IF want to stick with IDE
Just change the Application.MyApp file and edit MainForm tag
<MainForm> Write form name (without extension) </MainForm>
Write whatever your form name and get it on list....
no need to modify code...

Look in the Main method, there should be an Application.Run(New Form1()) call or similar. You can change Form1 to the form you want to run.
Remember that in the project properties you must set Main as the start object.

With .NET Framework you can create a class (eg. Program) with a static method (Shared in VB) called Main and set this method as startup object in the project properties and, as above mentioned, uncheck Enable application framework flag.
In the Main method you can start your preferred form using Application.Run.
Public Class Program
Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(True)
Application.Run(New Form2())
End Sub
End Class

Related

WinForms Startup Event not being handled

I have a Windows Forms Application in .NET 5 with Application Framework activated and the startup object set to (my) MainForm.
Using "View Application Events" in the application's properties, I auto-generated the ApplicationEvents.vb file and with the given controls auto-generated a method to do something on Startup (as I understand before even the MainForm loads) - but nothing in this method gets run, not even breakpoints are triggered.
I would assume an auto-generated sub in an auto-generated file designed for this should work and every other event handling sub does, just not Startup's.
This is my ApplicationEvents.vb (without the auto-generated comment):
Imports Microsoft.VisualBasic.ApplicationServices
Namespace My
Partial Friend Class MyApplication
Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
Debug.Print("Test")
MsgBox("Test")
End Sub
End Class
End Namespace
Background:
I'm trying to enable high DPI scaling for my application.
For this I've tried using the app.manifest file (opened by "View Windows Settings" in the application's properties), but no combination of tags I found in the docs worked (I'm running the required version of Windows 10 of cause).
The same for the app.config file, but I expected this because it's a .NET Framework feature to use it for that.
So I landed on Application.SetHighDpiMode(HighDpiMode). This worked when put into the MainForm's Loadevent, but it only did what was intended when the form was already loaded and it was then put on a scaled up screen / the screen was scaled up while it was already loaded.
If it was started on an already scaled screen, it looked jumbled.
So I figured that enabling it only on the form loading is just a bit too late and it should be run asap, so I landed on Startup.
The startup object has to be Sub Main for the Startup event to work, not MainForm (or any form at all).
(Thanks to #Hans Passant for the tip.)
If you want to change which one is the main form later on you have to do the following:
Close your project in Visual Studio.
Open your project's folder in explorer.
In it, open folder My Project.
Open Application.myapp with any text editor.
Change the form between the <MainForm> tags to your (new) main forms name.
Save and close.
Open ApplicationDesigner.vb with any text editor.
Find the following line and change YourMainFormsName to your (new) main forms name:
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = Global.YourProjectsName.YourMainFormsName
End Sub
Save and close.
Open your project again and start it up. The startup form should have changed.
This way you can keep the application framework and don't have to write your own Sub Main.

Buttons change style at run time

I have a vb.net Windows Forms application using Visual Studio 2010. At design time my buttons look like this:
at run time they seem to revert to a Windows classic style:
It only happens for this project which I took over from a developer who left. I want them to look like they do at design time. I'm stumped. Any ideas?
If the app starts from a Sub Main rather than a main form (See Project -> Properties -> StartUp Object) it might be missing this:
Public Sub Main()
' probably missing:
Application.EnableVisualStyles()
Application.Run(New Form1)
End Sub
When starting from a Main sub, be sure that EnableVisualStyles() is invoked very early in the procedure before any UI Objects are created.
If it starts from a main form, go to the same Project properties and be sure that both Enable application framework and Enable XP Visual Styles are checked.
If it still doesnt work, turn on Show All Files in Solution Explorer and open Application.myApp under My Project. Make sure this setting is true:
<EnableVisualStyles>true</EnableVisualStyles>
This file/setting should be managed by VS, so if it is not being updated to match the IDE, you might have other issues.

Unable to Set Startup Form in Visual Studio 2012 (VB.NET)

Okay, in Visual Studio 2012 I have a VB.NET project with originally two forms. Form1 was originally set as the startup form and then obsoleted. Form2 is a nearly identical form that has all of the new desirable functionality.
In my project's settings, Form2 doesn't appear as an available option for Startup Form. (Though it is a normally-created form that inherits Form) After some "troubleshooting," Form1 has been deleted, resulting in the the "Enable Application Framework" option being disabled and no Startup Object being available. When I select anything from the Startup Object drop-down, either Sub Main or Form2, I have an error message stating either "Sub Main not found in Solution" or "Form2 is a type in Solution and cannot be used as an expression" respectively. If I try to enable "Enable Application Framework," I receive an error popup stating "Startup object must be a form when 'Enable application framework' is checked." And Application.Designer.vb is empty.
Some things I've tried:
Clean and Rebuild Solution
Restarting Visual Studio
Temporarily deleting Form1 (it's still excluded from project)
Added a new form, per Neolisk's advice. It appeared in the available objects. I selected it and turned on application framework. From here, I copied the initialization code from Form2's designer code into Form3's. All was alright. Then, I copied Form2's main code into Form3. Now, "Form3 is a type in Solution and cannot be used as an expression" appears in my error list.
With that said, my question is how can I get Visual Studio to recognize my form as a form and set Form2 as the startup object?
add InitializeComponent() in new() function in form2
Found it! The issue was an overload of the form's constructor taking a form as a parameter. Since this was the only constructor in the file, I guess it caused confusion. Commented that out and all was right with the world again.
Sometimes you have a different class name from the form file name.
Check if the class name is listed.

execute other class before executing the main windows form application class

I've been given a windows form application written in VB. For some reasons I will need to execute the second class before the form application in the first class. The form class has to be the first class in the file. I can't simply inherit the second class and call the functions, because it has already used up the only allowable inheritance. I did some research and found there is something called main procedure that determines which codes executes first? It is automatically generated for any windows form application, but I simply can't find that file. Any thoughts on that? or any other ways that I do this?
Follow Start VB.NET GUI app using Sub Main or form startup object? for better alternatives.
But if you really need to start with Main(), follow these steps.
Open application settings.
Uncheck "Enable application framework"
Set startup object to "Sub Main"
Then add a new source file (.vb) and include Main() in it
Module MainModule
Sub Main()
'Your code here
End Sub
End Module

MyProject.MyClass - vb.NET custom controls

In a Visual Basic project, I created a homemade TabControl in order to fix a visual bug. The control works properly, however whenever I modify the form using my tab, Visual Studio adds MyProject in front of the control in its declaration:
Me.tabMenu = New MyProject.MyClass 'Gives a BC30002 compile error
If I remove the MyProject., the project compiles properly.
MyClass is in a separate file MyClass.vb and looks mostly like this:
Public Class MyClass
Inherits System.Windows.Forms.TabControl
Public Sub New()
InitializeComponent()
MyBase.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed
End Sub
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
//OnDrawItem code
End Sub
Private Sub My_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
//My_DrawItem code
End Sub
End Class
I tried removing the file and adding it again, copy and pasting the class inside MyForm.designer.vb, adding MyProject. to the class name, but nothing stopped Visual Studio from adding this so-hated MyProject.
Edit regarding this answer:
I understand the thing about the namespace, however my problem is mostly that the compiler does not recognize the class with the project name appended but still adds it everytime.
What is the actual compile error you are getting? Is it possable that the VB compiler is interpreting MyProject as something other than a namespace identifier? You could also try changing the default namespace for the project, then see what it does, it might give you a hint as to what the actual problem is.
You could also try changing the offending line to
Me.tabMenu = New Global.MyProject.MyClass
then let us know what the results are.
I've seen this before when you have a public module with the same name as your default namespace (project name). If that's the case, either rename the module or the default namespace and the problem should go away,.
By default, Visual Basic .NET assigned a default namespace to your project. (I believe the default is, in fact, MyProject.)
This is what's being prepended, and it's being done to explicitly identify your class in the designer.
No matter what your default namespace is for your project, the WinForms designer will add the namespace name to the .designer.vb file.
To change the default namespace, go to your project properties; it should appear on the first tab.
Also, generally, don't modify the .designer.vb files if you can avoid it. Those files get completely blown away and rebuilt by Visual Studio often, so your changes will more likely than not be eliminated.