Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Arguments Via Event Handler?

So I'm not actually sending arguments, but setting a class variable to a certain value, then using it again in another method. Is this the "best practice" way to do things? If not, I'd be interested in learning the correct way. Thanks! Can/Should the arguments be sent some other way?

private string PrintThis;

public void PrintIt(string input){
    PrintThis = input; //SETTING PrintThis HERE
    static private PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(PrintDocument_PrintSomething);
    pd.Print();
}
private void PrintDocument_PrintSomething(Object sender, PrintPageEventArgs e) {
    e.Graphics.DrawString(PrintThis, new Font("Courier New", 12), Brushes.Black, 0, 0);
    //USING PrintThis IN THE ABOVE LINE
}
like image 923
sooprise Avatar asked Feb 11 '11 16:02

sooprise


People also ask

How do you pass arguments to an event handler?

If you want to pass a parameter to the click event handler you need to make use of the arrow function or bind the function. If you pass the argument directly the onClick function would be called automatically even before pressing the button.

How do you pass a parameter with an event in react?

To pass an event and parameter onClick in React:Pass an inline function to the onClick prop of the element. The function should take the event object and call handleClick . Pass the event and parameter to handleClick .

When would you use an event handler?

Use the EventHandler delegate for all events that don't include event data. Use the EventHandler<TEventArgs> delegate for events that include data about the event. These delegates have no return type value and take two parameters (an object for the source of the event and an object for event data).

What is event argument?

The second, called the event argument, contains information specific to the event, if any. For most events, the event argument is of type EventArgs, which does not expose any properties. So, the general prototype for an event in Visual Basic is: Private Sub EventName (ByVal sender As Object, _ ByVal e As EventArgs)


2 Answers

Closures were introduced into the language to solve this very problem.

By capturing the appropriate variable, you can give it storage that 'outlives' the containing method:

// Note that the 'input' variable is captured by the lambda.
pd.PrintPage += (sender, e) => Print(e.Graphics, input);
...

static void Print(Graphics g, string input) { ... }

Do note that this very much a convenience feature; the way the compiler solves this problem on your behalf is suspiciously similar to your own, existing solution. (There are certain differences, e.g. the captured variable ends up as a field of a newly created object of some other (generated) class. Your existing solution does not do this: you have one 'temporary' storage location per instance of your class rather than per call to PrintIt, which is not good - it isn't thread-safe, for example)

like image 52
Ani Avatar answered Oct 23 '22 20:10

Ani


Not normally, but for this API (WinForms printing) it is the usual approach.

Consider that PrintThis is not just a variable but your "model" or "document".

like image 23
Henk Holterman Avatar answered Oct 23 '22 21:10

Henk Holterman