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.
Related
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.
My vb.net program was functioning properly. After I renamed Form1 in the code pane of Visual Studio 2015 the Program.vb[Design] pane will not open and the following message appears:
The class Form1 can be designed, but is not the first class in the file. Visual Studio requires that designers use the first class in the file. Move the class code so that it is the first class in the file and try loading the designer again.
The name change was in the first class (form) so I cannot move it further up.
Before the change there were no errors. After the change there are many handles clause, variable not declared and not a member of errors.
I had 2 choices for Startup form in the Application tab of My Project before the change. After I have only 1 choice from a dropdown list. Both choices were Class names.
Is there anything I can do to recover the Designer view? Should I just recreate the form?
Thanks in advance, for any suggestions.
I have the following classes in my vb.net application:
Form1
Usercontrol1
LnkLabel
Usercontrol1 is a user control , and doesnt contain any extra code. LnkLabel is a class that inherits Forms.Label. Its code is goven below:
Public class LnkLabel
Inherits Label
Sub clk handles me.click
Process.start(text)
End sub
End class
When I add an Instance of LnkLabel to usercontrol1, i get an error "type LnkLabel is not defined"
There are three instances of the error in uc1.designer.vb.How can I solve these Errors?
Note:
Visual Studio 2010
.Net FW 3.5
Edit:
The usercontrol1 donot contain any code that might be causing the error. It is just a new usercontrol added to the project.
LnkLabel is added to UC1 by the designer, not by using code at runtime.
The class name is LnkLabel, and not "LinkLabel".
I find that the easiest way to resolve this type of issue is to open the Designer.vb file directly.
To do this, choose Show All Files from the Project menu, then expand UserControl2. Double-click on the UserControl2.Designer.vb file.
You should also be able to get there by double-clicking on the error in the compile errors list.
Once there, search for the definition of UserControl1 or uc1, whatever it may be called (ensure you are in the type definition area, not the property assignment area).
Looking at the definition may give you an instant clue as to the problem (is it in the wrong namespace; did the name of the user control change after you created it, but the change was not propagated to this form; etc).
If it is not obvious what the issue is, use VS intellisense to help you get the right class. I usually clear the previous type definition and start typing the name I know it should be (i.e. UserControl), then select the appropriate value from Intellisense.
Selecting a different class (or correcting the class selection) will require a change to the control instatiation code and may also require a change to some of the properties (I usually just remove the properties I am unsure of and update the control directly in the designer).
Before you switch back to the designer, ensure that you save your changes and, if possible, compile the app.
I'm really struggling to find out how to dynamically load a form when starting a VB.NET winforms application in VS2010.
Looking at existing answers such as this: Programmatically change the startup form on application launch?
Has not helped. I do not have a main method (that I can see) in my winforms project and when I go into the project properties I can only select a start-up form. But I have one of two forms to display on start-up depending on the user accessing the application.
I tried to set a loading form up which, in it's load event would call .Show() on the correct loading form after it had determined it and then the loading form would close itself down, but doing this led to both forms being closed.
Below are steps for VS2010 VB.NET Winforms select startup form programmatically.
1 : Go to My project from Solution Explorer
2 : Click on Application Tab--->Uncheck Enable application Framework
3 : Then Inside module create Sub like this
Public Sub Main()
MsgBox("called Main") 'This is testing
Login.Show() 'Set your start up form here
End Sub
4 : Again My Project--->Application Tab--->Startup Object--->Sub Main
5 : Thats it, It will give you message box and will show Login form.
Hope It will help you.
Thanks
Mahesh
Nevermind. I found in the properties a button to generate the MyApplication class in which I can access the startup event.
Another option is to use an MDI form. When it loads you can determine which child form to display.
Using Sub Main is the way I have done this forever but for some reason, MS has decided to make the norm, difficult. To use the Sub Main way, create a "Module" if you dont already have one. Put this code in there:
Sub Main()
Stop
End Sub
Now, in your project properties, assuming your are doing a standard WinForms application, on the "Application" tab, uncheck the "Enable Application Framework". This will allow you to see (and select) "Sub Main" in the "Startup Object" drop-down.
I have a very small VS2008 Winforms project that will not start.
When I attempt to start debugging the project, I get the message:
'<form>' is a type in '<project>' and cannot be used in an expression.
From the file .Designer.vb.
The problem is that is indeed a form. If I create a new WinForm and set the startup object to the new form, I get the same message.
When I attempt to check the "Enable application framework" checkbox in the Project properties, I get the message "Startup object must be a form when 'Enable application framework' is checked.
I've tried creating a new project and moving all the code and designer objects to a new form file in the new project, and same result.
Other projects on the same computer run fine.
Any suggestions?
Thanks!
Turns out that the problem was that I didn't have a New() function with no parameters. This is required for VS to see the class as a form.