Re: Newbie Control Subclassing Question by Jack
Jack
Sun Mar 07 16:37:04 CST 2004
On Sun, 7 Mar 2004 16:49:58 -0500, "Jeff Grippe" <jeff@door7> wrote:
>Thanks in advance for the help.
>
>I'm still new to subclassing controls and I have what I believe is a very
>simple question. Where does the class code go so that I can use the new
>control on a form. For learning purposes I wrote a very simple variation of
>the label class that responds to the click event by reverting the colors.
>I'm putting the code here because it is short and I hope someone will tell
>me if I'm doing something silly.
>
>DEFINE CLASS ActiveLabel as Label
>PROCEDURE Click
> LOCAL TempRGB
> TempRGB = this.forecolor
> this.forecolor = this.backcolor
> this.backcolor = TempRGB
> this.refresh
>ENDPROC
>ENDDEFINE
>
>My question is where do I put this code so that I can use the ActiveLabel
>control on one of my forms using either AddObject in code or using the form
>designer. My immediate need is for using AddObject and doing it in code but
>I'd like having both answers.
You can either put the above code in a .prg file, or you can create
the class using the visual class designer.
If you create your class using the visual class designer, you can use
it either in code or by dragging the class onto a form. From the
Classes tab in the Project Manager, click on New, enter your class
name, make it based on Label, and select a class library (it doesn't
have to already exist). When the new class opens, double-click on the
Click method and add your code (minus the PROCEDURE and ENDPROC).
To use the class using AddObject:
1. Put the above code in a .prg file. In your code:
SET PROCEDURE TO nameofprocedurefile.prg ADDITIVE
Thisform.AddObject("somename", "ActiveLabel")
Thisform.somename.Visible = .T.
2. If you are using a class library:
SET CLASSLIB TO yourclasslibraryname.vcx ADDITIVE
Thisform.AddObject("somename", "ActiveLabel")
...
If you are using a fairly recent version of VFP, you can use NewObject
instead of AddObject - it allows you to directly specify the vcx or
prg file containing the class definition.
I personally find it much easier to use the visual class designer than
to build classes in .prg files.