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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AddObject code for cmdbtn.Click event ? 1

Status
Not open for further replies.

markathon

Programmer
Jul 11, 2002
7
US
Hi,
I am trying to create an add-on command button to the VFP 8 Standard toolbar. I am having trouble with getting the Click event to work properly. Does anyone know what the code here should be for the click event to take in the "do ..." command?

if wvisible('Standard')
release oTbr
public oTbr
oTbr = createobject('toolbar')
oTbr.addobject('cmdProjects','commandbutton')
oTbr.cmdProjects.Height = 26
oTbr.cmdProjects.Width = 58
oTbr.cmdProjects.Name = 'cmdProjects'
oTbr.cmdProjects.Caption = '\<Projects'
oTbr.cmdProjects.Click("do home()+'Utils\Appmgr.app'")
oTbr.cmdProjects.Visible = .t.
oTbr.dock(0)
oTbr.show()
endif

Thanks,


markathon
 
You can't add method code to an instance object.

Define your button in a VCX and use that class to add your toolbar. While you're at it, put your whole toolbar in a vcx and just createobject('yourtoolbar').
 
What dan is saying is: You can't assign code to a method in this way!
 
As stated by others, you'll have to have the code already
in the commandbutton's click() event.

As an additional note, if you want dynamic binding of
events, you could use "BindEvent()" and "RaiseEvent()"
in VFP 8 and above or subclass your commandbuttons and add
a property that the click() event tests to determine if it
should execute an alternative code path.

i.e.
[tt]
* This class would be placed in a vcx or
* could be based on code as demonstrated here
Define class clsCommandButton as commandbutton

bClickBindTo = .f. && Flags whether click event should
* execute and alternative code path

cClickCode = "" && Pointer to a method or file to run
* if bClickBindTo is True


Procedure Click
If !This.bClickBindTo
* Execute default click method
* Programmer may have placed code there at design time
DoDefault()
Return
Endif

Local cCodePath

*!* Should perform additional testing to
*!* determine how "this.cClickCode" should be called,
*!* and/or if a return value is expected
cCodePath = this.cClickCode
&cCodePath
Endproc

Enddefine
[/tt]
 
Markathon,

This is untested, but you can drop this in and see what happens. Additions are in bold:

Code:
if wvisible('Standard')
   release oTbr
   public oTbr, oBindClass
   oTbr = createobject('toolbar')
   [b]oBindClass = CREATEOBJECT('MyClass')[/b]
   oTbr.addobject('cmdProjects','commandbutton')
   oTbr.cmdProjects.Height = 26
   oTbr.cmdProjects.Width = 58
   oTbr.cmdProjects.Name = 'cmdProjects'
   oTbr.cmdProjects.Caption = '\<Projects' 
   oTbr.cmdProjects.Visible = .t.
   [b]BINDEVENT(oTbr.cmdProjects, "click", oBindClass, "MyMethod")[/b]
   oTbr.dock(0)
   oTbr.show()
endif

[b]DEFINE CLASS MyClass AS Custom
	PROCEDURE MyMethod
		do home()+'Utils\Appmgr.app'
	ENDPROC
ENDDEFINE[/b]

Andy
 
Dan, wgcs, darrellblackhawk, agoeddeke

Thanks for your answers, confirming what I had come to suspect - that I can't add method code to an object prodedurally. I was trying to keep it simple, as this code block was to exist inside my vfp8start.prg routine.

I tried the BindEvent code as suggested by agoeddeke but no luck. Instead, I will code the command button in a vcx that I can instantiate from the start up routine.

Thanks again,

markathon
 
markathon,

markathon said:
I tried the BindEvent code as suggested by agoeddeke but no luck
I just pasted the code I posted in a program and it worked fine in VFP 8. I'm curious as to why it didn't work for you?

Andy
 
agoeddeke,

I took another look at it and found that yes, your code works very well, it was my click event code that didn't work. It was missing a necessary parameter that should have been passed in.

I am using D. Hennig's Application Manager form, which if you put as "do AppMgr.app" in your vfp8start.prg, it will load it onto your VFP Tools menu. If you want to display it immediately from your start program or interactively from the Command window, it needs the "with RUN" added on to the command.

Thanks very much.

markathon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top