tags:

views:

53

answers:

4

Hi All

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

+2  A: 

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:

public object Create(object control)
{
    System.Windows.Forms.TextBox t = new System.Windows.Forms.TextBox();
    // textbox properties go here etc...

    return control;
}

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

public T Create<T>() where T : Control
{
    T control  = Activator.CreateInstance<T>();
    // textbox properties go here etc...

    return control;
}
Fredrik Mörk
thank you heaps Fredrik :) :D
baeltazor
@baeltazor: You are welcome :) Note the updated code sample (with the added type constraint that I first forgot)
Fredrik Mörk
The input parameter was going to be used so i can just pass a few properties before the control gets created in case i just wanna have a control created thats slightly different onetime... but i like your way better :P
baeltazor
re revised Create<T> : while the 'where constraint allows you to set Properties and Events ALL instances of 'Control expose (like Name, Text, MouseDown) : you will, of course, NOT be able to set properties unique to a specific type of Control : like : for TextBox : the 'MultiLine property. But, you can automatically upcast (.NET 4.0 : not tested in earlier versions) the result of calling Create<T> by assigning it to a strongly typed variable : example : TextBox myTextBox = Create<TextBox>(); : at that point you do, of course, have access to TextBox specific properties, events, etc.
BillW
oh awesome! Thanks BillW :) :D
baeltazor
I don't quite get why this is better than just using the `Textbox` (or other control) constructor though...
Svish
@Svish: the idea is the same as with any factory method; to make sure that the created instance is initialized to a certain state by assigning values to some properties. If the method does nothing else than simply create the instance and return it there may not be much use for it, but it could be that you want to initialize it to use a certain font, background color, border style or whatever, and then a factory method is preferable over multiplying this behavior in your code.
Fredrik Mörk
+1  A: 

Yes you have to add a reference to the winforms dll (System.Windows.Forms) in the references of the project. This happens automagically when you make a win forms application but since you made just a dll it is not there.

Hogan
+1  A: 

Add the reference. Because you created a class library instead of a forms project, you don't have the necessary libraries reference.Go to Project menu>Add Reference... and select System.Windows.Forms from the .NET tab.

Jay
A: 

As said above, adding a reference to System.Windows.Forms is enough. Though I'm personnaly not fond of having common classes and controls all mixed up in the same assembly, but rather having one or more projects for the non-GUI code plus another project for the pure GUI code.

Sylvestre Equy