Referencing System.Windows.Forms from a Class Library - c#

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.

Related

Use TreeView Class in a Class Library

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.

How to make dll with UIElement for Windows Phone

I frequently load dll file and import namespace for instance
xmlns:UI="clr-namespace:Microsoft.Advertising.Mobile.UI;assembly=Microsoft.Advertising.Mobile.UI"
and then create XAML element using it
<UI:AdControl .../>
Can anyone share a post where is described how to create such a useful dll?
You create this like any other DLL in .NET:
Create a "Library" project. There is a "User Control Library" in some versions of WPF, so you might as well use it, but I believe a standard "Class Library" will work just as well
Add a "UserControl" to your library. Make sure it is marked public!
From the project that will use this DLL, add a reference to the new library project
Create a xmlns line like the one you have, but mapped to your assembly and namespace
Use the UserControl, again just like you already have.
Nothing special really, it really is just like any other DLL project.

Can't find "Point" in System.Drawing

As I know, Point are exist in namespace System.Drawing, but Visual Studio can't find it.
using System.Drawing;
class Flower
{
public Point Location { get; private set; }
}
Error: Can't find name of type or namespace "Point"
Edit 1:
.Net 4.5.1
Right click in Solution Explorer on your Project's References entry and click on Add Reference...
Make sure you're looking under Assemblies --> Framework, then find and check the checkbox for System.Drawing, then click OK.
From here, you can useSystem.Drawing.Point in your code.
As others have indicated, you are missing a reference to System.Drawing in your project. The reason this works in some project types and not others is that some project types, specifically Windows Forms projects, will automatically add the reference to System.Drawing for you. While other project types like Console App, Class Library, or WPF Application do not automatically have that reference, so you have to manually add it.

C# using <myowndll>, doesn't work (VS10 Express)

Background: I am a novice in C#, and use Visual Studios 2010 Express.
I have a class (let's call it myclass) which I want to use in multiple projects. I used to add classes with Project->Add Existing Item... Which creates a copy of myclass.cs.
Now I just found out that when I build the original myclass.cs it creates a myclass.dll and places it in the release folder of my project.
But when I try to use this DLL, I get the following error:
The type or namespace name 'myclass' could not be found(are you
missing a using directive or an assembly refference?
Which is weird to me, because I already have referenced it (it is also in the Reference folder of my Solution Explorer). And I already have added this to my code:
using myclass;
So what am I doing wrong?
Update: When I tried my old method (add existing item -> myclass.cs) the error message goes away. So it's not a matter of spelling things correctly.
Add the dll first:
Click on references in your project-explorer in visual studio and add your dll then you can use it as you expected it.
Add the reference in your project and check that the target Framework version of that assembly fits the project.
Check the namespaces inside the assembly and then use them like:
using YourAssemblyNamespace.class
Okay so I found the answer myself. It turns out that when you use the using function, it automatically searches for all public classes in the namespace you want to use.
If it can't find a public class, it refuses to recognize the DLL.
Furthermore, not specifying a class makes it internal.
So:
class myclass // internal!
private class myclass // private!
public class myclass // only this makes it visible for others!
Everything was okay after changing class myclass into public class myclass.

WeakEventManager class not found

I'm attempting to inherit from the WeakEventManager class but the namespace can't be found in my project. I'm not sure what's going on as I have the using System.Windows; directive. I can load a project that uses this class successfully I just can't seem to implement it on my own. The project is .Net 4.5 and I'm unsure as to what's happening.
Any help is appreciated. Thanks in advanced.
The class lives in the System.Windows namespace, but is defined in the WindowsBase assembly. Make sure you have imported the WindowsBase assembly and added it as a reference to your project.
In Solution Explorer
open your project
right-click on References
click Add Reference
select .NET tab
select System.Windows
click OK
In Solution Explorer
open your project
right-click on References
click Add Reference
select .NET tab
select WindowsBase

Categories

Resources