events.  
Author Message
pessi





PostPosted: Visual C# Language, events. Top

hi all

I have a method like following..

public void something()
{
string label1 = "myname";
string label2 = "myplace";

I have a checkbox p in here.

p.CheckedChanged + = new EventHandler(pBox_Changed);
}
public void pBox_Changed(Object sender, System.EventArgs e)
{
I want to access the values of label1 and label 2 HERE.
}

As I said above I want to access the values of label1 and label two inside the event..
Could you pass these to the eventhandler

Thank you in advance,
cheers,
prasad..



Visual C#16  
 
 
Michael Letterle





PostPosted: Visual C# Language, events. Top

No, the event args are passed by the code firing the event, which exists in the CheckBox class. Your best bet is to move the declarations of label1 and label2 out of the something method and make them global to the class.


 
 
Michael Letterle





PostPosted: Visual C# Language, events. Top

Let me say that you COULD create a new checkbox class that inherits from System.Windows.Forms.CheckBox and override the OnCheckedChanged method and use a custom EventArgs class that contains the info you want...


 
 
RizwanSharp





PostPosted: Visual C# Language, events. Top

variables defined in a function have limited scope only to those functions and cant be accessed out side of that function.

Try this:

class SomeClass

{

string label1;

string label2

public void something()
{
label1 = "myname";
label2 = "myplace";

I have a checkbox p in here.

p.CheckedChanged + = new EventHandler(pBox_Changed);
}
public void pBox_Changed(Object sender, System.EventArgs e)
{
// You can access them because they are defined on the class level.

}

}

I hope this will help.

Best Regards,

Rizwan