Question communication between a class and multiple forms

Behumble

Member
Joined
Oct 7, 2012
Messages
6
Programming Experience
5-10
Hi, I have a VB code which i want to convert to VB.NET code.

I am trying to understand how to do the below in VB.NET

-create a class (abc.cs file)
In the class itself if condition a = true then assign values to frm1, frm 2 and frm3. Now from this class i can call frm1.
But once from 1 is called, can i call frm2 and frm3 whose fields were populated with data in abc.cs class.

Please advice

Thanks.
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

Here is a bit of an example for you to look into.

1) Create a new forms application and in the solution explorer add another 3 forms so that you have Form1, Form2, Form3 and Form4.

2) Add two buttons to Form1 and then replace the code behind in Form1 with the code below:-

VB.NET:
Public Class Form1
 
  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    MyABCClass.AddDataAndDisplayForm2()
    MyABCClass.AddDataAndDisplayForm3()
    MyABCClass.AddDataAndDisplayForm4()
  End Sub
 
  Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    MyABCClass.AddDataAndDisplayAllForms()
  End Sub
End Class
3) In the solution explorer add a new class object and then replace the code in the class with the code below:-

VB.NET:
Public Class MyABCClass
 
  Public Shared Sub AddDataAndDisplayForm2()
    Form2.Text = "My Form 2"
    Form2.Show()
  End Sub
 
  Public Shared Sub AddDataAndDisplayForm3()
    Form3.Text = "My Form 3"
    Form3.Show()
  End Sub
 
  Public Shared Sub AddDataAndDisplayForm4()
    Form4.Text = "My Form 4"
    Form4.Show()
  End Sub
 
  Public Shared Sub AddDataAndDisplayAllForms()
    AddDataAndDisplayForm2()
    AddDataAndDisplayForm3()
    AddDataAndDisplayForm4()
  End Sub
End Class
4) Run the application and click the buttons.

This only demonstrates how to create the class and access objects and properties from within the class. It's up to you to decide how you want to expand the class to accommodate what you need to do.

Hope that helps and good luck.

Cheers,

Ian
 

Behumble

Member
Joined
Oct 7, 2012
Messages
6
Programming Experience
5-10
Hi Ian,

Thanks. I am creating a add-in tool, which on click executes MouseDown Event in a class file. In the mouse down event, based on certain conditions labels, comboboxes, text fields in form1, form 2 and form3 are assigned with values. From the same event Form1.show is executed. Till this everything is fine. But in Form1 there is a button which on click tries to access values assigned in mousedown event to form 2 and then executes form2.show()

This form2.show() shows a blank form, because i am not able to access the instance of form2 which was assigned values during mousedown event in class file.

I hope i have not confused you. Can your suggestion still be implemented?
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
Hi,

I think I understand what you are trying to say. To Clarify:-

When you click the button on form1 which opens form2 you want the opening of form2 to simulate a mousedown event being triggered on form2 thereby populating the controls that form2 has?

If so then create a subroutine or function in the public class object (this can also be in a module) which populates the controls in form2. Lets call it PopulateForm2Controls for this exercise. Then in the mousedown event of form2 and the button click event of form1 call the PopulateForm2Controls.

Have I understood you correctly?

Cheers,

Ian
 

IanRyder

Well-known member
Joined
Sep 9, 2012
Messages
1,130
Location
Healing, NE Lincs, UK
Programming Experience
10+
I also forgot the obvious here, you could also set the MouseDown event of form2 to Public instead of Private which can then be called from form1 Button Click event as follows:-

Form2.Form2_MouseDown(sender,e)

Ian
 

Behumble

Member
Joined
Oct 7, 2012
Messages
6
Programming Experience
5-10
I also forgot the obvious here, you could also set the MouseDown event of form2 to Public instead of Private which can then be called from form1 Button Click event as follows:-

Form2.Form2_MouseDown(sender,e)

Ian


Hi Ian,

Thank i will try this and get back to you.

Cheers
 

Behumble

Member
Joined
Oct 7, 2012
Messages
6
Programming Experience
5-10
Hi,

Here is a bit of an example for you to look into.

1) Create a new forms application and in the solution explorer add another 3 forms so that you have Form1, Form2, Form3 and Form4.

2) Add two buttons to Form1 and then replace the code behind in Form1 with the code below:-

VB.NET:
Public Class Form1
 
  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    MyABCClass.AddDataAndDisplayForm2()
    MyABCClass.AddDataAndDisplayForm3()
    MyABCClass.AddDataAndDisplayForm4()
  End Sub
 
  Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    MyABCClass.AddDataAndDisplayAllForms()
  End Sub
End Class
3) In the solution explorer add a new class object and then replace the code in the class with the code below:-

VB.NET:
Public Class MyABCClass
 
  Public Shared Sub AddDataAndDisplayForm2()
    Form2.Text = "My Form 2"
    Form2.Show()
  End Sub
 
  Public Shared Sub AddDataAndDisplayForm3()
    Form3.Text = "My Form 3"
    Form3.Show()
  End Sub
 
  Public Shared Sub AddDataAndDisplayForm4()
    Form4.Text = "My Form 4"
    Form4.Show()
  End Sub
 
  Public Shared Sub AddDataAndDisplayAllForms()
    AddDataAndDisplayForm2()
    AddDataAndDisplayForm3()
    AddDataAndDisplayForm4()
  End Sub
End Class
4) Run the application and click the buttons.

This only demonstrates how to create the class and access objects and properties from within the class. It's up to you to decide how you want to expand the class to accommodate what you need to do.

Hope that helps and good luck.

Cheers,

Ian



Thanks Ian. This worked. I just had to declare form objects as shared readonly in the class and then access it from outside just like you mentioned. Awesome!!! Thanks a lot.

Cheers
 
Top Bottom