4

As the question says, what is the reason for this? I sort of understand that it helps to separate the visual code (design) from the background code (what actually happens), but this makes it more frustrating for a new person that works with Visual Studio.

For example, you create a new application, with one form. You then get 3 files and a design. Form1.cs where the main programming is, Form1.Designer.cs where most of your visual representation happens, and another Program.cs which serves as entry point.

Why is it like this exactly? And is it possible to create a full working form program with all the code in one file, how would it look like? Or does VS specify that all three files must be separate?

The main reason I am asking this is because I would like to go right down to the basics, and see if it is possible to make a simple 100-200 line program just in a .txt file, and what exactly would I need to look out for.

EInherjar
  • 339
  • 6
  • 17
  • someone @Microsoft though its good idea. – dipak Jun 01 '17 at 12:33
  • You ask for opinions, so my opinion is that it is far better now than in the first days of WinForms in Net 1.1 where everything was stuffed in a single file. You can focus your mind on the real work and not on the plumbing – Steve Jun 01 '17 at 12:33
  • 3
    The designer code is the config file, if you like, for the designer - so if you manually edit this file you can easily break the designer. Having the file separate helps to prevent this breakage. – Enigmativity Jun 01 '17 at 12:34
  • 3
    If you've ever had to jump through the hoops of trying to preserve user-generated content when re-generating a tool-generated file, you'll realise how much pain and suffering existed before partial classes were created. – Damien_The_Unbeliever Jun 01 '17 at 12:34
  • 2
    It didn't used to work that way, back in .NET 1.x the designer modified the same file where the programmer put his code. You can sort of see the numerous things that went wrong with that. – Hans Passant Jun 01 '17 at 12:38
  • So the reason is that it is less likely to break your code if you change the visual elements? Is it possible to write the code in one file? Mainly the reason I am asking is, I want to know if it would be possible to create a Forms application in a .txt file. – EInherjar Jun 01 '17 at 12:40
  • Why would you want to do that? What is it you are trying to achieve? – Igor Jun 01 '17 at 12:41
  • You can create an Form in a class that you completely manage yourself. Painfull, but sometime required. I just did it last week for my Exception handling (all exception are shown in the same cute window) – Bestter Jun 01 '17 at 12:42
  • I am trying to write a Forms program simply in the notepad, nothing else. I know it is possible and fairly easy to write a console program in .txt file. – EInherjar Jun 01 '17 at 12:45
  • You can do that, nothing prevents you to write a WinForms program using a single file cs including in it the static Main function and all the controls initialization required. It is easy for a simple form with a button and a couple of textboxes. Things change for more complex scenarios. – Steve Jun 01 '17 at 12:49
  • @Steve Of course, I am not looking to write a whole program, I would just like to create a simple program with a few elements, but I find no documentation on how to actually combine all partial classes into one file. It can't be as easy as copy-pasting? I would just like to be pointed in the right direction. – EInherjar Jun 01 '17 at 12:52
  • I thought it was a good question =( – Mark C. Jun 01 '17 at 12:55
  • @EInherjar Partial classes are just a convenience. You can copy the methods from the designer.cs file inside the main form.cs file. As well the Main function from the program.cs. If you want to experiment quickly with this approach I suggest to download LinqPad and build and run a simple winform program – Steve Jun 01 '17 at 12:58
  • 1
    Back in old days this was exactly how winforms designer works: it was polluting **your** code with huge generated stuff and it was easy for beginner to accidentally break something. Now it's much better, when the code is split. Btw, you **shouldn't modify** it, only use designer to change something. My guess this is what make this question bad - there is no point of achieving what you want, unless you explain yourself. Why do you want it? What frustrates you? etc. – Sinatr Jun 01 '17 at 12:59
  • Edited a bit, hopefully it's enough. But basically, as relatively new person to programming, I would just like to know how exactly all of it works, how to write, compile and execute a program without VS. The only thing I'm doing right now with the designer is changing the names of some elements. – EInherjar Jun 01 '17 at 13:13
  • https://stackoverflow.com/questions/553143/compiling-executing-a-c-sharp-source-file-in-command-prompt –  Jun 01 '17 at 13:30
  • @EInherjar if you want to know how to write code in notepad, ask how to do that. *Why* VS puts things into partials isn't relevant to accomplishing that goal. These comments have answered that point a few times already, anyway. –  Jun 01 '17 at 13:31
  • Okay, I will check that out, thank you very much for the replies – EInherjar Jun 02 '17 at 08:25

0 Answers0