2011-04-16 86 views
3

我想在ASP.NET C#中訪問CrystalReportViewer的Print事件(當我單擊CrystalReportViewer的Print按鈕時),怎麼樣?CrystalReportViewer print

+0

妮可 - 這是使用的WinForms或ASP.NET。你用什麼語言使用C#或VB.NET? – codingbadger 2011-04-19 16:53:43

+0

請看我更新的答案。 – Dusty 2011-06-01 15:50:32

回答

7

像下面的東西應該工作。希望能幫助到你。

public Form1() 
    { 
     InitializeComponent(); 
     foreach (ToolStrip ts in crystalReportViewer1.Controls.OfType<ToolStrip>()) 
     { 
      foreach (ToolStripButton tsb in ts.Items.OfType<ToolStripButton>()) 
      { 
       //hacky but should work. you can probably figure out a better method 
       if (tsb.ToolTipText.ToLower().Contains("print")) 
       { 
        //Adding a handler for our propose 
        tsb.Click += new EventHandler(printButton_Click); 
       } 
      } 
     } 
    } 

    private void printButton_Click(object sender, EventArgs e) 
    { 
     MessageBox.Show("Printed"); 
    } 
+0

此版本的黑客將只能使用英文版的查看器,因爲工具欄將隨UI中使用的語言而改變。例如,法語版的黑客應該是'tsb.ToolTipText.ToLower()。Contains(「imprimer」)'。 – Matthieu 2012-01-11 14:49:01

+0

有沒有可能將這個轉換爲'vb.net?' – leonardeveloper 2014-01-17 06:13:36

0

不是一個真正的新的答案,但這裏是在C++上面使用.NET Framework對象等:

CRXViewerNetControl(void) 
    { 
     InitializeComponent(); 

     // OK, go though the CRViewer and find the Print Button in the toolbar. When we find it, 
     // attach a "Click" event handler so that we can detect the operator clicking the button to 
     // print the report. 
     for (Int32 i = 0; i < CRXViewer->Controls->Count; i++) 
     { 
      if (dynamic_cast<System::Windows::Forms::ToolStrip^>(CRXViewer->Controls[i])) 
      { 
       System::Windows::Forms::ToolStrip^ ctlToolStrip = dynamic_cast<System::Windows::Forms::ToolStrip^>(CRXViewer->Controls[i]); 
       for (int j = 0; j < ctlToolStrip->Items->Count; j++) 
       { 
        if (dynamic_cast<System::Windows::Forms::ToolStripButton^>(ctlToolStrip->Items[j])) 
        { 
         System::Windows::Forms::ToolStripButton^ ctlToolStripButton = dynamic_cast<System::Windows::Forms::ToolStripButton^>(ctlToolStrip->Items[j]); 
         if (ctlToolStripButton->ToolTipText->ToLower()->Contains("print")) 
         { 
          //Adding a handler for our propose 
          ctlToolStripButton->Click += gcnew System::EventHandler(this, &CRXViewerNetControl::printButton_Click); 
         } 
        } 
       } 
      } 
     } 

    } 

private: System::Void printButton_Click(System::Object^ sender, System::EventArgs^ e) 
    { 
     MessageBox::Show("Printed"); 
    }