8

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.

enter image description here



Public Class Form1 of Project 1: -

enter image description here



Public Class Form1 of Project 2: -

enter image description here



Error message: -

enter image description here



Don't have the option to select "Import Namespace": -

enter image description here



This is how the form Project1 looks like: -

enter image description here



My Reference Manager => Solution option is empty

enter image description here



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

enter image description here



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()

enter image description here



For kicks, I try to add the whole project via "Add as link" method and I got this error message

enter image description here

Community
  • 1
  • 1
pdsd
  • 81
  • 1
  • 1
  • 4
  • 1
    Which project is your startup project? – Shar1er80 May 06 '15 at 17:28
  • 1
    Project1 - on startup, Form1 of Project1 will open. – pdsd May 06 '15 at 17:32
  • 1
    You've added Project2 as a reference to Project1? I would guess, that in Project1, you would have "Dim p2Form2 As New Project2.Form1" – Shar1er80 May 06 '15 at 17:35
  • 1
    Yes, that's what I thought but I got the error saying Project2.Form1 is not defined. I also tried Solution1.Project2.Form1 but to no avail. – pdsd May 06 '15 at 17:44
  • Is Form1, of Project2, a Public Class? Posting some code & screen captures would help. – Shar1er80 May 06 '15 at 17:46
  • Yes. In fact both are Public when I added new forms. – pdsd May 06 '15 at 17:47
  • If project A and B are WinForms (which seems to be the case since they both have forms), then each project compiles to its own EXE. Its more common for one project to be an app, then the other(s) to be DLL. – Ňɏssa Pøngjǣrdenlarp May 06 '15 at 18:14
  • Pls bear with me. I do not know how to add screenshots per this thread http://meta.stackexchange.com/questions/135483/how-can-i-add-a-screen-shot-into-my-question. – pdsd May 06 '15 at 18:16
  • Hi @Shar1er80, I just added a few snapshots. Just FYI. – pdsd May 06 '15 at 19:04
  • you see the little red "line" at the right, hover it, and then click import namespace... – Icepickle May 06 '15 at 19:20
  • I don't have that option to "import namespace" (pls see snapshot above) – pdsd May 06 '15 at 19:26
  • You're still having an issue with this? Have you actually saved the solution? Meaning that there is a .sln file somewhere that you can double click on, in a Windows Explorer, that will open Visual Studio and your two projects will be a part of that solution? – Shar1er80 May 11 '15 at 20:24
  • Yes, the solution is saved and it has the .sln file as you described above. The prob is when I go to the Solution option in Reference Manager, there was no project listed in the Projects tab. Any clue what did I do wrong? Thanks – pdsd May 11 '15 at 22:37
  • 2
    At this point, I would suggest starting over. Create a new solution with just Project1 write some test code, save and build. Then add Project2, then follow either answer to add Project2 as a reference to Project1, save and build that. Add the code to Project1 to open a Project2.Form1 and try it. – Shar1er80 May 12 '15 at 14:27

5 Answers5

5

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...'

add reference to startup project

Then, use the solution option in the sidebar, to click the checkbox on Project2

select second project

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:

enter image description here

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.

Icepickle
  • 12,689
  • 3
  • 34
  • 48
  • Beat me to gathering screenshots for this answer, and addresses the comment I posted about adding Project2 as a reference to Project1 – Shar1er80 May 06 '15 at 19:50
  • @Shar1er80: is that a good or bad thing? And i updated the answer with some points of thoughts as well :) – Icepickle May 06 '15 at 19:52
  • It's a good thing. You were faster at answering it before I could finish my answer :-) – Shar1er80 May 06 '15 at 19:53
  • Thanks @Icepickle. The name Form1 on both Project1 & Project2 was deliberate so I know how to address each form from different projects.

    Also, if Project1 is a Console Application and Project2 is a VSTO Add-In, would this solution still holds true ??
    – pdsd May 06 '15 at 19:59
  • If I right-click to add reference, but in the Solution option of Reference Manager is empty meaning there is not a single project listed in there, what did I do wrong? Thanks again. – pdsd May 06 '15 at 20:35
  • @pdsd You might have forgotten to Build the solution, or to save the changes?, also make sure that Project2 isn't referring to Project1 yet... – Icepickle May 07 '15 at 13:31
  • Sorry, I wasn't able to work on it until now. @Icepickle, I did build the solution and save changes, just FYI. Let say, if my Solution option is still empty, do you know where should I browse to choose the reference file ? (snapshots are attached above). Thanks in advance – pdsd May 11 '15 at 20:31
  • hi guys, I think I know why my Solution option in Reference Manager was empty. Let say if the 2nd project that was added to Solution1 is a VSTO add-in then it wouldn't show up in Solution option tab (pls see snapshot above). Therefore, I couldn't add a reference to that 2nd project. So my question is, how do I call Form1 in olAddIn_With_Form1 from Project1 ? – pdsd May 13 '15 at 16:54
  • @pdsd I just tried it with a Visual Studio Add in, and then added as well a FormsControlLibrary as a windows forms project, and I could add both as a reference. Which version of Visual Studio are you working with, just to be sure? – Icepickle May 13 '15 at 18:37
  • @pdsd A small update, I also couldn't import the Outlook Addin into my windows form app, but I will post a work around for you later this evening (has something to do with the above mentioned layers of concern :)) – Icepickle May 13 '15 at 18:42
  • I'm using Visual Studio 2012 (VS2012). Thanks @Icepickle for looking into this. I think I may have found the solution. See the snapshot I attached above under the title "How to call Form1 (in olAddIn_With_Form1) from Project1 (Startup project) ?". I basically answered my own question after a few tries and errors by adding the .dll – pdsd May 13 '15 at 19:07
  • @pdsd Though that might work, I added an update in my post trying to explain why this might not be the best option that you are using. Try thinking about a "bigger" picture. I hope I could explain it clearly enough – Icepickle May 14 '15 at 07:33
  • Yes, I absolutely agree with you, Icepickle. SharedLibrary is definitely the way to go. The code wasn't written with code reusability in mind from the get-go. Seems like some major tweaks need to be made prior to using the SharedLibrary method. I'm just a newbie and still learn the ropes but I'd to thank you for taking the time off your busy schedule & sharing this great knowledge with us all. Thank you again. – pdsd May 14 '15 at 15:46
0

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

enter image description here

When I click open, I get the following screen

enter image description here

I click the Add button and get this screen

enter image description here

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.

Shar1er80
  • 9,001
  • 2
  • 20
  • 29
  • Thanks Shaler80. That is another way to get to the Reference Manager => Solution option page. The prob is, there was nothing listed in Projects tab. Not sure what did I do wrong here. OK let say, if I want to browse to the reference file as shown in the Project tab, do you know where does this file reside? Thanks – pdsd May 11 '15 at 22:29
  • @pdsd You probably need to create a brand new solution, save it, add these projects to it and try the suggestions listed. – Shar1er80 May 12 '15 at 00:06
0
  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.

SSS
  • 4,807
  • 1
  • 23
  • 44
  • Add as link is good if I'm re-using part of the code, but if I'm re-using the whole project,I think it would make more sense to add as a project. – pdsd May 13 '15 at 17:02
  • For kicks, I did try to "Add as link" by selecting all items under the folder and I got an error message saying "Some of the files you are attempting to add to the solution cannot be added because ..." (see snapshot above). – pdsd May 13 '15 at 17:05
0

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
bautista
  • 765
  • 10
  • 26
0

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

Khan
  • 1
  • 2