Control which form will load first in vb.net by coding - vb.net

Is there a way to control which form will load first when the program starts in vb.net programmatically? Like a main method or something like that?

Yes, this is possible.
Add a public Main somewhere. (For example in a new class)
Public Shared Sub Main()
'Create and show your form here
End Sub
Then in the application settings, disable "Enable application framework" and select the Main method as the startup object.

Related

How to implement a MVC type (global) controller within a VB.Net (windows form) application?

I am used to creating (Excel) VBA user form programs in a MVC style, and have recently been looking at VB.Net a bit more.
In general, I would create a MVC style user form application in VBA by creating an 'Initialize' function which creates an instance of a 'Controller' module and utilizes a 'LaunchProgram' method contained within this controller. This method would create an instance of the model and view used by the application, and then present the view to the user.
For example,
Public Sub Launch()
With New Controller
.Present
End With
Debug.Print "Execution ended"
End Sub
and then in the Controller (class) module I would have code such as
Private WithEvents m_View As View
Private m_Model As Model
Public Sub Present()
Set m_View = New View
Set m_Model = New Model
m_View.Show vbModal
End Sub
My issue with VB.Net is that there is no obvious way to create an equivalent of the 'Launcher' sub, which controls the execution of the program, and instead just presents a default instance of the main form to the user upon running the program. I have attempted to replicate the desired architecture by creating a main form which is (supposed to be) immediately hidden upon starting the program. This hidden form (should) then create specific instances of all other forms, controlling how user input is processed and returned in accordance with the general MVC methodology.
However, this doesn't work since the VB.Net code...
Public mSetupForm As SetupForm
Public mOutputForm As OutputForm
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Debug.Print("Program initialized")
mSetupForm = New SetupForm
mOutputForm = New OutputForm
Me.Hide()
mSetupForm.Show()
End Sub
...does not hide Form1.
By default, VB.NET applications use the mysterious Windows Application Framework which means that the build process adds an entry point for you which automatically runs whichever form you use as the main form. You can access these settings via Project Properties > Application > Windows application framework properties.
If you don't want to use the framework, un-check the Enable application framework check box and then select that you want to use Sub Main as your Startup object. Then you can create a module with a Public Sub Main method which will be the entry point to the application.
In the main, if you want to show a form, just call Application.Run and pass it the instance of the form you want to show (you can call it multiple times in succession if you want to).

application open on startup in background

Apologies if this question has already been asked.
I currently have it in the startup folder, but I'm not sure what code to use to not open the parent form.
I want my vb.net application to open on startup but in the background so it doesn't annoy users when they log in.
How would i go about setting that up?
To prevent a form from showing itself (but still create itself) you can override the following code in the form.
Protected Overrides Sub SetVisibleCore(value As Boolean)
MyBase.SetVisibleCore(False)
End Sub
This will always hide your form. Obviously pass true to make it display on whatever criteria you'd like to use.
This approach doesn't require you to restructure your application to separate ui + logic.
Try to move all the form init code to a sub Main function in a new module a set that function as a start up function, after the init code add:
dim frm as new <your_form>
after when you want to display it only call frm.show or frm.showdialog

VS2010 VB.NET Winforms select startup form programmatically

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.

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

How to export form as an image (not during runtime)

I'm using Visual Studio just to create GUIs for a project. There is no code, only the designs.
I need to convert these forms into images that I can paste into the report.
Is there a simple way to do this?
Thanks.
Edit: the only solution I have so far is to edit the project so that each window I want to export is the start up form, but I am going to have over 40 forms, so this will get tedious after awhile.
Create a new Class in your Projects, lets say Startup.vb, which looks something like this:
Friend Class Startup
<STAThread()> _
Public Shared Sub Main()
Application.Run(New Form1)
Application.Run(New Form2)
Application.Run(New Form3)
End Sub
End Class
In the Project Properties the general Tab, untick Enable application framework and choose Startup as new start-object.
Obviously you'd need to adjust your startup class to open all forms you wish to open. When running the application just press Alt+PrintScr to copy an image of just the selected Window/Form to the clipboard. Paste that back into the paint application of your choice. Microsoft Paint will do.
Using a screen capture program might be your best choice. Personally I recommend TechSmith SnagIt application. You can capture any selected region in the screen using this application.
If you right click on the form a select Lock Controls this will remove the resize handles you can then press PrtScn to take a copy.
Note: After applying Lock Controls it does still have a focus rectangle and lock symbol but these are outside the co-ordinates of the form so they could be cropped in MS Paint or similar?