RSS


[ Pobierz całość w formacie PDF ]
.Just add the following code:Property Let Backcolor(c As ColorConstants)Text1.Backcolor = cPropertyChanged  BackColorEnd Property'------Property Get Backcolor() As ColorConstantsBackcolor = Text1.BackcolorEnd PropertyNote that you must add the PropertyChanged method call whenever youchange a property of a user control.This passes this information to theVBengine to make sure that the screen is refreshed as needed.Copyright © 2001, by James W.Cooper 67In the same way, you can add methods to your user control, and they willappear in the syntax completion dropdown for any instance of that control.For example, the TextBox control lacks a Clear method.However, we caneasily add one to our new control:Public Sub Clear()Text1.Text = ""End SubNow that we ve added that convenient Clear method, we can connect it to theClear button:Private Sub Clearit_Click()hiText1.ClearEnd Suband clicking the button will clear our new text box.Compiling a User ControlOnce you have your test program and user control working, you can compileboth of them by selecting File | Make Project Group.This will produce an.EXE file for your test program and an.OCX file for your user control.Then,if you want to use this control in further programs, you will find that VB hasautomatically registered it and you can find it under Project | Components, toadd to any new project.SummaryBuilding a user control like this helps you see how encapsulation can be usedto create new objects that have the properties you need.The onlydisadvantage of this approach is that you must add all of the properties to thecontrol manually rather than having them inherited as could occur inlanguages that allow inheritance.However, in many cases encapsulation isvery effective, since you have only a few properties to pass through from theouter control interface to the enclosed control interfaces.And, if the newcontrol contains more than one basic control, this is the only possibleapproach.Copyright © 2001, by James W.Cooper 68Copyright © 2001, by James W.Cooper 696.INHERITANCE AND INTERFACESAs you begin to work more with classes, you soon come across programmingcases where you have classes that are rather like other classes you are alreadyusing in this program or another similar one.It seems a shame to just copy allthat code over again and have a lot of objects that are separate but verysimilar.In languages like Java and C++, you can derive new classes from existingclasses and change only those methods that differ in the new class, with theunchanged parent methods called automatically.VB6 does not support thislevel of inheritance, but it does provide interfaces and implementations whichallow you to produce related classes with only a small amount of effort.InterfacesIn VB, you can create a class containing only definitions of the methods andno actual code.This is called an interface definition.Then, you can createother classes which implement that interface and they all can be treated as ifthey were instances of the parent interface, even though they implement themethods differently.For example you could create an interface called Command which has thefollowing methods:Public Sub Execute()End Sub'------Public Sub init(nm As String)End SubThen you could create a number of Command objects such as ExitCommandwhich implement the Command interface.To do this, you create a classcalled ExitCommand, and insert the lineImplements CommandThen, from the left drop-down, you select the Command interface, and fromthe right dropdown, you create instances of the Execute and init methods.Then you can fill these methods in with whatever code is aprropriate.Private Sub Command_Execute()Copyright © 2001, by James W.Cooper 70'do somethingEnd Sub'------Private Sub Command_init(nm As String)'initialize somethingEnd SubThe advantage of this approach is that the ExitCommand class is now also ofthe type Command, and all of the classes that implement the Commandinterface can be treated as instances of the Command class.To see how thiscan be helpful, let s consider a program for simulating investment growth.An Investment SimulatorOur investment simulation program will present us with a mixture of stocksand bonds, and we can look at their growth over any time interval.We willassume that the bonds are all tax free municipal bonds and that the stocks areall ones with positive growth rates.The program starts with a list of seven stocks and bonds and an investmentnest egg of $10,000 to use.You can invest in any combination of stocks andbonds at any rate until all your money is invested.The initial program state isshown in Figure 6-1.Figure 6-1-An investment simulator at the start.You select investments by highlighting them, selecting a purchase amount,and clicking on the Buy button.Copyright © 2001, by James W.Cooper 71Once you have selected some stocks, you can enter any future date andcompute the total stock value and the total taxable income [ Pobierz caÅ‚ość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • nvs.xlx.pl