Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Subclassing OptionButton
Message
From
06/10/1998 11:26:20
 
 
To
05/10/1998 10:49:36
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
General information
Forum:
Visual FoxPro
Category:
Classes - VCX
Miscellaneous
Thread ID:
00143846
Message ID:
00144229
Views:
18
>>>>Am trying to add an OptionGroup to my class library and want to incorporate my own properties & methods with the associated OptionButtons.
>>>>
>>>>However the OptionButton is not one of those available for subclassing in the Class Browser.
>>>>
>>>>The ButtonCount of the OptionGroup will vary each time I add my new class to a form. Will I have to create the OptionGroup class with the maximum number of Buttons forseen and then change the ButtonCount property to a lower number ? Or is there a better way ?
>>>>
>>>>- Werner
>>>Werner,
>>>You could subclass the "buttons" nonvisually.
oForm = createobject("Form")
>>>with oForm
>>>	.addobject("MyoptGrp","Optiongroup")
>>>	with .MyoptGrp
>>>		.buttoncount = 5
>>>		for ix = 1 to 5
>>>			.removeobject("Option"+ltrim(str(ix)))
>>>			.addobject("Option"+ltrim(str(ix)),"myoption",;
>>>				"Hello"+ltrim(str(ix)), ix)
>>>		endfor
>>>		.setall("Visible",.t.)
>>>		.autosize = .t.
>>>		.visible = .t.
>>>	endwith
>>>	.addobject("myquitter","myquit")
>>>	.myquitter.visible = .t.
>>>endwith
>>>oForm.show
>>>read events
>>>
>>>define class myoption as optionbutton
>>>	fontname = "Arial"
>>>	fontbold = .t.
>>>	fontitalic = .t.
>>>	autosize = .t.
>>>	procedure init
>>>	lparameters tcCaption, tnPos
>>>		this.caption = tcCaption
>>>		this.left = 1
>>>		this.Top  = tnPos * (this.height + 2)
>>>	endproc
>>>
>>>	procedure click
>>>		=rand(-1)
>>>		this.forecolor = rgb(int(rand()*255), ;
>>>	                         int(rand()*255), ;
>>>	                         int(rand()*255))
>>>	endproc
>>>enddefine
>>>
>>>define class myquit as commandbutton
>>>Autosize = .T.
>>>Caption="Close"
>>>procedure init
>>>	this.left = this.parent.width - this.width - 2
>>>	this.top  = this.parent.height - this.height - 2
>>>endproc
>>>procedure click
>>>    release all
>>>	clear events
>>>endproc
>>>enddefine
Cetin
>>
>>Thanks, and I understand your suggestion, but I want to be able to repeatedly use my OptionGroup with my OptionButtons in the Form Designer. Is there a way to programatically add classes to a class library and make them a permanent part of that library ?
>>
>>- Werner
>Yes Werner. Class libraries could be prg too. And having them during form design is nearly what builders are. A very fast and small example of the above optionbutton as a builder (called manually from commandwindow to keep it simple) :
*myoptiongroupbuild.prg
>* Maybe an interface here to get buttoncount etc
>aselobj(aOptionGrp)
>with aOptionGrp[1]
> .buttoncount = lnButtons
> for ix = 1 to lnButtons
>    .removeobject("Option"+ltrim(str(ix)))
>    .addobject("Option"+ltrim(str(ix)),"myoption")
> endfor
> .autosize = .t.
>endwith
>
>define class myoption as optionbutton
>* PEM code
>enddefine
Select an optiongroup in designer and run this from command window. Here the key is aselobj() which provides a way to reference objects at design time. If you register this to builder.dbf it would be a builder also (would need 3 dummy parameters for builder.app).
>Cetin

Got the above to work with some minor modifications (such as the names of the buttons being removed and added in the FOR...ENDFOR loop cannot be exactly the same), BUT after saving the form and reopening it, the buttons revert to the standard VFP class. This happens whether I use the standard VFP optiongroup as a starting point or whether I use my own optiongroup as a starting point in the form designer. It's as though VFP won't let me substitute my own optionbuttons for the standard optionbuttons.

In the meantime I've created nine optiongroup classes with from 2 to 10 buttons incorporating the event coding I wanted. Unfortunately I'm not able to add my own properties to the buttons, but can work around this limitation.

- Werner
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform