Reputation: 81
This is partially related to this thread Combining two projects and get a single .sln file.
What are the correct syntax to call the forms on these projects. For example, if Solution1 contains Project1 and Project2, ...and... Project1 has Form1.vb & Project2 has Form1.vb. So what is the syntax to call Form1.vb in Project2 from Form1.vb in Project1 (assuming there is a button to click and open a form on click event).
Just a note however, I've added Project1 & Project2 to Solution1 as well as added a reference to My Project.Resources.Designer.vb.dll.
But when I tried to call Form1.vb in Project2 from Project1, I got syntax error - Project2.Form1 is not defined.
Can someone point me in the right direction?
Any help is greatly appreciated. Thanks in advance.
Project1 is bold hence the startup project.
Public Class Form1 of Project 1: -
Public Class Form1 of Project 2: -
Error message: -
Don't have the option to select "Import Namespace": -
This is how the form Project1 looks like: -
My Reference Manager => Solution option is empty
Let say, if I want to browse to the reference file in Solution => Projects option above, which file type should I choose ??
a. Visual Basic Project file
b. USER File
c. VSPSCC File
How to call Form1 (in olAddIn_With_Form1) from Project1 (Startup project) ?
Answer:
Add the .dll via Reference Manager window then browse to ...\bin\Debug\olAddIn_With_Form1.dll
Dim myolAddIn_With_Form1Form1 As New olAddIn_With_Form1.Form1
myolAddIn_With_Form1Form1.ShowDialog()
For kicks, I try to add the whole project via "Add as link" method and I got this error message
Upvotes: 8
Views: 12950
Reputation: 1
I try this and its work for me.
if the error show ('A reference to project could not be added...'). then go back to your project references and delete that reference and repeat the above steps again
Upvotes: 0
Reputation: 794
I'd like to add another approach since I've come across this problem too. The only difference is that I couldn't add a reference to my second project since it already had a existing reference. Trying to do so would result in the following error:
A reference to 'XXXX' could not be added. Adding this project as a reference would cause a circular dependency.
So here is a working approach (for me):
In my calling class:
'Path of the .exe which contains the form that should be opened
Dim allAssemblies As System.Reflection.Assembly = System.Reflection.Assembly.LoadFile(Application.ExecutablePath)
'loads content of the assembly file
Dim runTimeTypes As Type() = allAssemblies.GetTypes
'iterate the content
For Each runTimeType As Type In runTimeTypes
'if matching record is found
If runTimeType.Name = "F_myForm" Then
'open it with the desired parameters; of course a matching constructor has to exist in "F_myForm"
Dim parameters As Object() = {"x", "y", "z"}
Dim form As Form = CType(Activator.CreateInstance(runTimeType, parameters), Form)
form.Show()
End If
Next
Upvotes: 0
Reputation: 12806
So, to answer with some screenshots:
First create your two projects. The project that is the startup project (in your example and in mine, that would be Project1) needs to know about the other solution. To do this, we need to add the reference to the project, right click on Project1 and click on 'Add reference...'
Then, use the solution option in the sidebar, to click the checkbox on Project2
And then you can add the project in your code using the Project2.Form1 identifier, as such
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frmOtherProject As New Project2.Form1
frmOtherProject.Show()
End Sub
End Class
or, in case your form in the second project doesn't have a biased name (form1 currently exists 2 times, so lets rename it as form2), you can import the second project and use it's classes directly as such
Imports Project2
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frmOtherProject As New Form2
frmOtherProject.Show()
End Sub
End Class
I used Visual Studio 2012 as a basis, but the principle should be the same ;)
ADDITION
I might point out that this will not be your typical style of referencing the projects, mostly you will separate your application by levels of concern, by adding, for example:
There are of course plenty of ways to arrange your application in a meaningful structure, but I find that this one is a good example on how you can structure your application in a meaningful way
UPDATE
As an update still, getting your solutions to share code, shouldn't be this hard, if you write your code so that it can be reused. By sharing the logic and the harder code inside a class library that can be shared over both solutions, you only have to rewrite the Presentation layer (how you display the data). And you can do it more specifically for the environment you want to work with.
In the end, your Outlook Addin Solution and Your windows forms project could share the code that requests the resources, or loads the data, or does some other complex calculations, and the only code you have to "reproduce" is how you show it on the screen. So according to the environment you can present the data in a better way, specific to that environment, but share the logic and the models you use in both (or more) environments.
This way, you development time is cut down, and your code becomes less error prone, because you don't have the same code several times, as an example, see the following screenshot:
As you can see, there is a shared library, that is referenced by the outlook addin and by the windows form application. Neither the Forms application nor the outlook application know about each other, and they also shouldn't, as they have essentially nothing to do with the other.
So, although, my latest update doesn't answer your question, I still think it's the better way to arrange your code. If you would later want to make a website reusing the code you made, you only need to make an extra presentation layer, and reuse the code from the SharedLibrary once again.
Upvotes: 5
Reputation: 5403
This allows multiple projects to share code files without requiring a shared dll, references, or anything. Each app can stay as a standalone EXE.
Upvotes: 0
Reputation: 9041
I have a solution that has a C# project and a VB.Net project.
When I right click on My Project
in my VB.Net project I'm given an Open context menu
When I click open, I get the following screen
I click the Add button and get this screen
The project that is listed here is my C# project that I can add and in my VB.Net project and I can call upon any public classes in that project.
Upvotes: 0