Lifetime of class instance passed from one form to another - vb.net

My project uses several forms to collect information. Since child forms cannot access variables from the method in which they were instantiated, I decided to create a custom Class that can hold gathered information from the child forms in order to use that information in the parent form. However, I am unsure of when the instance of my custom class will be disposed.
Form1 collects information and uses that to determine which child form to launch next.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim customClass As New CustomClass
Dim form2 As New Form2(customClass)
form2.Show()
Dim form3 As New Form3(customClass)
form3.Show()
End Sub
End Class
Form2 collects data and assigns values to shared variables in CustomClass, as well as making several function calls.
Public Class Form2
Private _CustomClass As CustomClass
Public Sub New(ByRef instance As CustomClass)
InitializeComponent()
_CustomClass = instance
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
_CustomClass.MySharedVariable = TextBox1.Text
_CustomClass.MyCustomMethod()
Me.Close()
Me.Dispose()
End Sub
End Class
When is customClass destroyed? Is it disposed after End Sub from its parent method, or is it disposed after all the forms using that instance have closed?

Related

VB.NET Set text value of a textbos of another form

what i need is when form2 is opened from form1 cliking on a button then on form2 i click on anothe butto and i set the value of a textbox of form1.
if i set the type of application as windows form application it is all ok but if i set as class library i have the error reference to a non-shared member requires an object reference.
if i reference to Dim frm = New form2 it open a second form2 and i dont want it.
how can resolve this?
thank you.
here is the code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.ShowDialog()
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.TextBox1.Text = "aaaa"
Me.Close()
End Sub
End Class
When you build vb.net winforms project, some extra code is generated in the project. For example static instance of the Form class, so you can access instance methods through the class name Form1.ShowDialog().
This were done for VB6 programmers to make shifting from VB to VB.NET easier.
When you change project to be a library project, this code not generated anymore and Form1 is just a class and you can not access instance methods directly, but need to instantiate an instance "manually".
Instead of using this "hidden" shared instance, create form instances manually. You can pass instance of form1 to the constructor of form2 and update form1 from there.
Because you are using ShowDialog, I would suggest to make Form2 not depend on the Form1 and instead of updating Form1 textbox directly(you would like to avoid making Form controls public), return value as result of the dialog.
Public Class Form2
Public Property ResultValue As String
Private Sub Button1_Click(s As Object, e As EventArgs) Handles Button1.Click
ResultValue = "Value from Form 2";
DialogResult = DialogResult.OK; ' This suppose to close the form
End Sub
End Class
Public Class Form1
Private Sub Button1_Click(s As Object, e As EventArgs) Handles Button1.Click
Using form As New Form2()
Dim result As DialogResult = form.ShowDialog()
If result = DialogResult.OK Then
TextBox1.Text = form.ResultValue
End If
End Using
End Sub
End Class
Add the following to form1
Public static sub changeTitle(myTextBox as Object,title as string)
myTextBox.Text=title
End sub
Call above function in form2
Form1.changeTitle(Form1.TextBox1, "new title")

Share an event between 2 forms usign a common class

I would like to share an event to another form via a common class with a public shared event.
My code is setup like this:
Form1 is the main form where the button.click event is generated
Form2 is a subform displayed in Form1 in a panel control, where if the event is generated executes a sub.
To share data I use a commonData class, to share the only data i need, because I want to keep private function and variables in the single forms.
Can someone help me figure out what I'd like to do?
something like this, but working
Public Class commonData
Public Shared Event event1()
End class
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles button1.Click
RaiseEvent commonData.event1()
End Sub
End class
Public Class Form2
Private Sub eventFired(sender As Object, e As EventArgs) Handles commonData.event1
MsgBox("event")
End Sub
End class
There is many techniques to do that but this one (which is one of those) I hope might help you to have an idea how can have a shared Event between Forms or other classes.
First as you want here is the common module (you can use a class instead, is your choice; but <Extension()> go only in modules).
CommonData as a Class and ExtensionUtils as a Module for extensions:
Imports System.Runtime.CompilerServices
Public Class CommonData
Public Shared Event MyGlobalEvent(eventSender As Object, otherParams As String)
Shared Sub RaiseMyGlobalEvent(eventSender As Object, otherParams As String)
RaiseEvent MyGlobalEvent(eventSender, otherParams)
End Sub
End Class
Module ExtensionUtils
<Extension()>
Public Sub ButtonClick(ByVal buttonCaller As Button, eventArgs As String)
CommonData.RaiseMyGlobalEvent(buttonCaller, eventArgs)
End Sub
End Module
And here the implementation:
In this example I use a button named “Button1” in Form2 and when I click on it show a msgbox in Form1.
Form1:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form2.Show()
AddHandler CommonData.MyGlobalEvent, Sub(objSender As Object, message As String)
MsgBox(message & vbCrLf & " But I'm telling you that from form " & Me.Name)
End Sub
End Sub
End Class
Form2:
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Button1.Click, Sub()
Button1.ButtonClick(("I'm clicked from form " & Me.Name))
End Sub
End Sub
End Class

