8 Replies - 2445 Views - Last Post: 23 May 2015 - 08:50 PM

#1 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 125
  • Joined: 10-August 12

structuring multiple c# forms with main program

Posted 06 April 2015 - 07:05 AM

For projects with multiple forms, what is the "best" way to structure the code? By this I mean how do you know what code to put on the form (form.cs) or what code to put in the main program (program.cs). I feel like having to pass data back and forth between constructors and such becomes confusing easily, but it seems safer than keeping the majority of code in the form (I'm guessing static forms aren't a good approach). I understand it will probably depend on what is being implemented, however, I was wondering if there is a general rule to go by when deciding where to write different parts of the code.

Is This A Good Question/Topic? 0
  • +

Replies To: structuring multiple c# forms with main program

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13493
  • View blog
  • Posts: 53,898
  • Joined: 12-June 08

Re: structuring multiple c# forms with main program

Posted 06 April 2015 - 07:12 AM

Generally keep what ever functionality a given object needs in its own class. Keep your object interaction tight and controlled, and avoid making any interaction a breaking requirement (as in if form A and B interact if form B is not there A still, mostly, functions).
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,374
  • Joined: 02-June 10

Re: structuring multiple c# forms with main program

Posted 06 April 2015 - 08:13 AM

Quote

For projects with multiple forms, what is the "best" way to structure the code?

By leaving WinForms back in the 1995-2002 era where it was the best option available. Today employers expect you to build new application in WPF.

The rule for both WinForms and WPF remains the same:
The GUI is an interface between the User and the Logic. Your data and logic classes should not be dependent on the GUI. They should work with almost no changes if wired up to a console app, WinForms, WPF, web app or completely headless as a service. Basically the UI talks to a layer between it and the logic. The logic talks to the layer between it an the UI. That layer in between can then be updated as new UI technology is invented without having to change your logic. In some cases this is MVC: Model View Controller. In WPF we have MVVM: Model, View, ViewModel. But its really the same concept in general terms. The 'model' is your data: A coded model of a thing. The view is just that, the view or UI, the ViewModel is the layer between.

  • If you're interested in how this is done in this decade study up on MVVM. There are links to tutorials on transitioning from WinForms to WPF in my signature block.
  • If you're looking to make applications using WinForms for Win98 see FAQ 6 below





TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 6: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated March 2015
Spoiler

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13493
  • View blog
  • Posts: 53,898
  • Joined: 12-June 08

Re: structuring multiple c# forms with main program

Posted 06 April 2015 - 08:18 AM

Quote

WinForms for Win98

Let's bring that back to reality. Winforms operate on Windows 8 and 7.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,374
  • Joined: 02-June 10

Re: structuring multiple c# forms with main program

Posted 06 April 2015 - 08:38 AM

Sure it operates on Windows 8, even Windows 10. So will a console application. I just don't see anyone hiring for doing either.

My default stance on people asking questions in the C# forum because they are learning coding today is an ASSUMPTION they want to become developers in today's desktop/laptop/mobile market in the next 6 months to 4 years depending on their graduation date.

Again I ASSUME that although there are niche markets for things like embedded software, systems running on older platforms even today and so on, most people learning today probably want to take advantage of today's technology. Users have come to expect pretty and customized UI's, metro friendliness, Windows Store compatibility, etc. Multi-point touchscreens are quickly becoming common place and people already expect to be able to swipe and pinch and so on. By the time today's noobie that is asking questions is actually out in the world looking for a job they're going to have to be able to code for gestures and so on.

So can a WinForms application run on today's Win7/8/10 operating system. Sure can. I run Win10 right now and have WinForms applications that work fine on it. But do they look and behave as a dated WinForms applications? Again 'yes.'

Does my stance fit every noobie asking a question? No. Those that aren't interested in making modern applications that take full advantage of today's computers probably know who they are. They know they are looking to do something different and probably ask questions accordingly.

When I see someone asking about WinForms I ASSUME they simply don't know how much that don't know and that includes not knowing their school is 10 years behind the curve because they don't want to update their curriculum. I could just answer the question by pointing them at FAQ 6 and leave it at that. But I think there is an inherent duty to let someone asking about WinForms know that it is a legacy technology now, and will be even less desired by employers by the time they graduate.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13493
  • View blog
  • Posts: 53,898
  • Joined: 12-June 08

Re: structuring multiple c# forms with main program

Posted 06 April 2015 - 09:06 AM

Today's market still does include winforms, and your statement that they were only for win98 was just silly.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,374
  • Joined: 02-June 10

Re: structuring multiple c# forms with main program

Posted 06 April 2015 - 09:20 AM

Lively conversation... That's what forums are all about.

We all have opinions. Mostly formed by our backgrounds and experience. You think my statement is silly. Cool. From your position it is silly. I think its silly for anyone in school, halfway into the second decade of the 21st century, to be learning WinForms. With 500,000 users here on DIC I'm sure some others will chime in with their ideas of silliness. None of us have a crystal ball. I predict that it won't be too many more years before the average Windows coder won't know WinForms unless they were hired to specifically maintain legacy applications or to dissect legacy applications in order to make modern replacements. But hey, I could easily be wrong. Maybe the desire to use things like Xamerin frameworks to build OS-agnostic applications will take control and we'll all go back to forms. It could happen.

Also, I readily admitted already that WinForms will run on Win7/8/10. I meant only to imply that WinForms is a product of the Win98 era and while still forward functional it leaves an application looking like it came from 1998. But hey, retro is hot in a lot of areas from fashion to car design. So why not retro looking applications?
Was This Post Helpful? 0
  • +
  • -

#8 graFight  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 125
  • Joined: 10-August 12

Re: structuring multiple c# forms with main program

Posted 07 April 2015 - 09:32 AM

I know winform applications are probably not being used as much as many better alternatives are out, but I'm more concerned with the concept of a successful model of structuring code for apps similar to winforms (event driven with multiple classes/forms/activities and a GUI layer). I'm really only using winforms because of the requirement of the assignment, however, now I'm thinking about switching it to WPF. Thanks for all the information from everyone.
Was This Post Helpful? 0
  • +
  • -

#9 click_here  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 300
  • Joined: 25-November 13

Re: structuring multiple c# forms with main program

Posted 23 May 2015 - 08:50 PM

To answer your question: Do a little research on MVC as an introduction to software architecture
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1