I selected Class Library option in C# Vis Studio Express to create a DLL which holds heaps of my commonly-used methods etc…
I’m trying to create a textbox in the class file, so that when I add the dll to another project all i have to type is:
MyControls.Create(TextBox);
…And it will create a text box and return it to me and then i can add it to the form.
I know how to create classes etc, so , my quesiton is… Why can’t I use System.Windows.Forms is a class file? I have the following in my Class1.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyControls
{
public class class1
{
public object Create(object control)
{
System.Windows.Forms.TextBox t = new System.Windows.Forms.TextBox();
// textbox properties go here etc...
return control;
}
}
}
BUT the red squigly lines keep telling me that “The type of namespace ‘Windows’ does not exist in the namespace ‘System’ (Are you missing an assembly reference)?”
Have I forgotten to add something here…??
Thank you 🙂
It sounds as if you are missing a reference to
System.Windows.Forms; add that reference and your code should compile fine.Side Note
I do get a bit curious about your method:
What is the input parameter used for? If you don’t use it there is no need to pass it. Also, since the method is supposed to create controls for you, you could change the return type to
Control. That will remove the need to cast it in order to add it to the form. I would suggest design the method like this instead (making use of generics):