Open a form from another form and get a value and then pass it back to the first form [duplicate]

This question already has answers here:
VB.Net Passing values to another form
(2 answers)
Closed 5 years ago.
I have 2 forms with text boxes and buttons and I want the first form to open the second form. I then proceed to type a value in the text box in the second form so that when I click the button on the second form it must close and pass the value of the text box to the first form text box.
I have been trying to do this with no luck so far...
I am currently using vb.net in visual studio 2015
Here is my code:
Public Class Form1
WithEvents fr2 As New Form2
Private Sub btngetvalue_Click(sender As Object, e As EventArgs) Handles btngetvalue.Click
fr2.Show()
End Sub
Private Sub fr2_passvalue() Handles fr2.passvalue
Me.txtform1.Text = fr2.txtform2.Text
End Sub
End Class
Public Class Form2
Event passvalue()
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub btnpassvalue_Click(sender As Object, e As EventArgs) Handles btnpassvalue.Click
RaiseEvent passvalue()
Me.Close()
End Sub
End Class
I have tried accessing the controls directly but it does not work as vb.net now uses classes for everything. I cannot seem to figure out how to get around this.
Redefine your event to pass a string. Then use the Form2 event to pass the value to the handler.
Public Class Form2
Public Event passvalue(text As String)
Private Sub btnpassvalue_Click(sender As Object, e As EventArgs) Handles btnpassvalue.Click
RaiseEvent passvalue(txtform2.Text)
Me.Close()
End Sub
End Class
Handle it in Form1, updating Form1's TextBox.
Public Class Form1
WithEvents fr2 As Form2
Private Sub btngetvalue_Click(sender As Object, e As EventArgs) Handles btngetvalue.Click
fr2 = New Form2()
fr2.Show()
End Sub
Private Sub fr2_passvalue(text As String) Handles fr2.passvalue
Me.txtform1.Text = text
End Sub
End Class
As you had it before, Form1 was accessing controls on Form2 directly, which defeats the purpose of the event.
You could simply define a global variable (Public), which would be accessible from both forms.

How to pass an integer value to other forms? [duplicate]

This question already has answers here:
Passing variables between windows forms in VS 2010
(3 answers)
How to pass value from Form1 to Form2
(2 answers)
Closed 5 years ago.
Is it possible to use a label? I'm planning to display a scoring system, like every correct answer the score will add 10pts.
Use an instance of a class within a form. The Form can call methods of the Class. The Class can raise events to the Form. Don't talk directly from a Class to a Form. Or from one Form to another.
Form1 with TextBox1:
Public Class Form1
Private myClass1 As Class1
Private myForm2 As Form2
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myClass1 = New Class1()
myForm2 = New Form2(myClass1)
myForm2.Show()
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
myClass1.SetText(TextBox1.Text)
End Sub
End Class
Form2 with Label1:
Public Class Form2
Private WithEvents myClass1 As Class1
Public Sub New(instance As Class1)
InitializeComponent()
myClass1 = instance
End Sub
Private Sub myClass1TextSet(value As String) Handles myClass1.TextSet
Me.Label1.Text = value
End Sub
End Class
Class1:
Public Class Class1
Private text As String = ""
Public Event TextSet(value As String)
Public Sub SetText(value As String)
Me.text = value
RaiseEvent TextSet(value)
End Sub
End Class
Form2.Label1 will update as you type in Form1.TextBox1. You may change it around as you need to fit your application, but try to keep this structure.
Form >> instance >> Class
Class >> events >> Form
The form instantiating the other form is for simplicity of this example. Larger scale projects could have a form loader factory responsible for making forms.
In your Form2 add a public shared integer
Public Shared score As Integer = 0
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Then in Form1 increase it by adding to it
Form2.score += 50
for example
Add two forms to project
in Form2 define Label1 control
and in Form1
Public Class Form1
Private intScore As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Form2.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Label1.Text = intScore.ToString
End Sub
End Class

Handle events of all controls on a VB.net form from a class module

I'm a beginner using VB.Net. I have a form and I need to handle the
events of all controls on a form by using an external class module.
How I can do this by passing to this class only the parameter form?
Thanks friends.
After a bit of messing around - this works.
Create your event handling class either at the bottom of your main form file after End Class with all your event handling code - or in another file - it doesnt matter. As long as all your subs in the new class are declared with the shared keyword. - like this
Friend Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Shown
End Sub
End Class
Public Class OtherClass
Public Shared Sub test2(sender As Object, e As EventArgs)
MessageBox.Show("hi")
End Sub
End Class
In your main code, you need to use addhandler to subscribe the code in your other class to the events - like this
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Shown
AddHandler Button1.Click, AddressOf otherclass.test2
End Sub
So now - based on the above code, when you click the button, a MessageBox will pop up showing Hi