Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to add program code to events if the object has already been created

Status
Not open for further replies.

sashaDG

Programmer
Jun 20, 2022
112
BY
Good day. The Form designer has a Grid with a columncount = -1 property.
In GRID.init I create a cursor that I display. Is it POSSIBLE at this stage to set the code programmatically in Grid.column1.header1.click()?

When I change the columncount and paste the code in Grid.column1.header1.click() everything works, BUT I have a search for records where the cursor is recreated and DEBUG happens text1.destroy(), header1.destroy().

And nothing works, because the header is different, do I understand correctly?
 
I'm not sure what you mean by "DEBUG happens". But the short answer to your question is that you cannot add code to an object that is created at run time.

What you can do is to create a custom column class at design time. Add the required code to the column's header's Click event. Then, at run time, set the the grid's MemberClass property to point to the custom column class.

For more information, read the Help topics, "Member Classes (Visual FoxPro)" and "MemberClass Property".

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike has given the best advice when it comes to Grid column headers. Member classes apply to some more controls, but on the most general level you may have noticed that in fact many classes of VFP offer the methods ReadMethod() and WriteMethod() which suggest you can do such things at runtime.

In short, you can when your code is a builder, which puts VFP in a mode that's closer to the design time mode, in which an object of the class is created without executing its Init() code, besides other differences. So that object can be modified and the class definition finally can be updated. And in short: No, it wouldn't be a good idea to use this mode in a runtime environment.

The solution to have the code where it should be, within the click method of the header is not only possible with member classes, though. You could also design a grid in a form and let its column headers have click code, if you pay attention to never cause grid reconstruction, which is a challenge, sometimes. And then you also have the possibility to design a separate class that you connect to the native grid, its columns and headers by BindEvent().

Chriss
 
Chris,

You wrote:

You could also design a grid in a form and let its column headers have click code, if you pay attention to never cause grid reconstruction, which is a challenge, sometimes.

I agree. And I think that is the case here. Because the ColumnCount is -1 (which is necessary because the RecordSource is created a run time), the columns won't exist at design time, so you can't add any code to them.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thats true, Mike, but you can always change code to a general solution that starts with an empty cursor in a grid, to which you append the data. Then you can have the columncount at designtime and program all code into the columns, headers and the controls of the columns and they don't ever reconstruct, so code is never lost and can be prepared at runtimedesigntime.

It's not that we waited for member classes to finally be able to have this.

Chriss
 
Thanks disclosing the topic, I have no experience with native classes and I didn't understand the latest
suggestion: creating code for the future cursor with Headers. Anyway, I followed the path
least resistance and created a ComboBox in which I recreate the cursor using SQL. Agree,
that using Header would be more elegant (because there is not much space on the form)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top