Thanks Tracy - very helpful as usual! Essentially to make my code work you added the objects to the container and then configured them instead of trying to configure them before adding them to the container. Your solution is so much more elegant!
Now some additional questions:
Is it essential to have a separate method to Add Controls or can it be done in the Init method? (The separate method in the original code was added to try to eliminate the error.)
Should I be able to run code that I copy from the View Code button in Class Designer? Would posting it here let others help determine whether my errors are a result of a corrupted system or my own bugs?
Thanks in advance, Joe
On Wed, Jul 10, 2024 at 11:43 PM Tracy Pearson tracy@powerchurch.com wrote:
Hi Joe,
VFP isn't able to add an existing object to the form to be displayed. It instantiates an object on the form with the AddObject or NewObject commands. When you add an object this way it will not display until you set the Visible property.
I have two examples below.
Here is how that code could be written: * Create a blank form loMyForm = CREATEOBJECT("Form")
MESSAGEBOX("Form created successfully") * Add the spotpick1 to the form loMyForm.AddObject("SpotPick1", "spotpick1") loMyForm.SpotPick1.Visible = .T. * Show the form, in a Modal state loMyForm.Show(1) RETURN DEFINE CLASS spotpick1 AS container WIDTH = 100 PROCEDURE Init * Add your custom logic here (e.g., create controls, setproperties) THIS.AddControls() ENDPROC
PROCEDURE AddControls * Add a textbox to the container THIS.AddObject("Textbox1", "TextBox") THIS.Textbox1.Value = "Hello from TextBox 1" THIS.TextBox1.Visible = .T. * Add a second textbox to the container THIS.AddObject("Textbox2", "TextBox") THIS.Textbox2.Value = "Hello from TextBox 2" THIS.TextBox2.Visible = .T. THIS.Textbox2.Top = 30 ENDPROC ENDDEFINEHere is an example of defining the classes in a prg and using it in a form class two ways
* Create class form loMyForm = CREATEOBJECT("FormClass") * Show the form loMyForm.Show() RETURN * Define the customized container DEFINE CLASS spotpick1 AS container WIDTH = 200 ADD OBJECT Textbox1 as TextBox WITH Value = "Hello from TextBox 1",Width = 200 ADD OBJECT Textbox2 as TextBox WITH Value = "Hello from TextBox 2", Width = 200, Top = 30
ENDDEFINE * define the customized form DEFINE CLASS formclass as Form WindowType = 1 ADD OBJECT spotpick1 as spotpick1 PROCEDURE init This.AddSecondContainer() ENDPROC PROCEDURE AddSecondContainer This.AddObject("spotpick2", "spotpick1") This.spotpick2.top = 100 This.spotpick2.visible = .T. ENDPROC ENDDEFINEHTH, Tracy
-----Original Message----- From: ProfoxTech [mailto:profoxtech-bounces@leafe.com] On Behalf Of Joe Yoder Sent: Wednesday, July 10, 2024 10:02 PM To: profoxtech@leafe.com Subject: Class code issue
I am developed a custom class based on a container that worked properly when it was on a form but when I converted it to a class I have issues I can't resolve. I have only one installation of VFP and sometimes I wonder if it might be corrupt.
In order to avoid any corruption of the visual tools I have attempted to come up with a small .prg program that creates a form, defines a custom class in code, and puts an instance of the class on the form. ,The following code was written by Microsoft's CoPilot but the author has not been able to make it work! The problem seems to be that the definition code needs to reference the class before it is instantiated.
- Create a blank form
loMyForm = CREATEOBJECT("Form")
MESSAGEBOX("Form created successfully")
- Create an instance of your spotpick1 class
loSpotPick1 = CREATEOBJECT("spotpick1")
MESSAGEBOX("Class created successfully")
- Add the spotpick1 to the form
loMyForm.AddObject("SpotPick1", loSpotPick1)
- Show the form
loMyForm.Show()
RETURN
DEFINE CLASS spotpick1 AS container PROCEDURE Init * Add your custom logic here (e.g., create controls, set properties) THIS.AddControls() ENDPROC
PROCEDURE AddControls LOCAL oTextbox1, oTextbox2 oTextbox1 = CREATEOBJECT("TextBox") oTextbox2 = CREATEOBJECT("TextBox") oTextbox1.Value = "Hello from TextBox 1" oTextbox2.Value = "Hello from TextBox 2" THIS.AddObject("Textbox1", oTextbox1) && Errors here THIS.AddObject("Textbox2", oTextbox2) && Errors here ENDPROCENDDEFINE
Thanks in advance for any help! Joe
--- StripMime Report -- processed MIME parts --- multipart/alternative text/plain (text body -- kept) text/html
[excessive quoting removed by server]