简体   繁体   中英

How to call Forms in multi projects, single solution environment (VS2012)?

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

在此处输入图片说明

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:

  • An entitylayer, that contains your models, and is referenced by all other layer (solutions)
  • A datalayer that can load/save your models and serve them, this is referenced by the "business logic layer" and references the entitylayer itself
  • A business logic layer, that references the datalayer and the entitylayer and that is referenced inside the presentation layer, it doesn't know anything about which database you are using, it just serves as an intermediary between your presentation layer (which is what the user sees) and the datalayer, and only handles the entities defined in your entity layer
  • And long last, the presentation layer, that references the entity layer and the business logic layer, it to doesn't need to know which database is serving the data, but only presents the data in a useful way to it's users

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.

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.

  1. Solution Explorer>Project>Right-click>Add>Existing Item
  2. Browse & select the file
  3. -- here's the trick -- click the arrow next to "Add" and select "Add as link"

This allows multiple projects to share code files without requiring a shared dll, references, or anything. Each app can stay as a standalone EXE.

How to accomplish this task without references:

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

I try this and its work for me.

  1. In the Solution right click on project in which you want to show your form.
  2. Add references.
  3. Click on Project.
  4. select your project checkBox to true and click ok.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM