Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

good techniques: new form or reuse form ?

Status
Not open for further replies.

kunmun23

Programmer
Oct 28, 2003
17
0
0
IN
hi all,
I am developing a front end in VB6 ...I have certain forms that have almost the same controls (text boxes, buttons etc.) but the functionality of the new form will vary. For instance the behavior of a button click will vary..or it may have an extra checkbox etc.

I need expert advise on which is the recommended approach

APPROACH 1:
Create a separate form for each individual functionality. This produces cleaner and simpler code, easy to understand and debug and free from undesirable effects of the reusable forms.

APPROACH 2:
Use the same form and add functionality to it...flags may be used to decide how the form behaves...for example depending on a certain boolean flag i can decide what the button click event does.
This produces complex and messy code. On the other hand, since I've heard that VB loads all the forms at run time, this will have a smaller memory foot print (due to less number of forms) and would make the application run faster.

APPROACH 3:
Try to combine the best of the above two approaches..i.e. use a separate form for each functionality to produce cleaner code. Then in the main forms Load event, immediately unload all the forms that are not needed...loading them explicitly through code as needed.
This way the memory usage for the code will be smaller and the code will run faster...

I may be wrong in certain of my assumptions above...and thats why this post..please correct me and help me decide the best approach for my code.

Thanks.

PS If I'm not clear in my explanation please post..and I'll try to explain it better.
 

Approach 4:
Place all code that is the same in a module. Then either use the flag approach (still tends to be messy code) or better yet use the seperate forms only to contain the unique parts of the code you need.

From the sounds of it you are using an MDI application design. If this is so then make sure that the you read up on the AutoShowChildren (set it to false so that the child forms are not automatically loaded).

Welcome to TT

Good Luck

 
well..thats exactly the point...i could do what you suggested above ...but i don't want to create a messy code that would be hard for a person who takes up my position in future to understand and maintain....

i understand as a programmer that there are a lot of nifty tricks that I could use to reduce the lines of code and still make it run faster...

but I feel that in the current IT scenario, we should be more concerned about the clarity and maintainability of the code even at the cost of slightly sloppier code ( redundant line of codes)...

I want to know...whether the approach 3 I described above is of any use at all...would my code run slow if I have say...20 forms in my project ? can i improve the code's performance using Approach 3 ?

oh and its not an MDI application....just a front end for an MS access database using ODBC...its more like a complete suite consisting of lot of forms..that provide only the required database functionality of the database thru simple GUIs...consists of forms for data entry...editing....query results shown in datagrids..etc.

thanks for another suggestions...I love TT !!

 
btw...how do I edit my posts..to correct the grammatical errors I made above :p ??
 

Last question first ... you can't! Proof read first (use word and cut and paste if you feel you need to).

Ok, since you are not using an MDI application then the only forms that are loaded are the ones you tell VB to load (unless say your start up object is Form1 then that is loaded automatically). So if you have form1 loaded and want to load and use form2 you usually do it with a show statement. When you are done with form2 and hit the "OK" button (or whatever) you would have the Unload Me statement which will unload the VISIBLE elements of the form leaving the code behind the form in memory (see help, unload statement).

Then to answer you question more directly yes it wouldn't hurt if you unloaded unneeded forms. This will definitly reduce the graphical requirements and memory allocated for such.

Now back to my suggestion. If there are several or a couple of forms that use the same code sequence i.e. update table, query table, or validation of controls(1) ect. you can place this code in a module and call from each of the forms that need it, thus reducing the amount of code needed (and retained in memory) for each of those forms that have been loaded.

(1). If using two similar forms that require a fairly large amount of similar controls to be validated it is easier if the controls have the same name between forms. Then all you need to do is pass a reference of the form to the validation routine. Then where the forms differ you would have that part of the validation contained within the form.

I hope some of that helps

Good Luck

 

kunmun23

Did any of that help??? If not maybe someone else will read this and help you.

Good Luck

 
Hi all,

Do seperate forms and reuse the most code possible, with this version of VB its the most recommendable, you can reuse easy a form with a 100% OO language like vbasic.net, meet OO, you will love it.

--
Luis MX
 
vb5prgrmmr yes it helps a lot..I finally decided to create new forms and reuse the common code from the old forms...
 
I once made an application that creates up to 250 different controls at run time, The code is very simple because I gave each type all the same name and distinguish between them by a different index number.
I had a cluster of labels in a borderless frame so that all I had to do was to duplicate it and position only the frame.
Code was much simpler using only 1 sub_click for all command buttons with select case index to distinguish which control was prssed or usong the index in a formula.
Because the index is different I can place them where I want by referring to a Position() array like a look up table initially loaded from the MDB.
MyControl.Top=Position(Index,0)
MyControl.left=Position(Index,1)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top