Use TreeView Class in a Class Library - c#

Is it possible to use the TreeView Class in a class library.
I want to make a method where I pass a Treeview from windows form , I tried to import System.Windows.Forms but it is not found and I cannot find any nuget package to install.
I want to do something like this:
public void MyMethod(TreeView tree){tree.Nodes.Add("Something");}
Thanks.

In Solution Explorer:
Right click on the class library project, select Add > Reference. On the left sidebar choose Assemblies > Framework. Check System.Windows.Forms and click Ok.
I would caution against adding this reference in your existing class library unless you want it to be specifically only for WinForms. Perhaps a cleaner approach would be to create a new class library, say .WinForms, and add a project reference to the original project and a Framework reference to System.Windows.Forms as I described above. This would keep the rest of your code separated out from the WinForms code in the base project, and then it could be used in other contexts without WinForms.

Related

Converting a C# class I created inside a project to a separate reusable class

I'm not new to C# programming, but I suppose I'm new to programing "the right way" in C#. I've worked in C on embedded devices for years and have written desktop apps to support them. First in VB6, then in C#.
I recently started making better use of classes for reusing code (and for instantiating more than one instance of the class in a program). For example, I "wrapped" a UART interface with some additional functionality so I can use the same code for multiple ports by creating an instance of the class for each one.
It is in a separate file, but still in the same program namespace, so when I want to reuse it, I have to copy the file and change the namespace to the new project.
I'm sure there's a way to create it such that I can just reference it like everything else with either a "using..." reference at the top of the program or with a "Project | References..." checkbox. But for the life of me I can't find a good learning journey for this.
Any direction would help.
You want to create your reuseable class in an assembly - this is the equivalent of a dll from your C experience.
To create an assembly, have a separate project of type assembly (instead of exe) . You can reference the assembly from other projects. If your project is in the same solution you can reference the project, otherwise you can reference the compiled assembly.
C# uses a packaging system called Nuget, so you can package your assemblies into "Nugets" which you host in a Nuget Server. You can then use tooling to discover and import these.
Please create a Class Library project and include your class into that project. Make sure your class is public. Once you build this project you'll get an assembly which can be referenced from other projects. See Tutorial: Create a .NET class library using Visual Studio
There are different ways of referencing it.
You can have the class library project in the same solution as the main project. In this case you should add a project reference.
You can copy the compiled *.dll file to some folder in your solution (e.g. Lib) and add an assembly reference.
If this assembly is to be used in multiple projects please consider creating a NuGet package with this library and pushing it to some repository. Then other projects can add a package reference to this package.
Details:
How to: Add or remove references by using the Reference Manager
Install and manage packages in Visual Studio using the NuGet Package Manager
It is in a separate file, but still in the same program namespace, so when I want to reuse it, I have to copy the file and change the namespace to the new project.
Well, it isn't the best practice but (unfortunatly) still a common behavior. So don't worry to much about it.
What you could do to improve it place the file (and other reusable parts) in a seperated csproj.
For example name the project of the type class library and name it VinDag.Tools. Within the project create a folder UART and place the wrapper there. The namespace of the wrapper would then be VinDag.Tools.UART.
From know on you can just reference the class library instead of renaming the file. It's not necessarily required to be the same namespace as the project.
From there you can start considering (private) nugets. This would prevent you from copying files/csproj around.

How to use class from other project in visual studio C#

I have a Xamarin.Forms project and a C# console app project. I want to use one class from the console app in my Xamarin.Forms project.
I added the console app project to the solutions explorer of the Xamarin.Forms project.
Unfortunately, I can't figure out how to use the class from the console app in one of the Xamarin files.
I always get the error message:
The name 'MyClass' does not exist in the current context.
I tried to press alt+enter to show potential fixes but it does not offer me the option of importing/using the class.
I also wrote manual using directives in various forms but it still does not seem to make the class accessible.
The only way I was able to use the class was by adding the class directly to the Xamarin project by adding it with add->existing item. The problem with this is that it imports a copy of the class. Since I'm still working on the class within the other project the added class is fast outdated and I have to manually copy its contents over.
How can I use a class from an external project without making a copy of the file?
Instead of access class from console app(its exe) try creating new reusable library add that class and use in both projects also you can write wrapper class in both projects
Try to add a reference to the second project in your first project. To do this, right-click on your project, select Add Reference then select the project in your solution. Once your main project references the second project, then you can access its public types.

Using third party controls

I am pretty new to WPF Developing and i am trying work with the GraphSharp lib within my project but I cant access the GraphSharp.Controls dll classes.
i declared this row in my mainwindows.xaml file
xmlns:gsc = "clr-namespace:GraphSharp.Controls;assembly=GraphSharp.Controls"
and when I try to write
<gsc:GraphLayout/>
Note-GraphLayout is a class that represents a graph in xaml
I tried to look on how to declare namespace in xaml and other resources but couldn't find a good answer for my problem...
Did you try to rebuild the solution after adding the GraphSharp lib? In order for the XAML designer/editor to see it, it needs to have the solution rebuilt. Otherwise it will give you an error stating it could not be found.
I just added the GraphSharp lib to a project and was able to use the same namespace you provided, so the namespace is valid.
Also make sure you have added a reference to the GraphSharp library in your project. The best way to do this is via NuGet. Right-Click on your project References and chose to add a new NuGet package. Search for GraphSharp and install it.

Referencing System.Windows.Forms from a Class Library

I am creating a Class Library and need to inherit from PictureBox: public class Picture : PictureBox { ... } but Forms is not available when I try to add a using directive for it at the top of my Class Library: using System.Windows.Forms;. I know that I can get it to work by right-clicking Referenced in Solution Explorer and selecting Add Reference, then adding the System.Windows.Forms assembly from the list.
But is it okay to do this? Is it okay to reference WindowsForms from a Class Library?
It is totally okay to do this.
System.Windows.Forms is just an assembly like any others. There is no special treatment to the project file, as with for example Office add-ins.
You can safely add this assembly to your project file.

VS 2008 C# : Class library is not accessible in another class library in same solution

I have a solution in VS 2008 which has one web project and 3 Class libraries as 3 different Projects. One project is for DataAccess and one is for BusinessLogic.
I have a class in DataAccessLayer. From there when I am trying to access the Class of BusinessLogic class library (project) it is not coming in the IntelliSense when I type. I used the same namespace in both projects. Still same results.
Do I need to to create DLLs for the first project and add as reference to second?
You need to add reference to this project in another project in your soultion.
Visual studio has an option to add project as a reference, so you don't have to add assembly files directly
You need to reference the library in the other projects.
To do that, right-click the references folder in the Solution Explorer, click Add Reference, go to the Projects tab, and select the library that you want to reference.
EDIT: Also, make sure that the class you are trying to use is declared as public (eg, public class MyClass).
If you leave out the public modifier (which is the default), the class will only be usable in its project. To expose classes and members to other projects, add the public modifier to their declaration
You'll need to add a reference to the project containing the BusinessLogic class in the DataAccess project. Otherwise, the compiler doesn't have anyway of finding the implementation of your BusinessLogic class, even if it does use the same namespace.
This may sound silly, but have you specified the class in question as Public or Friend? They'll need to be "shared" in that sense in order to be properly picked up and used within the other applications, even when the project reference is specified.

Categories

Resources