Sal-laS
Sal-laS

Reputation: 11649

removing a form

I am manipulating a C# code, and going to remove all the Forms in the project and record output in a simple *.txt file.

Already, the code is started in this way:

   Application.Run(new Form1());

Since, I am going to remove the Forms, I tried to replaced it with a something else:

   Application.Run(new MakeFile());

That MakeFile class is responsible for creating a file, and recording the output on it.

The error is:

cannot convert from 'project.MakeFile' to 'System.Windows.Forms.ApplicationContext

The question is:

Is it possible to have a Form Application Project and records its data in a File?

If yes, what should i do?

Upvotes: 0

Views: 722

Answers (3)

Tomas Kubes
Tomas Kubes

Reputation: 25108

Delete what you have

 Application.Run(new MakeFile());

And replace it by

MakeFile makefile = new MakeFile();
makefile.DoWork();

And switch from Winform application to Console application in the project properties according to the answer of jgauffin bellow. It will work even without switching, but switching to Console Application will change the header of compiled executable and it will affect calling your app from cmd.

Upvotes: 0

jgauffin
jgauffin

Reputation: 101150

You can remove all forms without problems.

Then you need to tell Visual Studio that you want a console application instead. Open app the project properties and change "Output type" to "Console Application":

enter image description here

Finally remove all Forms specific startup code (lines below) from Program.cs:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());

.. and just invoke your new class that generates the text files.

Upvotes: 1

Michael Jaros
Michael Jaros

Reputation: 4681

Your use of Application.Run requires a Form or ApplicationContext object, so you can't just pass an instance of another class.

Check the invocation of that method without arguments.

Upvotes: 0

Related Questions