Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Windows Forms
  4. Structuring Windows Forms apps... [modified]

Structuring Windows Forms apps... [modified]

Scheduled Pinned Locked Moved Windows Forms
questioncsharpwinformscareer
12 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Luc Pattyn

    Hi, here is what I do: 1. I don't like region stuff, never use it; 2. sometimes I use partial classes; 3. sometimes I use inheritance, i.e. I create a base class with some of the functionality, derive another class adding (not replacing!) some functionality, derive from that one, and so on, even if all I need is one (or a few) instances of the most derived class. The advantage of (3) over (2) is you get better locality as part of the code and data is available only to part of the entire construct, as opposed to the many-files-with-a-single-partial-class where it soon starts looking like everything is global. :)

    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


    I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


    1 Offline
    1 Offline
    1 21 Gigawatts
    wrote on last edited by
    #3

    Hey thanks Luc, it's nice to know how others do it. :thumbsup:

    "If you can't explain it simply, you don't understand it well enough" ~ Albert Einstein "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." ~ Paul Neal "Red" Adair Now reading: 'The Third Reich', by Michael Burleigh

    L 1 Reply Last reply
    0
    • 1 1 21 Gigawatts

      Hey thanks Luc, it's nice to know how others do it. :thumbsup:

      "If you can't explain it simply, you don't understand it well enough" ~ Albert Einstein "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." ~ Paul Neal "Red" Adair Now reading: 'The Third Reich', by Michael Burleigh

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #4

      you're welcome. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      1 Reply Last reply
      0
      • 1 1 21 Gigawatts

        This may seem like a daft question, but as I'm not a professional C# programmer I think I can get away with it ;-) I have a windows form application with a number of controls on it (menu items, push buttons) along with internal methods etc. It’s not big, but like anything over time if I'm using it, mods will be added and it'll get bigger and fatter. What is the best way to keep this all neat and tidy, along with making it easy to navigate your way through the code? I mean, I know about ‘region’ elements, which I do make use of, but I still think that sometimes it can get a bit crowded and hard to find the things you’re looking for easily. Do you pro’s make use of the ‘partial’ keyword, and split things off into separate files to aid readability? Say, leave all the event methods in the forms main class file, and then split off certain things into other files? What would you guys do if you were writing a large WinForms app for a client? I appreciate that this is quite a vague question, so vague answers are fine :thumbsup: TIA :) [EDIT] I should make clear that I don't have all my code in just one file! That may not easily apparent from my initial post [/EDIT]

        "If you can't explain it simply, you don't understand it well enough" ~ Albert Einstein "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." ~ Paul Neal "Red" Adair Now reading: 'The Third Reich', by Michael Burleigh

        modified on Tuesday, December 8, 2009 8:35 AM

        V Offline
        V Offline
        vtchris peterson
        wrote on last edited by
        #5

        It's good that you're thinking about this now while your code base is small :) I find that Windows Forms code is pretty easy to keep tidy. If you feel like a given class is getting untidy, it's probably time to decompose it into simpler components. That said, don't over-decompose to start with as this creates unnecessary levels of indirection. Defer refactoring until it makes sense (e.g. if you want to re-use common functionality in multiple places). The best tip I can give is to be rigorous about namespace organization and use meaningful type names so that someone else (or you 6 months from now) can know where to start looking.

        1 1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, here is what I do: 1. I don't like region stuff, never use it; 2. sometimes I use partial classes; 3. sometimes I use inheritance, i.e. I create a base class with some of the functionality, derive another class adding (not replacing!) some functionality, derive from that one, and so on, even if all I need is one (or a few) instances of the most derived class. The advantage of (3) over (2) is you get better locality as part of the code and data is available only to part of the entire construct, as opposed to the many-files-with-a-single-partial-class where it soon starts looking like everything is global. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          R Offline
          R Offline
          Roger Wright
          wrote on last edited by
          #6

          Say, Luc, I have a related question, since you've been helpful on project I've been working on. When you are dealing with classes that represent real world objects that need to be handled in similar ways, do you define them in a separate file, or just inline where you need them? For example, the project I'm working on is intended to manage about 6 types of devices used in electrical substations. I'm only working on one type right now, because the techniques that work there will work for the others. I've defined the one type in the main program form, but I'm thinking it will be more convenient later to create a library file containing all of the devices and 'using' it whenever I need to later as the program develops. I really prefer to keep everything in one file (from past history with procedural programming) because it's easier to follow the program flow. But I can already see that this approach will be a nightmare with a Windows program if it grows beyond a few thousand lines. And it will...

          "A Journey of a Thousand Rest Stops Begins with a Single Movement"

          L 1 Reply Last reply
          0
          • V vtchris peterson

            It's good that you're thinking about this now while your code base is small :) I find that Windows Forms code is pretty easy to keep tidy. If you feel like a given class is getting untidy, it's probably time to decompose it into simpler components. That said, don't over-decompose to start with as this creates unnecessary levels of indirection. Defer refactoring until it makes sense (e.g. if you want to re-use common functionality in multiple places). The best tip I can give is to be rigorous about namespace organization and use meaningful type names so that someone else (or you 6 months from now) can know where to start looking.

            1 Offline
            1 Offline
            1 21 Gigawatts
            wrote on last edited by
            #7

            I think you've gone along the same lines as Luc (above) so that's another vote for decomposing. I'll have a look though the code and see where I can best split it up. Many thanks :thumbsup:

            "If you can't explain it simply, you don't understand it well enough" ~ Albert Einstein "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." ~ Paul Neal "Red" Adair Now reading: 'The Third Reich', by Michael Burleigh

            A 1 Reply Last reply
            0
            • R Roger Wright

              Say, Luc, I have a related question, since you've been helpful on project I've been working on. When you are dealing with classes that represent real world objects that need to be handled in similar ways, do you define them in a separate file, or just inline where you need them? For example, the project I'm working on is intended to manage about 6 types of devices used in electrical substations. I'm only working on one type right now, because the techniques that work there will work for the others. I've defined the one type in the main program form, but I'm thinking it will be more convenient later to create a library file containing all of the devices and 'using' it whenever I need to later as the program develops. I really prefer to keep everything in one file (from past history with procedural programming) because it's easier to follow the program flow. But I can already see that this approach will be a nightmare with a Windows program if it grows beyond a few thousand lines. And it will...

              "A Journey of a Thousand Rest Stops Begins with a Single Movement"

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #8

              Hi Roger, I don't have any strict rules for this; I don't start a new file for every new class (or type) I create; however I try and keep most files at less than 500 lines of code, using either separate classes, or partial classes, or inheritance as outlined before. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


              R 1 Reply Last reply
              0
              • L Luc Pattyn

                Hi Roger, I don't have any strict rules for this; I don't start a new file for every new class (or type) I create; however I try and keep most files at less than 500 lines of code, using either separate classes, or partial classes, or inheritance as outlined before. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                R Offline
                R Offline
                Roger Wright
                wrote on last edited by
                #9

                Thanks, Luc! :-D I moved the Recloser class to its own file and namespace, then modified the rest of the project to 'use' my new namespace. That file will grow, adding new equipment types as I expand the app, and defining them on the fly, wherever I happen to need them is going to get really ugly. This is much better, though it was a scary moment there when I commented out my original class definition on the main page and clicked Build Solution. It worked very well, much to my surprise.

                "A Journey of a Thousand Rest Stops Begins with a Single Movement"

                1 Reply Last reply
                0
                • 1 1 21 Gigawatts

                  I think you've gone along the same lines as Luc (above) so that's another vote for decomposing. I'll have a look though the code and see where I can best split it up. Many thanks :thumbsup:

                  "If you can't explain it simply, you don't understand it well enough" ~ Albert Einstein "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." ~ Paul Neal "Red" Adair Now reading: 'The Third Reich', by Michael Burleigh

                  A Offline
                  A Offline
                  Ashfield
                  wrote on last edited by
                  #10

                  These might help (or drive you mad as they do me!) StyleCop and FXCop - they are both free AFAIK

                  Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

                  V 1 Reply Last reply
                  0
                  • A Ashfield

                    These might help (or drive you mad as they do me!) StyleCop and FXCop - they are both free AFAIK

                    Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

                    V Offline
                    V Offline
                    vtchris peterson
                    wrote on last edited by
                    #11

                    Oh man, FXCop... that brings back (painful) memories.... I used it about 6 years ago on a team with lots of junior developers (and senior developers who thought they were above adhering to conventions). It was effective, but comes with a lot of overhead. On a team of competent developers, I much prefer a document describing the style convention rather than a tool that enforces it. Come to think of it, I prefer teams of competent developers outright :)

                    A 1 Reply Last reply
                    0
                    • V vtchris peterson

                      Oh man, FXCop... that brings back (painful) memories.... I used it about 6 years ago on a team with lots of junior developers (and senior developers who thought they were above adhering to conventions). It was effective, but comes with a lot of overhead. On a team of competent developers, I much prefer a document describing the style convention rather than a tool that enforces it. Come to think of it, I prefer teams of competent developers outright :)

                      A Offline
                      A Offline
                      Ashfield
                      wrote on last edited by
                      #12

                      I didn't say I would use it through choice :) Like you say though, for juniors in particular it can stop some bad habits (and introduce some different ones :laugh: ), so I thought it may be of interest to the OP

                      Bob Ashfield Consultants Ltd Proud to be a 2009 Code Project MVP

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups