Results 1 to 7 of 7

Thread: General Coding Question

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Location
    California
    Posts
    11

    General Coding Question

    Not sure where to ask this question but here it is: I'm new to VB 2008- now when as an example, you load a Student Sample Program in order to modify it - now when I look at the code structure - there is always a PLUS mark and next to it says "windows generated designer code" - when I click on the PLUS mark it opens up a ton of code - Can any one explain to me what all this code represents? thanks.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: General Coding Question

    Welcome to the forums.

    That is code generated by the Forms Designer.

    Where did you get this project?

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: General Coding Question

    If you look at that ton of code, you will see a few items that should look familiar: All the controls are declared in that section, and all the controls are instantiated in that section. Both steps could have been done at once, but for whatever reason, the declaration and creation are separate, thereby doubling the size.

    What else is in there? You should see sections for each control that sets all the properties for the control, and somewhere you will see a section that adds the control either to the form control collection, or to the control collection of whatever control contains it (such as a panel, groupbox, etc.)

    Look it over and the patters within it will become obvious.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Location
    California
    Posts
    11

    Re: General Coding Question

    These are student sample programs from textbook - I know they are Windows Forms Designer Code - But there is so much of it - I cant make sense of it. So this is all the code already written in the sample program? Why so much? thanks

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: General Coding Question

    Hey,

    The Windows Form Designer Code region, is simply the code that is generated each time you change something on the design surface.

    For instance, if you add a button, and change the text to "hello" then the code that is necessary to do this is added within this region.

    There is so much code, because dragging and dropping a button onto the form surface requires a lot of work. For instance, you need to instantiate the button, you need to set it's size, it's location, it's colour, it's text, you need to hook up any event handlers that are created for the button, etc, etc. All of these things are done easily through the designer simply by clicking on, and changing the different properties. If you were actually to do all of those things by hand, then you would see that it does require a lot of code to do it.

    With the advent of .Net Framework 2.0, partial classes were introduced. You should find that all the Windows Forms Designer code is placed in one partial class, and another partial class is created in which the developer code is normally written, i.e. the Form Load event, Button Click events etc. Then, when the program is compiled, these partial classes are brought together and all the code executes.

    Bottom line, you don't really need to worry about the code that is in that region, and that is why it is hidden away in a partial class out of sight. Very rarely, if at all, do you need to change something in there. All changes are handled through the designer for you.

    Hope that makes sense!!

    Gary

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: General Coding Question

    Gary touched on the pint - Partial Classes. If this project was originally written in VB 2008 then there wouldnt be any "Windows Designer Generated Code". It would be linked in a partial class instead and not visible in your code unless you clicked the "Show all files" button. So perhaps this project was written in 2002/2003 and upgraded?

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: General Coding Question

    The code can be there, even in 2005-2008. Partial classes generally hide it all away from the user, but there are still instances where it is not hidden in a partial class (some code generators). Still, it can generally be ignored.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width