MyProject.MyClass - vb.NET custom controls - vb.net

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.

Related

Only Form1 in Startup Form in VS2022

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

The class MyApplication can be designed, but is not the first class in the file [duplicate]

I'm a newbie on vb.net, and I was in progress with my first application... and found some example code in the msdn so I just replaced my Form1.vb file content with the content from the MSDN. When I roll back the changes, and tried to compile my old code then hundreds of errors appeared, and when I switch to the Form1[Design] tab I see this:
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.
I'm really new on vb.net and the visual studio itself, and I dont know what to do in this case, is my work destroyed or what?
That's because you added some class or other code above the class definition in form1.vb. Remove that.
What worked for me is editing both Form1.vb and Form1.Designer.vb and placing at the beginning of both files: Namespace Whatever and at the end of both files: End Namespace. The "Whatever" can be any name not already used in the program (or the name of an existing Namespace that you're already using).
You added another class in your form and that is the reason of the error. I had ran into same issue. I had added another class in the form and that caused this error. To resolve, I moved the new class to a module(created new module) and then access the class in the required form.

stimulsoft report add my function with vb.net

we can add our function with this way
https://www.youtube.com/watch?v=TEYuQmia9IY
but it in c#
i want the way in vb.net
the problem in using namspacing in vb.net
thanks
In the video posted there are some critical steps missing. It seems that they are given for granted but without it you could not achieve a successful reference to your code from the Report Designer.
First: To clarify the problem between VB.NET and C#. If you want to use VB as scripting language for your report you need to set it in the Report Properties. You will find the ScriptLanguage property in the same property grid for the report where they set the reference to your application. Changing this property is not required because this property refers to the code written inside the report not the code in which you have written your app.
Second: In your VB.NET app your code that defines the function should be as this
Namespace MyFunc
Public Class Main
Public Shared Sub ShowMessage(text As String)
MessageBox.Show(text)
End Sub
End Class
End Namespace
Usually in VB.NET you don't define explicitily a namespace (everything is inside the main namespace defined in the project properties) but if you want a separate namespace there is nothing to stop you.
Third: In the Report Code Page you write
Imports WindowsFormApplication2.MyFunc
Fourth: After completing the design of your report remember to save it in the same folder where the application is created. (Usually Project\BIN\DEBUG) Otherwise the reference to your executable will fail.
Fifth: The Stimulsoft assemblies are installed in the GAC, but to be sure that they are found when you run the report don't forget to set their references to Copy Local = True when you run your app inside Visual Studio

How to create nested user controls in VB.net?

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.

Why does Console namespace need to be imported into a Console application project

If I create a new Console application then this compiles ok:
Module Module1
Sub Main()
Console.ReadKey()
End Sub
End Module
An alternative is this:
Imports System.Console
Module Module1
Sub Main()
ReadKey()
End Sub
End Module
What I don't now understand is, as this is a console application, why aren't these projects created in such a way that the following will compile?:
Module Module1
Sub Main()
ReadKey()
End Sub
End Module
Put another way: if it is a Console app why do I need to import the Console namespace?
Is this the same behavior as when using a winForms application I say Me.someControl? Although in that situation I can leave of the Me and it will still compile without importing a forms namespace.
EDIT
Imports System.Console
Module Module1
Sub Main()
ReadKey()
Console.ReadKey() '<<adding namespace seems a bit pointless as I still need to specify Console
End Sub
Function ReadKey() As Boolean
Return True
End Function
End Module
FURTHER EDIT
Hans has suggested further behaviour and to replicate this I have the following:
I've added a new ClassLibrary1 to the same Solution like so...
In the Console ConsoleSandpit I've got the following, with a reference to ClassLibrary1 like so...
...only problem is I'm expecting a compilation error from the above but it seems to be running ok!
System.Console is not a namespace. ReadKey is a shared method in the Console class which is in the System namespace
So, the breakdown of the following statement is:
System.Console.ReadKey()
System - Namespace
Console - Class
ReadKey - Shared method
So, when you call Console.ReadKey() (without specifying the namespace), you would normally expect to have to import System at the top of your code file. However, that is unnecessary because in VB.NET projects, you can specify a list of default namespaces that are automatically imported for all files in the project. You can modify these using the check-list of default namespaces in your project properties designer. System, among others, is automatically imported as a default namespace with all of the project templates.
As you demonstrated in your question, it is possible in VB.NET to import a class or module name. The Imports statement is not limited to just working with namespaces. However, as far as I know, there is no way to import a class name with the default namespaces option for the project (or at least not with the project properties designer, anyway).
If System.Console was a namespace, I agree that you'd expect the project template for console projects to import that namespace by default. However, since it's not a namespace at all, you wouldn't expect it to do that :)
1) You can do plenty of useful things, over many functions, inside a console application, without wanting or needing to actually interact with the Console.
2) I could easily envisage wanting to write a function called ReadKey which puts out a small prompt for one of two or three keys to be pressed - based on the user input, if its one of those keys, return it. Otherwise, indicate the error and re-prompt. Why should I have to pick a different name for this function, because my own namespace has been automatically polluted with the methods from Console?
Namespaces exist to help to separate and organize functionality. You're given options (such as the Imports statement) to hide these separations, where it makes sense for you within your application - but I wouldn't want this forced on all users.
Add a class library project to your solution and paste this code:
Module FooBar
Sub ReadKey(ByVal wait As Integer)
End Sub
End Module
And observe how your original code now suddenly fails to compile. With a seriously obtuse error message as well: "Argument not specified for parameter 'wait'". You can lose hours of your life trying to figure out why Console.ReadKey() wants a wait argument.