2008-04-11

How to disable print button in report preview

till now i had seen many samples showing how we can make the print button invisible from the report preview toolbar, but i had never seen the possibility to show it disabled.
here's a sample, totally based in emerson reed's, from an old post in his great blog: a sample on how to add features to report listener - http://weblogs.foxite.com/emersonreed/archive/2006/09/13/a_sample_on_how_to_add_features_to_report_listener.aspx


it brings the possibility to hide completely the print button or to disable it - just set the properties "printbuttonvisible" and "printbuttonenabled". apart from this, it also allows to change the tooltips from the buttons. for my case this is really helpful, in order to have the translated tooltips to my native language.


i've only added 7 or 8 lines of code all the sample comes from emerson.
it was tricky to set the "enabled" property of the print button, because there's no property that allows us to set this, differently from the "visible", that was allowed, using the property "allowprintfrompreview".
even setting the property "enabled" directly in the extensionhandler class below, the button still appeared enabled.


.cmdprint.enabled = .f. && did not work


my solution was to use bindevent, in order to control the behavior whenever the report previewer tried to change the "enabled" property:


bindevent(this.previewform.toolbar.cmdprint,"enabled",this,"disabled",1)
 
then, i added a custom function "disabled", that forces the print button to appear disabled:
 
procedure disabled
   this
.previewform.toolbar.cmdprint.enabled = .f.
endproc

 

 
* see "leveraging the default preview container" topic in help for more info
* report listener based on emerson reed's sample from
*
http://weblogs.foxite.com/emersonreed/archive/2006/09/13/a_sample_on_how_to_add_features_to_report_listener.aspx


* create a report listener object
local loreportlistener
loreportlistener =
newobject("myreportlistener")
with loreportlistener
   .
listenertype = 1 && preview
   .printbuttonvisible = .t.
   .printbuttonenabled = .f.
endwith
 
* run a report from the samples of vfp using the new report engine (object-assisted output)
report form ;
   home() + 'samples\solution\reports\colors.frx' ;
   object loreportlistener
return
 
 
 
* custom report listener that adds some features
define class myreportlistener as fxlistener of addbs(home()) + "ffc\_reportlistener.vcx"


* public properties
printbuttonvisible = .t.
printbuttonenabled = .t.
*
procedure loadreport
   dodefault
()
   with this
      if
.listenertype==1 and not vartype(.previewcontainer)=="o"
         .extendpreviewcontainer()
      endif
   endwith
endproc
*
function extendpreviewcontainer
   local lopreviewcontainer
   lopreviewcontainer =
null
   do
(_reportpreview) with lopreviewcontainer
   lopreviewcontainer.allowprintfrompreview =
this.printbuttonvisible

   local loexthandler
   loexthandler =
createobject("myextensionhandler")
   loexthandler.disableprintbutton = not
this.printbuttonenabled

   lopreviewcontainer.setextensionhandler(loexthandler)
   this.previewcontainer = lopreviewcontainer
endfunc
enddefine



* create a class that will extend report preview
define class myextensionhandler as custom
   disableprintbutton = .f.


procedure show(istyle)
with this.previewform
   with .toolbar
   * translate toolbar buttons tooltips to brazilian portuguese language
   .cbozoom.tooltiptext = "zoom"
   .cmdclose.
tooltiptext = "fechar a visualização"
   .cmdgotopage.
tooltiptext = "ir para a página"
   .cmdprint.
tooltiptext = "imprimir"
 
   if this.disableprintbutton
      * bindevent(this.previewform.toolbar.cmdprint, "visible", this, "invisible", 1)


      * here we control the enabled property
      bindevent(this.previewform.toolbar.cmdprint, "enabled", this, "disabled", 1)
   endif

   with
.cntnext
      .cmdbottom.
tooltiptext = "última página"
      .cmdforward.
tooltiptext = "próxima página"
   endwith


   with .cntprev
      .cmdback.
tooltiptext = "página anterior"
      .cmdtop.
tooltiptext = "primeira página"
   endwith


   with .opgpagecount
      .opt1.
tooltiptext = "uma página"
      .opt2.
tooltiptext = "duas páginas"
      .opt3.
tooltiptext = "quatro páginas"
   endwith
*
   endwith
   .windowstate = 2 && maximize report preview
   endwith
   dodefault
(istyle)

endproc


* here is the relevant code to disable the button
procedure disabled
   this
.previewform.toolbar.cmdprint.enabled = .f.
endproc
*
enddefine

1 comment:

  1. Olá Cesar!

    Parabéns por mais uma classe fantástica!!!

    Só uma pergunta, quando se mostra um report o valor do zoom aparece, por defeito, 100%. Nesse código não tem como abrir o report com a opção do zoom 'Fit to Width' ?

    Abraço,

    Pedro
    Ola Pedro,
    Isso é muito facil, basta configurar a propriedade nZoomLevel. Tente:
    .nZoomLevel = 11 && Page width
    Abraço
     

    ReplyDelete