[RESOLVED] [02/03] Manipulate a form from a different class
Hello,
I have three classes, Common, A, and B. A and B reference Common so that they have access to variables inside it (I know this may be bad design, but I didn't do it, and I can't change it). From A, I can make a new B.Main form. No problem. However, we only want one copy of B.Main open at a time. So, we store a variable in Common called (basically) BMainOpen (boolean). In A, if they try to open a new instance of form B, and one is already open, I would like to give them the ability to jump to the point they wish, not just close it and open a new one. However, I need to know the...?... handle?... memory address?... of the open B.Main form, so I can manipulate it (load the correct data, set the selectedTabPage of the tab control, etc.). I thought I would just add another variable of type B.Main to Common, but I can't reference B from Common because B already references Common, which would cause circular dependency (the VB IDE won't let me).
Quote:
In Form A
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f as B.Main
If Not BMainOpen Then
f = new B.Main
Else
f.? = MyStoredVariable
End If
f.DoStuff()
f.Show()
End Sub
I just need to know what f.? and MyStoredVariable would be.
Confused? I tried to make it as simple as I could. Anyone know the solution?
Thanks
Re: [02/03] Manipulate a form from a different class
Use.ShowDialog() instead of .Show. That will stop the processing on form A so they cant go back until they close form B.
Re: [02/03] Manipulate a form from a different class
I regularly use ShowShowDialog, but, in this case, they want to be able to have Form B open and Form A still be responsive.
See, Form B is used to attach things (pics, pdfs, whatever) to different objects. So, it interacts heavily with things outside of our main program (not Form A, but the MDIParent of Form A).
Re: [02/03] Manipulate a form from a different class
Ok, then create a public object variable of form B in a Module. Then when you create the instance from A you can test for B being opened or created yet. If it is then just reuse it or messagebox to the user that they need to close the other form B first.
Re: [02/03] Manipulate a form from a different class
Well, that is what I tried to do. I tried to create a public object variable of class B form Main in my Common Class (we call them modules). But, B references Common, so Common can't reference B to get that Main form type.
So, I was thinking, maybe I could store the memory address of my open form B and then just DirectCast it something over in form A.
Re: [02/03] Manipulate a form from a different class
Im not sure of your form designs but if you created the form B object variable in a Module or in the general declarations section of your Main form then it should work.
"Common Class" is not a valid term. A Module is a Module and shouldnt be called anything else. It creates confusion as it just did for me. :)
Re: [02/03] Manipulate a form from a different class
I was trying to say, in the Common Module,
Dim frmOpenFormBMain as B.frmMain
But I couldn't reference it, because B referenced Common already. So, I just changed it to
Dim frmOPenFormBMain as Form
And, then, when I need to use it, I just DirectCast(frmOpenFormBMain, B.frmMain).
Problem solved.
Thanks for the help. Looking at this from different angles made me realize I could just use a generic form. Also, I tried it using and Object instead of a Form, and it worked like that, too.
Re: [RESOLVED] [02/03] Manipulate a form from a different class
You can also Inherit a form a different way, probably better too. In the forms designer code where it says "Inherits System.Windows.Forms.Form" you can change that to say "Inherits frmOpenFormBMain". Then it wont be dependant upon form b but it will only be "created" off of form b as a template.
This is called Visual Inheritance. :)