Hey guys I'm new to C#.NET but I'm trying to create my own Form using my own GUI code. However I'm unsure which project I need when using Visual Studio 2008. A normal person would assume that selecting Windows Forms Application would be correct. However you'd be wrong it would appear that you can't create your own code for the GUI since it incorperates a GUI builder. I'd sooner learn the hard way than have the GUI Builder do it for you. Any ideas has to what I can do?

Dani AI

Generated

Quick practical clarification for anyone finding this thread later: WinForms (desktop) and ASP.NET (web) are different technologies. For hand-writing a GUI you should work inside a desktop project (a Windows Forms Application, or a Console/Class Library that references WinForms) — the Visual Studio designer is optional and can be ignored. , your learning approach is fine; you just need the right project type and references to get meaningful results.

The specific compile errors you saw ("The type or namespace name 'Windows' could not be found" and "Label could not be found") mean the compiler couldn't find WinForms types in that project. Adding "WindowsFormsInteragtion" (as you did) is not the same thing — it’s the wrong assembly for creating Form controls. Make sure the project has the System.Windows.Forms assembly referenced. Also remember that an ASP.NET page uses web controls (System.Web.UI.WebControls.Label), so trying to display a WinForms Label from a web project will not produce a browser UI. and were on the right track about the separation between web and desktop.

A minimal example of creating a Form entirely in code (no designer) — create a desktop project, add a reference to System.Windows.Forms and then try something like:

using System;
using System.Drawing;
using System.Windows.Forms;

class MyForm : Form
{
    Label lbl;

    public MyForm()
    {
        Text = "Manual Form";
        Size = new Size(400, 200);

        lbl = new Label { Text = "Hello from code", AutoSize = true, Location = new Point(10, 10) };
        Controls.Add(lbl);
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new MyForm());
    }
}

Extra tips: if you already have a WinForms project but want no designer, you can ignore or remove the .Designer.cs and initialize controls in your constructor. For server-side web work stick to ASP.NET web controls; for desktop experimentation use a WinForms project so the runtime and assemblies match. ’s point about the designer saving time is valid, but it’s purely optional for learning.

Recommended Answers

All 9 Replies

You can create any project type and put in your windows forms by referencing System.Windows.Forms.dll

You can create any project type and put in your windows forms by referencing System.Windows.Forms.dll

Right based on what you said I've gone for an ASP.net application, that gives me the best of both worlds. However the import for System.windows isn't there.

Yes, you can, but logically why user in web-based application interacts with windows forms?! it sounds not logic at all, instead interacts with web forms.

Ok well Ive tried the following

private Label lb;

however i get the following error:

Error 1 The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?) C:\Users\John\Documents\Visual Studio 2008\Projects\Project1\Project1\ 5 7 Project1

Error 2 The type or namespace name 'Label' could not be found (are you missing a using directive or an assembly reference?) C:\Users\John\Documents\Visual Studio 2008\Projects\Project1\Project1\ 13 17 Project1

In the references though I've added the link to WindowsFormsInteragtion, and added the using System.Windows.Forms;
Any ideas?

WindowsFormsInteragtion??
No just add reference to System.Windows.Forms and write it in Using section.
but just question, did u add this dll for creating labels?!!! HTML and ASP.NET has label control, you don't need to add Windows.Forms to create control exists in asp.net!!

Technology is something that makes things easier, Why do you want to write code just to have a Form, you can just select a Win Form when you start your Project or Add it

cause vuyiswamb sometimes it takes rewritting some code to understand it, and learn what goes on behind the scences.

You're getting yourself a bit confused, Acidburn. ASP .NET and WinForms are two different worlds entirely. ASP .NET is strictly for web-based application development, meaning that everything you display to the user will be a webpage.

WinForms is like a more traditional application.

I know I'm not answering your original question (I am an ASP .NET dev), but if you try to mix up WinForms and ASP, you'll just get frustrated.

I understand what you mean, If you do it for understanding purpose its fine. but man there are a lot of things to learn in .NET than a code behind a winform ,its Simple if you have once done coding in java, you will understand that the code behind the winform , is just declaring a form , telling it how big should it be, where should it display when ran and more, there is nothing interesting about it, but still i understand what you mean.

cool

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.