Ace Caserya
Ace Caserya

Reputation: 2923

calling windows form from console in C#

I'm using C# .net 4.0 VS 2010.

I copied the following code here in Stackoverflow and confirmed all from the reference that this is suppose to work, but i am having syntax error on my call on the "Application.Run(new ShoutBox());" the error is "The type or namespace 'ShoutBox' could not be found."

The project was originally build as a console application. I just recently added a windows form named ShoutBox and is saved as ShoutBox.cs. I have transfer my code to the form so it does not display stuff in console rather on a textbox of the windows form i created.

What did i missed? And how can i make it work?

    using System;
    using System.Windows.Forms;

    namespace ChatApp
    {
        class ConsoleApplication1
        {


            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();


                //this one works
                Application.Run(new Form()); // or whatever


                //this one does not work, error on second ShoutBox
                Form ShoutBox = new Form();
                Application.Run(new ShoutBox()); 
            }


        }
    }

Just for reference, here is my final working code: This code creates a new Shoutbox form instead of a blanked form.

    using System;
    using System.Windows.Forms;
    using ShoutBox; // Adding this

    namespace ChatApp
    {
        class ConsoleApplication1
        {        
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Form ShoutBox1 = new ShoutBox.ShoutBox(); //Changing this
                Application.Run(ShoutBox1);               //Changing this
            }
        }
    }

where my Shoutbox form are as follows:

    using System
    using System.Windows.Forms;
    namespace ShoutBox
    {
        public partial class ShoutBox : Form
        {
    ....

Upvotes: 3

Views: 3930

Answers (3)

Steve
Steve

Reputation: 216243

ShoutBox is the name of a variable referencing a Form, you can't call new ShoutBox().

You have already instantiated the form in the previous line, now you call simply

 Application.Run(ShoutBox); 

But, if you have a form called ShoutBox defined in this way

namespace ShoutBox
{
     public partial class ShoutBox: Form
     {
        .....
     }
}

then you need to add the using declaration at the beggining of your file

using ShoutBox;

or you can simply change the namespace used in the ShoutBox.cs file to the same namespace used in the program Main file

namespace ChatApp
{
     public partial class ShoutBox: Form
     {
        ....    
     }
}

Upvotes: 5

TcKs
TcKs

Reputation: 26632

The ShoutBox class is propably in different namespace.

And the code Form ShoutBox = new Form(); is useless, you need only Application.Run(new ShoutBox());.

Upvotes: 0

Simon Whitehead
Simon Whitehead

Reputation: 65049

You are missing either one or two things.

Firstly, you need to import the namespace that ShoutBox is in:

using Your.Namespace.Where.ShoutBox.Is.Declared;

An easy way to do this in Visual Studio is to put your cursor somewhere on the word ShoutBox and either press Alt+Shift+F10 or.. as some people who are much more productive than I am do, press Ctrl+.. This will bring up a menu that will show the namespace that needs to be included.

Also, if that namespace is in another assembly (you alluded to that), then you need to add it as a reference to the project.

Also, this:

Form ShoutBox = new Form();
Application.Run(new ShoutBox());

..is incorrect. I would suggest a tutorial on basic class creation.

Upvotes: 0

Related Questions