General windows forms design question...

Assume that you are designing a windows form that displays the details 
(name, address, etc.) of a selected entity for editing by the user.  When 
the form is instantiated, the constructor is passed the entityID of the 
entity to be displayed.  Some of the tasks that must be completed in order 
to be able to initialize the form include:

1.  Retrieving the entity from the database (based upon the entityID)
2.  Bind editable properties of the object to controls on the form
3.  Set control or form properties (such as form caption text, etc.)
4.  Possibly doing some role-based security checking to set some controls to 
read-only based upon the authenticated user's role.

Considering that there are several places where the initialization code to 
accomplish these tasks can be placed, such as:

1.  Form constructor
2.  Activate event handler
3.  Load event handler

Are there any best practices or recommendations regarding how to distribute 
the code to perform these functions

Thanks!!!


Answer this question

General windows forms design question...

  • poopiman

    Actually, I am looking for guidelines on the best way to distribute initialization code within a windows form.
  • TheKingKev

    Assuming the client has a good reason for instantiating the form I'd do all that in the constructor.  I'd also supply public methods in case the client decides to declare the form as a class variable (and so does not always dispose of the form within the calling method)  If the form is wrapped in a class then the properties and methods which handle this should be intuative to the client.
  • shron

    not sure what you mean... long question only confuse me :) heheheh

    you mean pass an ID from one master form to the detail form

    if so I do this .. 

    in the detail_form next line after the inherits commands

    public id as interger


    that way you can refrence this althrough your code.


    but on your master you would have something like this

    dim newfrm as new detail_form
    newfrm.id = 1  '(this is the value that you want to send to the detail
    newfrm.show


    I could be wrong.. but thats what I do.


    is this  what you wanted to know






  • Kelly Dyjur

    I would say that of the options you listed 1 and 3 are the ones to concentrate on; I probably wouldn't use Activate for any of the items you listed.

    Now, as for choosing between Sub New() and Form.Load()...

    If you are designing a form to be reused by others, I would follow the suggestions made by bloke above.

    If the form is just for this project, I would use the following guidelines to decide which to use:

    Use Sub New()
     - If the objects created or properties set could be useful when the form is instanciated but not yet shown

    Use Form.Load()
     - If the objects created or properties set will not (or cannot) be used until the form is displayed

    For instance, lets say we're going to call this form frmEditDetails and we are going to display it from the main form called frmMain.  We might do step one from your list (retrieve values from the database) in Sub New() so that when we create the new instance of frmEditDetails, the data to display is available.  Before we show the form, we may want to use other values of frmMain to decide what the RowFilter will be for the data to display.  So by using Sub New() we can work with our form's data before we call Show().

    In contrast, item 3 from your list (setting caption text) isn't relevant to a form that hasn't been displayed yet, so we can save that code for Form.Load().

    That's my $0.02!

  • mr. robot

    I am not sure I understand what you mean by, "Assuming the client has a good reason for instantiating the form..."

    Is there a specific reason that you would do the initialization tasks in the constructor rather than in the Load event handler   I am leaning in that direction myself, but I am wondering if my reasons are similar to yours.

    I am also wondering if there are any "best practices" guidelines for initializing windows forms in general.

    Thanks!

  • monsoondawn

    "Assuming the client has a good reason for instantiating the form..."   As opposed to instantiating the form just in case the user wants to use it.  If that was the case I'd do next to nothing in the constructor and create a public method (within the form) which then did all the work and displayed the form.  
    MyForm frm = new MyForm();
    frm.ShowUser(UserID)
    I'd use the constructor because that is the point where the object is being initialised where as Form Load is more presentation related.  You'll find some guidelines here:
    http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpgenref/html/cpconnetframeworkdesignguidelines.asp
    But I'd don't think they specify what your after.

  • General windows forms design question...