accessing a form control from a module [VB.NET] - vb.net

Can I reach/access a form control object from a module in VB.NET ? I have a webbrowser in the Form , so my question is can i access it from a module (changing settings for example). I have no idea how to do this .

The control has to come in as a parameter. Since the Module behaves as a static (shared) class, it has no state or access to instance members natively.
Module MyModule
Public Shared Sub MyMethod(ByVal myControl as WebUserControl)
' do your mojo
End Sub
End Module
If you just want to have global access from the page level you can send the Page in, but you'll need to perform a recursive search for various controls and you won't have access to the rendered output, so databound controls such as GridViews or FormViews will not work the same way and will be subject to the event lifecycle.
Public Shared Sub MyMethod(ByVal myPage as Page)
End Sub

Related

Call form module function from form event

I have a tab control object on my form. I want a function from within my form's vba module to be called when the tab control's on change event occurs.
I have a form with a module enabled.
I have a public function in the form module named "foo()"
I have a macro "bar" that is:
Runcode: "foo()"
I get an error that Access cannot find "foo()" despite being public. If I move "foo()" to a regular module then the macro can find it. Is there a way to use a macro to call functions from a form module since it's convenient because of the "me" keyword.
https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/runcode-macro-action
"However, when this action runs in response to clicking a menu command on a form or report or in response to an event on a form or report, Access first looks for the function in the form's or report's class module and then in the standard modules."
It appears the solution is to navigate to the event box, click the "..." box, and then click "code builder."
This will create a private sub in your form module which you can then use to call your function.
Private Sub [tab control name]_Change()
Call foo()
End Sub

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).

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

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.

Visual basic. Change default startup form in code, depending on value of variable

I have been working on a Visual Basic project in Visual Studio and have encountered a problem.
I understand that the Startup form property in the Application page of the Project Designer can be changed to a default form, however what I require is a way to do this through code in ApplicationEvents.vb depending on the value of a variable within application settings.
The goal is that if a user completes a form then a value is assigned to a variable, e.g. variable username = "xxx". If this value is true, then the default startup is a login form (as the user has already registered), and if it is false then the user is taken to a register form.
I appreciate that I could use another form to determine this, however this seems like I would be squandering the capabilities of ApplicationEvents and not using it correctly (I also want to avoid the inevitable flicker of a blank form as it decides).
I know that the default form is stored in Application.myapp, however with the final publication of the .exe this file will (presumably) not be exported with it, so I want to avoid writing directly to it. I have also read into the windowsformsapplicationbase.mainform property, however cannot figure out how to use it?
Here is a example piece of code from ApplicationEvents.vb to demonstrate my question.
If String.IsNullOrEmpty(My.Settings.username) Then
MsgBox("You have not registered")
'set register as default form
Else
MsgBox("You have registered")
'set login as default form
End If
Usually, if you need that much control over what happens at start-up, you just want to disable the a application framework. To do so, just un-check the Enable application framework check-box in the Application tab of the My Project settings designer window. Once you un-check that, you will be able to change the Startup object to Sub Main. Then you can add a new module with a Main method, like this:
Module Module1
Public Sub Main()
Application.EnableVisualStyles()
If String.IsNullOrEmpty(My.Settings.username) Then
Application.Run(New RegisterForm())
Else
Application.Run(New LoginForm())
End If
End Sub
End Module
Be aware, however--by disabling the application framework, you will loose the other automatic functionality that it provides, such as ApplicationEvents. If you want to use the application framework, you can accomplish the same thing by simply setting the MyApplication.MainForm property in the MyApplication.Startup event:
Partial Friend Class MyApplication
Private Sub MyApplication_Startup(sender As Object, e As ApplicationServices.StartupEventArgs) Handles Me.Startup
If String.IsNullOrEmpty(My.Settings.username) Then
Me.MainForm = New RegisterForm()
Else
Me.MainForm = New LoginForm()
End If
End Sub
End Class
Alternatively, you could always show the same form, but then have the form contain nothing but a single UserControl. Then you can simply switch which UserControl is displayed depending upon the settings. The user-controls would need to include all of the controls that would have otherwise been placed on the two different forms.

Allowing all classes within a project access to a form

I am currently designing a project with a windows form. I am also creating classes that I want to interact with this form. Instead of passing through the form to each object I create, is there a way to allow any class to interact with the form. This is mainly for debugging purposes, I only want access to the textboxes. At the moment I have to do this:
Dim x as new MyClass(Me)
where the me refers to the form I instantiate MyClass in. This means I need to assign a variable the form in the Sub New of the MyClass (creating a new instance of the form is not helpful).
Within the class I access other classes. If there was a way to interact with my form without the need to constantly pass it forward, it would be great.
Thanks.
Create a Public Module with a Public variable declaration of your Form and assign it.
Public Module FormInstance
Public Property MyForm as Form1
End Module
'Somewhere on you Form Startup
FormInstance.MyForm = me