p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   Visual Basic 2005 Basics (http://p2p.wrox.com/visual-basic-2005-basics-137/)
-   -   Reuse Forms and part s of code (http://p2p.wrox.com/visual-basic-2005-basics/66299-reuse-forms-part-s-code.html)

thillai February 29th, 2008 06:46 AM

Reuse Forms and part s of code
Hi all,
I am learning to program in Visual basic net 2005 after retirement as a hobby! As a start I am trying to write a maths, spelling and find words program all from a main part menu driven form to my grandson who is six.
After writing the maths part, I find I can reuse the same form with little modification and also reuse some functions in the other part ie spelling.
1)Is there any way I can reuse the form and modify slightly for use in the other part.
2) Will I be able to reuse the same code for the functions and how.
I would be greatful for any help and suggestions.
Thanks in advance.

urtrivedi February 29th, 2008 07:36 AM

It is very great to know that you are learning programming, otherwise people even hesitate to learn simple computer operation at your age.
On form you can use radio buttons,combo box & check box for choices.
for some repetitive code you can define one function which you can call from any form in your application. I think books will be more helpful in your case rather than any forum, If you find any problem then do post your problem.
Best of luck.


Help yourself by helping someone.

Ankur_Verma February 29th, 2008 09:46 AM

Ya Thillai, Upendra said it there. It really is heartening to see what you are doing for your grandson, let me warn you though, you might soon find your grandson egging you on to learn game programming :). There are many ways to approach this task, you can define a class with all the logic in it and access its functions from the forms, dynamically add/remove controls, inhering your forms from ones you have already designed, all depends on how much programming you wanna do and how much you wanna learn. Do write back and let us know which approach you are taking, and if you are facing problems.


thillai March 2nd, 2008 12:26 PM

Hi all
Thanks for the replies and special thanks to Upendra and Ankur for your sentiments, I am happy to learn any amount as I am interested in programming and to keep myself occupied at the same time. So i thought this simple project to write so i can learn all along.
The form i have already made is one where there are textboxes, labels and buttons. Information on labels and textbox is for information to the user. The text in the textbox will change from one form to the other. Information collected from other textbox will also change from form to the other. I just thought without redrawing another similar form I may be able to reuse one with changes. So was my question.
Also some codes in functions like checking for validity of textbox entry can be reused, so was my second question.
Thanks for the replies but i do not seem to grasp what you have said there, may be my age! Can you please be more specific and help me. I am not able to grasp the class definition so far.
Thanks for further help and if you like i can write to you an email to correspond if you ok it only.
Thanks again

dparsons March 2nd, 2008 01:33 PM

Upendra and Ankur have alluded to this but you should definately pick up a book about programming with Visual Basic. To build on what Ankur said with regard to classes, this is where you will find alot of programmers place code that they call from various places throughout an application.

For example, let's say that in your application once the user has clicked a button you check that several textboxes do not contain an empty string. You could write an if statement for each of the textboxes:

If textbox1.Text = "" then 'do something
If textbox2.Text = "" then 'do something

Or you could create a class that does this for you:

Public Class foo
    Public Shared Function isValid(ByVal value As String) As Boolean
        If value = "" Then
            Return False
            Return True
        End If
    End Function
End Class

so now to check the value of each of your textboxes you would make a call similar to this:

if Not foo.isValid(textbox1.Text) then 'do something
if Not foo.isValid(textbox2.Text) then 'do something

Granted, this is an overly simplistic example but it demonstrates the methodology very well because now, through out your entire application, everytime you need to check if a textbox contains a value you can call this central class 'foo' which will do the evaluation for you; your application simply needs to handle the Boolean value coming back from the function.

In so far as not redrawing the form, that depends. If the form elements are the same for each form then you could add a combo box to the form that could change the 'mode' of the form. In your case the combo box would contain: Math, Spelling, and Word so, theoretically, when the user clicks the 'Complete' button your event handler could look something like this:

Protected Sub btn_Click(ByVal sender As Object, ByVal e As eventArgs) Handles btn.Click
     Select Case cmb.Text
         Case "Math"
         Case "Spelling"
         Case Else
     End Select
End Sub

This should be enough material for you to continue working on your app but I emplore you to purchase a book that can address not merely HOW to do something but WHY you should be doing it.

Wrox has many books that address this topic:

From that list I would suggest:

Alternatively you can purchase a subscription to Wrox's online library which gives you access to a large portion of the Wrox Collection (the book mentioned above is in there as well): http://wrox.books24x7.com/ it costs 49.99 for a quarter or 175.00 for an entire year. You can get more information regrading this service here: http://www.wrox.com/WileyCDA/Section/id-130024.html


================================================== =========
Read this if you want to know how to get a correct reply for your question:
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========

thillai March 4th, 2008 09:40 AM

hello Doug,
Thanks for the explanation and guidance on class definition and I will persue this path and learn more.
Thanks again

All times are GMT -4. The time now is 03:25 AM.

Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.