Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I override the printpageeventhandler as follow :

C#
public class MyPrintDoc : PrintDocument
    {
   
       
        protected void MyOnPrintPage(PrintPageEventArgs args)
        {
            PagePrintHandler(this, args, this.arr);
        }


and call as follow :

C#
private void Create(string[] arr)
{
                MyPrintDoc PDoc = new MyPrintDoc(arr);
                if (iPrintCount > 0)
                {
            
}

private void PrintPagearr(object sender, PrintPageEventArgs ev, string[] arr)
{
...
}


but arr is null when print. how do solve it.

What I have tried:

I change delegate , but when print the arr[] don't print anything.
Posted
Updated 18-Jun-16 1:04am
v2

1 solution

Event handler is just the method. It becomes a handler when you add it to the invocation list of some event instance.

Therefore, the concept of "overriding" is not different from overriding of any other method. The method should be virtual and the derived class need to have sufficient access to it; most typically, protected access modifier is used. Only then a method can be overridden.

In your code sample, I cannot see the keywords "virtual" or "abstract", therefore, there is nothing to override. To change it, you need to make the method virtual, only then you can override it.

If by "overriding" you actually meant something else, please clarify; probably I'll help you to use proper term for what you need, but in this case you have to explain your ultimate goals.

[EDIT]

For example,
C#
abstract class MyBaseForm : Form {
   internal MyBaseForm() {
      // ...
      Button.Click =+ ButtonClickHandler; 
   }
   Button Button = new Button;
   protected abstract ButtonClickHandler(object sender, EventArgs eventArgs);
}

// ...

class MyForm : MyBaseForm {
   protected override ButtonClickHandler(object sender, EventArgs eventArgs) {
       MessageBox("Do something");
   }
}

Note that doing the same with some control class and adding an event handler to the same event makes no practical sense, because, once you override, you can always use alternative handling, not adding an event handler, but overriding already existing virtual method, such as OnClick, OnPaint, and so on.

—SA
 
Share this answer
 
v2
Comments
bernova 17-Jun-16 16:46pm    
I Change it :
public class MyPrintDoc : PrintDocument
{
public delegate void MyPrintPageEventHandler(object sender, PrintPageEventArgs e, string[] arr);
public event MyPrintPageEventHandler PagePrintHandler;
private string[] arr;

public MyPrintDoc(string[] iarr)
{
this.arr = iarr;
}
public MyPrintDoc()
{ this.iarr = null; }
protected void MyOnPrintPage(PrintPageEventArgs args)
{
PagePrintHandler(this, args, this.iarr);
}
}
Sergey Alexandrovich Kryukov 17-Jun-16 17:17pm    
Where? Where is virtual or abstract?
—SA
bernova 18-Jun-16 2:14am    
could you shows an example ?
thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900