Call Forms from Two C# projects in a third Project - c#

This is the first time I start with StackOverflow...
I think my question is a silly question!!
I need to call the forms and classes from two C# projects in a third project
I follow these steps:
From Solution Explorer in a third project, I have clicked the Add Reference
I click the Project Tab
I didn't find any add browsing to add these two projects. see the image

Create your multiple project in a single solution. then add project1 and project2 in your project3 reference using >> Add Reference >> Projects >> Solution.
here's how: https://msdn.microsoft.com/en-us/library/f3st0d45.aspx

Your Projects should be under same solution.
If in case you are building two projects differently not under same solution follow these steps
1.Create a winform project let say "Project1" go to project settings change the output type to class library and build it.
2.Create another winform project lets say "Project2" go to project setting change the output type to class library and build it
3.Create third project from where you want to call it. Go to Add reference. Browse for folders as below
debug folder of project1 select project1.dll and add reference.
debug folder of project2 select project2.dll and add reference.
4.Now create objects of Forms and call.

Alright. Visual Studio offers you the ability to add references you just created yourself. Those references could be (dynamic-link-)libraries DLL or executables EXE. So go ahead and create a project and add a new Form. This custom Form needs to be public so the other project is able to extract the code.
Now you are able to add and use a reference. Just be sure to include the namespace, if it doesn't match the one, in which you're going to use it.
The thing is called Project-to-project Reference

Related

How to use a class of one project in another project under the same solution?

I have two projects of Web API and Windows Forms App under one solution.
The names are:
Solution - CliendAddress
Web API - ClientAddress
WFA - ClientAddressWFA
In the ClientAddress project there is a class called ServiceResponse. How can I use this ServiceResponse class in my project ClientAddressWFA?
From my ClientAddressWFA, I've already added the reference to ClientAddress ๐Ÿ‘‡
However, when I am trying to add using ClientAddress.Models; (<---- This is where the ServiceResponse class is) in my ClientAddress.WFA project, I'm getting an error๐Ÿ‘‡
Recording: https://screenrec.com/share/XTp0dwbI42
If you need just this one source file then you can just add it to the other project "as link". Quick and dirty, but who cares. However, if you need more classes, then make a shared library project that would then be referenced by both projects. That's what shared libraries are for.
So, in project ClientAddressWFA try to add this one .cs file but instead of clicking "Add" click on small triangle next to "Add", and select "Add As Link".
Sometimes just restarting the visual studio and compiling again will work (Given that class you are referencing from another project is public and you have added project reference)

Adding common files to different projects in C#

I have 2 project in difference paths. I want to use some common files (.cs files) in both of projects.
D:\Tests\WindowsFormsApplication_1\WindowsFormsApplication_1
D:\Tests\WindowsFormsApplication_2\WindowsFormsApplication_2
D:\Tests\Common
How do I add this common files from one path ("Common" folder) for both of the projects?
Yes, You can. Right Click on your project, "Add" -> "Existing Item...", select your *.cs file and click on Down arrow next to the "Add" button and select "Add As Link".
There is no way to use same files in 2 different project without copying them. But you can add the third project in type of class library and put your shared files in it then add references to that project in your previous projects
If you want the common files to be available to edit in your other projects then
follow user4015859's suggestion re setting up a class library project for your common files, but rather than adding a reference to that project in your other solutions, add the actual project to your solution by right clicking on the solution and select Add > Existing Project...
You still need to add a reference but in the reference Manager dialog (right click on references to open it) select Projects on the left hand side and your common project should be listed. Tick it and click OK.
To use the classes etc from the common project just add its namespace in the units where you want to use it.
This way you can edit the common files from either of your other solutions and you don't need to manually build your common solution when any of the files are changed.

Merging Two Projects and accessing files from each other in Visual Studio

