0

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?

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
Sal-laS
  • 11,016
  • 25
  • 99
  • 169
  • There's not much that isn't possible, what data are you looking to record? You can write data to a buffer and flush it to a file on app close, or continually write data to the file during application runtime... I'd probably create a class to handle writing to the file and inject that into the forms – Charleh Apr 18 '15 at 10:06
  • 2
    Or are you taking about having a forms project with no forms? In which case what's the point of it being a forms project – Charleh Apr 18 '15 at 10:09
  • Application.Run expects Application context class..please see https://msdn.microsoft.com/en-us/library/system.windows.forms.application.run%28v=vs.110%29.aspx – Rohit Apr 18 '15 at 10:10
  • If you are already thinking about this kind of refactoring, you should look into making a class library and then using that library from a console application as well as the existing forms application (if required) – Sayse Apr 18 '15 at 10:14
  • possible duplicate of [how to change a c# console project to windows forms application project?](http://stackoverflow.com/questions/4079483/how-to-change-a-c-sharp-console-project-to-windows-forms-application-project) – Tomas Kubes Apr 18 '15 at 14:37

3 Answers3

1

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.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
0

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.

Michael Jaros
  • 4,586
  • 1
  • 22
  • 39
0

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.

Community
  • 1
  • 1
Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148