Answered by:
Call a procedure thats in another form

Question
-
Hi friends,
I was wondering if I have a sub procedure that's in one form how do I call it from a different form. I don't know if the development environment requires some settings that I havent set but I have tried several how to's that none of them work. And in my case this procedure references controls like the report viewer which has added complexity to this problem. Any help would be greatly appreciated.
- Edited by Ronald Rex Wednesday, November 13, 2019 11:31 PM
Answers
-
The optimal approach IMO is to have the method in a classlib project that is referenced by both form classes in the form project, which would make the code sharable between two forms.
There is no reason the controls cannot be referenced and used by a class in the classlib project, and the method using the controls are accessible by a form class.
- Marked as answer by Ronald Rex Thursday, November 14, 2019 3:07 PM
-
Hi
If you have a Sub in (say) Form1 lets call it Proc1, and it is set as Public, then in Form2, you can call it by using Form1.Proc1. Proc1 may have parameters that could be derived from code in Form2 and passed as parameters in the call to Proc1.
Regards Les, Livingston, Scotland
- Marked as answer by Ronald Rex Thursday, November 14, 2019 3:07 PM
-
Here's something for VB WPF.
My.Windows.winNew.CallMe()
And the sub looks like
Public Sub CallMe() MessageBox.Show("Hello!") End Sub
Not really sure if that's the way you want to call the sub if the window was not already loaded... It would be something to learn if that would be destructed after the call. I know that if the window is already open it is referencing that window.https://awwshop.wikidot.com
- Marked as answer by Ronald Rex Thursday, November 14, 2019 3:07 PM
-
Hello,
VB.NET is fairly loose in regards to accessing controls, methods, procedures and event events between forms. Not knowing what you have tried makes it impossible to recommend what to do.
It's all about scope e.g. forms can see and change things when they are public and dependent on what you have friend scope is also viable. Having private variables and properties needs a design change, events inherently are private but dependent on what you are doing you can subscribe to an event in the child form from the parent form.
Here is a very simple example.
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
- Marked as answer by Ronald Rex Thursday, November 14, 2019 3:07 PM
All replies
-
The optimal approach IMO is to have the method in a classlib project that is referenced by both form classes in the form project, which would make the code sharable between two forms.
There is no reason the controls cannot be referenced and used by a class in the classlib project, and the method using the controls are accessible by a form class.
- Marked as answer by Ronald Rex Thursday, November 14, 2019 3:07 PM
-
Hi
If you have a Sub in (say) Form1 lets call it Proc1, and it is set as Public, then in Form2, you can call it by using Form1.Proc1. Proc1 may have parameters that could be derived from code in Form2 and passed as parameters in the call to Proc1.
Regards Les, Livingston, Scotland
- Marked as answer by Ronald Rex Thursday, November 14, 2019 3:07 PM
-
Here's something for VB WPF.
My.Windows.winNew.CallMe()
And the sub looks like
Public Sub CallMe() MessageBox.Show("Hello!") End Sub
Not really sure if that's the way you want to call the sub if the window was not already loaded... It would be something to learn if that would be destructed after the call. I know that if the window is already open it is referencing that window.https://awwshop.wikidot.com
- Marked as answer by Ronald Rex Thursday, November 14, 2019 3:07 PM
-
Hello,
VB.NET is fairly loose in regards to accessing controls, methods, procedures and event events between forms. Not knowing what you have tried makes it impossible to recommend what to do.
It's all about scope e.g. forms can see and change things when they are public and dependent on what you have friend scope is also viable. Having private variables and properties needs a design change, events inherently are private but dependent on what you are doing you can subscribe to an event in the child form from the parent form.
Here is a very simple example.
Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
NuGet BaseConnectionLibrary for database connections.
- Marked as answer by Ronald Rex Thursday, November 14, 2019 3:07 PM
-
Ronald,
The code from a form is in fact just a class as any other class. However, it is mostly started from an in the application framework included module main with application.Run(Form1)
That makes the first form special. As well does the designer included all code from controls in that class and that is not where it is used when you call them from form1
That makes that controls are on the Form1 object using the application framework.
However, if it is a simple procedure you can call that without any problem as long as the method is stated friend or public
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load Dim F2 = New Form2 Label1.Text = Form2.GiveName End Sub End Class Public Class Form2 Friend Function GiveName() As String Return "Ronald Rex" End Function End Class
Success
Cor -
Thanks for your help. For some strange reason when I type the form that contains the procedure it is not listed in the intellisense. This is behavior that I have never seen before. I don't know if I have a setting off in my development environment or what. Thanks !