I have merged another project in my existing project by right-clicking on the project name and selecting Add->Existing project option. Now i wan't to access the files from my newly added project from the existing one in Visual Studio (Its a C# project) , how can i do this?
If I'm following your question, you had a solution with a project A. You then added an existing project B.
You now want to access a file from A in B. If that's the case, right click on the B -> References and add a reference to project A. Now you can use the files from that project.
Edit: From your comment, I'm not sure if you want to have a separate project / file. If you want them to be only 1, you shouldn't really add a new project, you should add to your project A the files you need from project B. If you want to keep it as a different module that is included, then the above holds, but you need to realize that usually you have one solution, with many projects inside of it.
I struggle with this until I found this the link is below:
If you want all windows application to run as one program rather then calling multiple .exe. Then change 'Out Put Type' of each project to 'Class Library'.
Step 1:
You can do that by right-clicking on each Project in solution -> Go to Properties -> Application -> Out Put Type... set it to Class Library
Once you have done that output of these will be generated as .DLL.
Step2:
Add a new Window Form application project, add reference of exiting projects in it so that they can be executed from here.. you can do that by.. right click on main project-> Add Reference->Projects select all existing projects from here.
Now on the main application, you can create 3 buttons to launch each project...
[Inventory]
[Accounts]
[Payroll]
Now in each button code will be something like that...
Inventory_click()
{
Inventory.MainForm frm=new Inventory.MainForm();
frm.show();
}
Credit goes to forum post

Refactor such as winForm.exe calls a .DLL

I have a windows application (winForms). I would like to refactor it such that all functionalities are built to .DLL file so that when winForm is run, it will just call .DLL. In addition, I would be creating another .exe which is Console App so when a user wants to just "schedule task" it, he will create a config file that will run the console app, which when run will also call .DLL
I don't have much knowledge about refactoring and compiling projects to .DLL (I hope I am making sense)
I just want to know if I am correct on how I quite understand it for now:
Should I transfer all my functionalities from winForms to a class that will be compiled to .DLL? Or if I am wrong, what should I put in a .DLL class?
You should create a new project in your solution. Create a class library project in your solution in Visual Studio (or tool of choice, you did not specify what you are using so I assume VS).
To add a new project, right click your solution and select Add submenu, then New project.
From the categories menu on the left, select Visual C#, then Windows, and Class Library.
Then you should add a reference to this new class library project from your current WinForms project.
Right click References in your current WinForms project and select Add reference.
Then select Solution category on the left (VS2012), or Projects tab (VS2010) and Select your newly created class library project from there.
Then you can start moving classes from your current WinForms project to this new class library project. Class library project will be compiled as a dll and you will have access to all classes in this dll from your WinForms project.
Nothing dramatic is needed. Just Project + Properties, Application tab, change the Output type setting from Windows Application to Class Library. Done. You may have to declare a class public if you didn't already do that. You could remove your Program.cs source file since it won't be used anymore but that is entirely optional. A good reason to not remove it is keeping your project testable.
Fwiw, changing the Output type setting is not actually necessary, .NET doesn't distinguish between a DLL and an EXE at all. The CLR loads assemblies by their display name, it doesn't include a filename extension. You can add a reference to your EXE assembly in another project and it will work just fine.
So doing nothing at all already works :)

Shared source folders as an alternative to class libraries in C#

I'm wondering if its possible to set up and share an entire folder of source code between multiple projects in Visual Studio 2012 like how eclipse lets you use multiple source folders. I'm currently developing an entity framework model for a game I'm making with split server and client code, said framework currently sits in a class library referenced by both projects.
The reason I'd like to use a shared code folder instead is to add code to each entity component type by declaring the classes in the shared folder as partial and having another partial class in the client/server projects that adds sided functionality. I thought that achieving this would be more useful than extending each component on both sides, and it would also eliminate the need to have a DLL included with the product.
you can add a project to your solution (right click Solution -> Add -> Existing Project) and reference it (right click on your project -> Add Reference -> Solution -> Projects -> "Project you want to reference")
You can right click on a project and select - Add - Existing item.... Within the open file dialog select the desired file, click on the arrow next to the Add button and afterwards on Add As Link
(source: modbusdriver.com)

Categories

Resources