Disable default instance of forms in VB.NET - vb.net

Following this answer. Can I disable automatic instantiation? I want VS to show the error:
Reference to a non-shared member requires an object reference

Update:
When I originally wrote this answer, it was based on empirical evidence from the .vbproj file and the VB IDE and I had no reference material. Since then I have come across the article "Simplify Common Tasks by Customizing the My Namespace" published in the July 2005 MSDN Magazine (online version; full magazine edition download
This article shows to control the generation of the various My Namespace features via compilation constants. In particular it shows how to overide the MyType tag stored in the .vbproj file.
This is accomplished by setting the "Custom Constants" accessible by going to Project Properties->Compile Tab and clicking on the "Advanced Compile Options" button.
To override then MyType tag, set the custom constant _MyType.
If you want to include specific My Namespace items, you can set the pertinent constant(s) shown in the following table from the above referenced article.
The default form instances are part of the "My.Forms" member. Note that in order to prevent "My.Forms"
from being generated, set _MyType to either "Empty" or "Custom". You do not need to explicitly include _MYFORMS="False".
To use the additional constants defined in Table 5, set _MyType="Custom".
To include all the standard My Namespace items except for My.Forms, use the following constants:
_MyType="Custom", _MYAPPLICATIONTYPE="Windows", _MYCOMPUTERTYPE="Windows", _MYUSERTYPE="Windows", _MYWEBSERVICES="True"
End Update
The features enabled in Visual Studio are initialized based on the initial project template you select when creating a project. Normally, one would select the "Windows Forms App(.Net Framework) Visual Basic" template as shown below.
(Note: All images are based on VS 2017 Version 15.8.7)
This will load up a VB project environment that includes a lot of automatic code generation that supports the VB Application Framework including the "Default Form Instance" feature via additions to the My Namespace (see: My.Internals: Examining the Visual Basic My Feature. Now you could try to disable this framework via the Project Properties->Appliction Tab->Enable Application Framework checkbox, but all that does is require you to provide you own "Sub Main" implementation.
To create a project that free of all the My Namespace code generation including the the default form instances, you need to start with the "Empty Project (.Net Framework)" template.
This will create a bare-bones VB project. If you add a WinForm (Project Menu->Add Window Form) and go to the code-view and add the following:
Class Demo_NoDefaultForms
Sub ErrorOnForm1Reference()
Form1.Text = "ain't gonna work"
End Sub
End Class
You will see that the default Form1 instance is not supported.
Furthermore, all the "Application Framework" stuff in the project properties is disabled.
The project type My Namespace information is like all project information is stored in the projName.vbproj file and is stored in the <MyType> tag. Although I have never found any official documentation on this tag, the following summarizes my observations from creating various project types.
MyType Tag Value Project Type
----------------------------- ------------------------------------------------
WindowsForms Normal Windows Form App w/ Application Framework enabled
WindowsFormsWithCustomSubMain Normal Windows Form App w/ Application Framework disabled
Console Console App w/ Application Framework enabled
Custom WPF Application
Empty No My Namespace additions
Starting with the "Empty Project (.Net Framework)" template would be tedious as you would need to perform a lot of boiler-plate setup and declare a "Sub Main" each time. I recommend that you create a base project with all your own customizations and then export the project a new template (see: How to: Create project templates.

Related

How to set the startup project for an asp.net core template?

I am currently creating a multi-project template, I want to distribute using a .VSIX-extension for Visual Studio. When the extension is installed the user can select my template among the others provided by Microsoft in the "Create a new project" window.
My template consists of three project:
ProjectName.Server
ProjectName.Contract
ProjectName.Client
ProjectName is the user-given name while the suffixes "Server", "Contract" and "Client" are fixed. The issue I experience is that when the users create a new project the Client-project is always selected as a startup-project by default. I want the server-project to be selected as a startup-project by visual studio. It looks like following:
The client project is not the correct startup project, but users don't know that the server project it the correct startup project, so I want to take off the selection and integrate it right into the template, so the correct startup template is selected by VS when creating the project.
I already removed the suffix "Server" from this project, so it is shown as the first project in the solution explorer and I hoped VS would then select it as the startup-project. This did not work unfortunately.
Can anyone give me a hint on how to configure the startup-project in the template?
If you have any questions don't hesitate to comment.
These configurations are stored in the Solution User Options (.suo) file.
You can refer to my steps below to try:
First, Close Visual Studio and find the folder where your solution is.
Second, Open the .sln file with a text editor, you see all your projects encapsulated in Project – EndProject lines:
Third, Cut and paste the desired default startup items into the first.
Fourth, Delete your .suo file(hidden files: Solution Folder/.vs/Solution Name Folder/v17/.suo, I use VS2022).
Finally, Open your solution in Visual Studio.
You can have a try.
You will likely need to associate a custom IWizard assembly with your project template, and then explicitly set the startup project by setting the SolutionBuild.StartupProjects to the desired project in your solution.
Once upon a time, I did this using code similar to the following:
VSProject startupProj = FindVSProject(startupProjName);
sln.SolutionBuild.StartupProjects = startupProj.Project.UniqueName;
where FindVSProject was implemented as:
private VSProject FindVSProject(string projName)
{
foreach (Project p in _dte.Solution.Projects)
if (p.Name.Equals(projName, StringComparison.CurrentCultureIgnoreCase))
return p.Object as VSProject;
return null;
}

Compile UserControl and re-use in another project by referencing compiled .dll

I have created a custom UserControl in a vb.net Class Library project, and I would like to Build this library and be able to reference the .dll file from other projects in order to re-use the custom UserControl.
When I Build the Class Library project and add its .dll to a Windows Form Application project as a reference, I do not see my custom UserControl in the ToolBox. I have a feeling it has something to do with the fact that the code I've written for the UserControl is separate from the code the designer automatically creates when I design the control in design mode.
I've been searching the web for a few days no to no avail. I see similar answers for ASP.net UserControls, but have been unable to transfer those answers over to the vb.net platform.
Any help is greatly appreciated.
• Right-click in the toolbox.
• Click "Choose Items..."
• Click "Browse..."
• Navigate to your DLL and click Open
• Then click "Ok"
Your components should then show up

Debugging with my own custom Class Libraries

I want my class library conditionally compiled so that it is in debugging mode when my project is, and is not when my project isn't.
For example, I have this Module in my class library:
Module MyDebug
<Conditional("DEBUG")>
Sub print(ByVal msg As String)
Debug.Print(Now.ToString("yyyy-MM-dd HH:mm:ss.fff") & " " & msg)
End Sub
<Conditional("DEBUG")>
Sub debugEnd(Byval bool As Boolean)
Environment.Exit(0)
End Sub
End Module
When I debug my project which references this library it run any of these when they are called.
I've tried searching online, but I haven't found anything that helps since I can only find things to do with debugging the actually class library, while I only want it to send these conditionally compiled statements while I'm debugging my project.
However, by experimenting around a bit, I've found that if in the class library I go to:
'My Project' -> the 'Compile' tab -> 'Advanced Compile Options'
and then tick 'Define DEBUG constant' (and then build the library), the project does run the debug statements when called.
However, I'm not entirely sure of the behaviour of 'Define DEBUG constant' in the class library. Does it define DEBUG if and only if my project is in debug mode?
If not, then is there a simple way to acheive what I aim to do? (I don't want to have to tick/untick the checkbox in the class library each time I switch between debug and release in my project, and my class library is referenced by more than one project, anyway)
Note that in the project I'm referencing the .dll in the Bin -> Release folder of my class library which I hope is the right way to reference it.
In addition, I would like to ask about how VB acheives this with the Debug class, because it is also imported with a reference like any other class library, and works in the way I would like the above to — surely I could do the same?
If you keep the class library project in the same solution as your main project it will use the same configuration (Debug or Release) as every other project in that solution. This means that you won't have to manually check/uncheck Define DEBUG constant as it will not be defined anyway if you have set the configuration to Release.
Even if your projects are not in the same folder you may still add your class library project to your solution. Here's how to do it:
Right-click your solution in the Solution Explorer and goto Add > Existing Project.
In the file browsing dialog that opens, locate the .vbproj-file of your class library and click OK.
Now when you change compilation configuration it should be reflected over the entire solution and your class library shall only have its DEBUG constant defined if you set the configuration to Debug.
If you cannot see your solution in the Solution Explorer:
In Visual Studio, goto Tools > Options > Projects and Solutions > General.
Check Always show solution and press OK.
For the above to work you must also change the way you reference your class library. The way you are currently doing it is correct, but it won't work in this case as then you'll only be referencing the Release version of your dll.
Start by removing your current reference to your class library.
Open the Add Reference dialog and go to the Projects tab.
Select your class library's project and press OK.
This should now reference the class library's output from the solution's current compilation configuration.

Xamarin Forms InitializeComponent does not exist

I am learning Xamarin Forms for Visual Studio 2015 and trying to create a simple Shared project using Xaml but I keep getting InitializeComponent does not exist (as well as any reference to Xaml controls in cs classes).
It is my understanding that in the latest versions of Xamarin, Xaml works in Shared Xamarin Forms Projects (and not only in PCL projects). I've tried running the latest betas that got released less than a month ago but still no luck.
Any advice is greatly appreciated.
Turns out I had to manually change the properties for all Xaml files like so:
Build Action: Embedded resource
Custom Tool: MSBuild:UpdateDesignTimeXaml
For some reason adding new Xaml forms files doesn't set these by default in VS2015 with latest Xamarin.
I had the same issue. And it turned out to be that fully qualified Class Name (x:Class attribute in the main node) in the XAML file must match exactly the xaml.cs file's name. I corrected the namespace casing and it worked!!
This happens when your XAML markup is invalid. There is a background compilation task that parses the XAML file and generates a code file (this is normally hidden from the developer) which declares those control references. If your XAML is not valid then this task will fail silently, and your first hints are the symptoms you describe.
Comment out the contents of your XAML file using standard XML comment syntax (wrap it with <!-- and -->), then rebuild your project to clear out all of the errors. You may also need to comment out some code in your .cs files temporarily to get it to build. Once you get it building, then you can go back and start uncommenting your XAML until you find the part that was breaking it.
Try it:
Open the Package Manager Console, put the follow line:
update-package -project your_pcl_project_name -reinstall

Generate manifest for regfree COM from a VB project

I am trying to generate a manifest for a set of COM objects that are being used in our code as registration-free COM.
For that purpose I created a VB project in VS 2010 and added the COM objects DLLs as dependencies. The problem is I do not see them listed as dependencies in the generated manifest. Am I missing something simple like a project setting to generate the proper manifest listing the dependencies?
You left out too many details and the VB.NET IDE hides too much to do this right. It is not automatic.
Starting point is to force it to show more. Locate the "Show All Files" toolbar button in the Solution Explorer window (use the tooltip to find it back). Click it and you now get the "References" node added to the project.
Open it and locate the COM component that you added to the project. Select it and look at the Properties window. Set the "Isolated" property to True. Rebuild the project. Be sure to look in the Output window for any warnings. And to look at the .manifest file on disk instead of the one the IDE shows you if you added a manifest to your project yourself.