unable to access EventArgs e value to use in HandleRequest - c#

I am just learning about events, delegates and subscribers. I've spent the last 2 days researching and wrapping my brain around it all. I am unable to access the information being passed in my EventArgs e value. I have a saved project that wants to be opened. The state of the necessary forms are deserialized into a dictionary. A loop is hit that raises the UnpackRequest passing the key/value with it.
ProjectManager.cs file:
public delegate void EventHandler<TArgs>(object sender, TArgs args) where TArgs : EventArgs;
public event EventHandler<UnpackEventArgs> UnpackRequest;
Then farther down:
ProjectManager.cs file:
//Raise a UnpackEvent //took out virtual
protected void RaiseUnpackRequest(string key, object value)
{
if (UnpackRequest != null) //something has been added to the list?
{
UnpackEventArgs e = new UnpackEventArgs(key, value);
UnpackRequest(this, e);
}
}
Then within the open method, after dictionary gets populated with states of each form:
ProjectManager.cs file:
foreach (var pair in dictPackState) {
string _key = pair.Key;
dictUnpackedState[_key] = dictPackState[_key];
RaiseUnpackRequest(pair.Key, pair.Value); //raises the event.
}
I have a class for the Event:
public class UnpackEventArgs : EventArgs
{
private string strKey;
private object objValue;
public UnpackEventArgs(string key, object value)
{
this.strKey = key;
this.objValue = value;
}
//Public property to read the key/value ..and get them out
public string Key
{
get { return strKey; }
}
public object Value
{
get { return objValue; }
}
}
I am still working on the code to add subscribers and how the project components get re-constituted in the individual forms. But the part I'm trying to work on now is in the MainForm.cs where I handle the Unpacked Request using the arguements getting passed. My e contains the key values and the key represents where to send the value (which is the form object).
private void HandleUnpackRequest(object sender, EventArgs e)
{
//reflection happens here. turn key into object
//why can't i get e.key ???
object holder = e; //holder = VBTools.UnpackEventArgs key... value...all info
//object goToUnpack = (e.key).GetType();
//goToUnpack.unpackState(e.value);
}
I think I included all the necessary parts to get any help?! THANKS!

Change this:
private void HandleUnpackRequest(object sender, EventArgs e)
To this:
private void HandleUnpackRequest(object sender, UnpackEventArgs e)
Remember your event handler declaration:
public event EventHandler<UnpackEventArgs> UnpackRequest;

Related

Access object without knowing the name beforehand

How can I access an object without knowing its name ? (how to concatenate object name).
I have the following code:
private void pb1_Click(object sender, EventArgs e)
{
Check("1");
}
void Check(string x)
{
if (("pb"+x).Image == img1)
{
("pb"+x).Image = img2;
}
}
This is not the way to go about it. I would recommend you use a dictionary to keep track of your loaded objects, and then just look them up by number in the dictionary.
private Dictionary<int, MyClass> myDict = new Dictionary<int, MyClass>();
void Check(int value)
{
MyClass target = null;
myDict.TryGetValue(value, out target);
//target.Image = etc.
}
You shouldn't pass the controls' name. You'll be better off by passing a reference to that object, which you already have in the eventhandler:
private void pb1_Click(object sender, EventArgs e)
{
SwapImages(sender as PictureBox);
}
private void SwapImages(PictureBox pb)
{
if (pb.Image == img1)
{
pb.Image = img2;
}
}

Get exception when I try to raise an event

I got an enum:
public enum sprog{
dansk,
svensk,
norsk
}
In a method I would raise an event and use the enum to carry information:
public delegate void BrugerSprogChanged(Object sender, Sprog sprog);
class clazz
{
public event BrugerSprogChanged brugerSprogChanced;
public clazz(){}
private void comboBoxDokumentSprog_SelectedIndexChanged(object sender, EventArgs e)
{
Sprog sprog = FindSprog((string)((ComboBox)sender).SelectedItem);
dokumentSprogChanged(this, sprog); // <- here we have the problem
}
}
When the code shall raise the event I get an exception when dokumentSprogChanged(this, sprg) is called:
*"NullReferenceException was unhandled by user code
Object reference not set to an instance of an object"
"this" and "sprog" are not null.
Any suggestions?
The easy way is to drop the unem and use an integer/string instead, but then I end up with some ugly code.
Normally to call an event you have to check if it's handler is not null:
var handler = dokumentSprogChanged; // take a local reference
if (handler != null)
{
dokumentSprogChanged(this, sprog);
}
This way you can raise it safely.
EDIT
You need to register the event like this:
public event BrugerSprogChanged brugerSprogChanced;
....
brugerSprogChanced += class_brugerSprogChanced;
....
void class_brugerSprogChanced(object sender, EventArgs e)
{
// handle there
}
Try this:
class clazz
{
public event BrugerSprogChanged brugerSprogChanced;
public clazz(){}
private void comboBoxDokumentSprog_SelectedIndexChanged(object sender, EventArgs e)
{
Sprog sprog = FindSprog((string)((ComboBox)sender).SelectedItem);
if (dokumentSprogChanged != null)
{
dokumentSprogChanged(this, sprog); // <- here we have the problem
}
}
}

Passing data with events

I need to pass data with an event. Currently, when receiving more data (via comport), the event will fire but the previous event (&data) is not handled yet, so the data gets overwritten.
How can I handle the event &data in a safe way? I have multiple events like this (15x), so I am not sure if using a queue for the data is the best way or pass data along with the event (like S.O. item 4215845).
Example (this example is with a string, but I also use arrays, bools etc):
At the 'sender' side (class1):
public event EventHandler evDiaStringMessage = delegate { };
private void evDiaStringMessageEvent()
{
evDiaStringMessage(this, new EventArgs());
}
private static string _DiaString;
public string DiaString
{
get { return _DiaString; }
set { _DiaString = value; }
}
DiaString contains the data and gets overwritten when evDiaStringMessage is fired too soon.
At the 'receiver / GUI' side (class2):
dia.evDiaStringMessage += new EventHandler(dia_evDiaStringMessage);
private delegate void dia_evDiaStringMessageCallBack(object sender, EventArgs e);
void dia_evDiaStringMessage(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new dia_evDiaStringMessageCallBack(dia_evDiaStringMessage), new object[] { sender, e});
}
else
{
frmcomm.CommTextBox("Receiver message: " + dia.DiaString + "\r\n", Color.Red);
}
}
dia.DiaString does not contain the expected data (previous data), but all events are 'received'.
Your help is very much appreciated! Even more with an example!
Edit:
I changed the code to:
At the 'sender' side (class1):
public event EventHandler<DiaStringEventArgs> evDiaStringMessage ;
public class DiaStringEventArgs : EventArgs
{
public string DiaString { get; set; }
}
private void evDiaStringMessageEvent(DiaStringEventArgs e)
{
EventHandler<DiaStringEventArgs> handler = evDiaStringMessage;
if (handler != null)
handler(this, e);
}
...
private void PrepareDataAndFireEvent()
{
DiaStringEventArgs args = new DiaStringEventArgs();
args.DiaString = ByteToString(data);
evDiaStringMessageEvent(args);
}
At the 'receiver / GUI' side (class2):
dia.evDiaStringMessage += new EventHandler<ifDiA10.DiaStringEventArgs>(dia_evDiaStringMessage);
private delegate void dia_evDiaStringMessageCallBack(object sender, ifDiA10.DiaStringEventArgs e);
void dia_evDiaStringMessage(object sender, ifDiA10.DiaStringEventArgs e)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new dia_evDiaStringMessageCallBack(dia_evDiaStringMessage), new object[] { sender, e});
}
else
{
frmcomm.CommTextBox("Receiver message: " + e.DiaString + "\r\n", Color.Red);
}
}
You can store your data in a custom EventArgs class:
public class ReceivedDataEventArgs : EventArgs
{
// Add the properties you require
}
The event is defined like so:
public event EventHandler<ReceivedDataEventArgs> ReceivedData;
Your handler will take in an instance your class instead of the EventArgs object, and hence you'll have the correct data.
You should pass the value of dia.DiaString when the event is raised rather than reading it back when the event is handled.
You can do this by extending the EventArgs class and creating custom properties.
If you need an example let me know.

Observers in ASP.Net with Delegates

Well, i'm working in a asp.net 3.5 site.
I have set an Observer like this:
public delegate void ActionNotification();
protected Dictionary<string, List<ActionNotification>> Observers
{
get
{
Dictionary<string, List<ActionNotification>> _observers = Session["Observers"] as Dictionary<string, List<ActionNotification>>;
if (_observers == null)
{
_observers = new Dictionary<string, List<ActionNotification>>();
Observers = _observers;
}
return _observers;
}
set
{
Session["Observers"] = value;
}
}
public void Attach(string actionName, ActionNotification observer)
{
if (!Observers.ContainsKey(actionName))
{
Observers.Add(actionName, new List<ActionNotification>());
}
Observers[actionName].Add(observer);
}
public void Detach(string actionName, ActionNotification observer)
{
if (Observers.ContainsKey(actionName))
{
Observers[actionName].Remove(observer);
}
}
public void DetachAll(string actionName)
{
if (Observers.ContainsKey(actionName))
{
Observers.Remove(actionName);
}
}
public void Notify(string action)
{
if (Observers.ContainsKey(action))
{
foreach (ActionNotification o in Observers[action])
{
o.Invoke();
}
}
}
I use the observer like this:
//Esta es llamada al notify con cierto action
protected void btnNext_Click(object sender, ImageClickEventArgs e)
{
Notify("Next");
}
//Y este es el register del Listener
Attach("Next", new ActionNotification(NextButton_Click));
If before the o.Invoke(); for example i change the page title to "Hello".
And inside the "NextButton_Click" I set it to "Goodbye", after the NextButton_Click finish, the Title goes back to "Hello"...
Any idea why?
I think problem is that the "Page" in your NextButton_Click event is not the same page as the page you set the title to "Hello" on. Because you are passing around events in the session when the event is raised the object is acts on is no longer in scope. You can recreate it with the following code (which is using EventHandlers, but they are basically the same as what you have outlined in your code)
protected void Page_Load(object sender, EventArgs e)
{
this.Page.Title = "test";
//Store it in your session...seems like a weird thing to do given how your page should be stateless, so I would think about what you are
//trying to do a bit more carefully. You don't want to call an event handler such as test below from another page in your asp.net app.
Dictionary<string, EventHandler> myEvents = null;
if (Session["Invokers"] == null)
{
myEvents = new Dictionary<string, EventHandler>();
Session["Invokers"] = myEvents;
}
else
{
myEvents = Session["Invokers"] as Dictionary<string, EventHandler>;
}
//If the event handler key is not in there then add it
if (myEvents.ContainsKey("buttonClickOnPageDefault") == false)
{
//Subscribe to event (i.e. add your method to the invokation list
this.TestEvent += new EventHandler(test);
myEvents.Add("buttonClickOnPageDefault", this.TestEvent);
}
else
{
//if it does contain this key then you may already be subscribed to event, so unsubscribe in case and then resubscribe...you could
//probably do this more elegantly by looking at the vales in the GetInvokationList method on the eventHandler
//Wire up the event
this.TestEvent -= new EventHandler(test);
this.TestEvent += new EventHandler(test);
}
//Resave the dictionary.
Session["Invokers"] = myEvents;
}
void test(object o, EventArgs e)
{
this.Page.Title = "testEvent";
}
public event EventHandler TestEvent;
protected void Button1_Click(object sender, EventArgs e)
{
if (Session["Invokers"] != null)
{
Dictionary<string, EventHandler> myEvents = (Dictionary<string, EventHandler>)Session["Invokers"];
if (myEvents.ContainsKey("buttonClickOnPageDefault"))
{
EventHandler ev = myEvents["buttonClickOnPageDefault"];
ev(null, EventArgs.Empty);
}
}
}
If you put the above code in an asp.net page it will never change page title, but if you put a breakpoint in the Test method you will see it being hit. The reason is that its being hit in a different page (and that page is out of scope and may not be garbage collected as your event still has a reference to it, so this could cause a memory leak...be careful with it!). Really you probably shouldn't be using your events this way (at least not to act on a page...maybe it has some utility for domain objects). Note that the following will work (as its acting on the same page)
protected void Page_Load(object sender, EventArgs e)
{
this.Page.Title = "test";
//Store it in your session...seems like a weird thing to do given how your page should be stateless, so I would think about what you are
//trying to do a bit more carefully. You don't want to call an event handler such as test below from another page in your asp.net app.
this.TestEvent += new EventHandler(test);
Session["Invoker"] = this.TestEvent;
}
void test(object o, EventArgs e)
{
this.Page.Title = "testEvent";
}
public event EventHandler TestEvent;
protected void Button1_Click(object sender, EventArgs e)
{
if (Session["Invoker"] != null)
{
EventHandler ev = (EventHandler)Session["Invoker"];
ev(null, EventArgs.Empty);
}
}
Hope that gives you some pointers to where your problem might be.

How do I pass variables to a buttons event method?

I need to be able to pass along two objects to the method being fired when I click a button. How do I do this?
So far I've been looking at creating a changed EventArgs:
public class CompArgs : System.EventArgs
{
private object metode;
private Type typen;
public CompArgs(object m, Type t)
{
this.metode = m;
this.typen = t;
}
public object Metode()
{
return metode;
}
public Type Typen()
{
return typen;
}
}
But how would I use it? Is it possible to somehow override the click-event of the button to use a custom eventhandler, which takes CompArgs as a parameter?
private void button1_Click(object sender, EventArgs e)
{
Assembly assembly = Assembly.LoadFile(#"c:\components.dll");
int counter = 0;
foreach (Type type in assembly.GetTypes())
{
if (type.IsClass == true)
{
Button btn = new Button();
btn.Location = new Point(174 + (counter * 100),10);
btn.Size = new Size(95, 23);
btn.Name = type.Name;
btn.Text = type.Name;
btn.UseVisualStyleBackColor = true;
this.Controls.Add(btn);
object obj = Activator.CreateInstance(type);
//I need to pass on obj and type to the btn_Click
btn.Click += new eventHandler(btn_Click);
counter++;
}
}
}
And the event-method where I need it:
private void btn_Click(object sender, CompArgs ca)
{
MessageBox.Show((string)ca.Typen().InvokeMember("getMyName",
BindingFlags.Default | BindingFlags.InvokeMethod,
null,
ca.Metode(),
null));
}
Wow, you guys are making this entirely to difficult. No need for any custom classes or method overrides. In this example I just need to pass a tab index number. You can specify whatever you want, so long as your method is expecting that value type.
button.Click += (sender, EventArgs) => { buttonNext_Click(sender, EventArgs, item.NextTabIndex); };
void buttonNext_Click(object sender, EventArgs e, int index)
{
//your code
}
Cant you just set a property or member variable on the form that hosts the button and access these from the button click event?
EDIT: custom button class suggestion after feedback comment (not the same suggestion as above)
class MyButton : Button
{
private Type m_TYpe;
private object m_Object;
public object Object
{
get { return m_Object; }
set { m_Object = value; }
}
public Type TYpe
{
get { return m_TYpe; }
set { m_TYpe = value; }
}
}
Button1Click(object sender, EventArgs args)
{
MyButton mb = (sender as MyButton);
//then you can access Mb.Type
//and Mb.object
}
I'd create a new Button and override the OnClick method. Rather than passing down the EventArgs, pass a new derived class in with your additional members.
On the delegate receiving the event, cast the given EventArgs to the more derived class you're expecting to get, alternatively setup a new Event that will be triggered at the same time when the button is pressed and hook up to that instead to make things more implicit.
Example Code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ButtonEx b1 = new ButtonEx();
b1.OnCustomClickEvent += new ButtonEx.OnCustomClickEventHandler(b1_OnCustomClickEvent);
}
void b1_OnCustomClickEvent(object sender, ButtonEx.CustomEventArgs eventArgs)
{
string p1 = eventArgs.CustomProperty1;
string p2 = eventArgs.CustomProperty2;
}
}
public class ButtonEx : Button
{
public class CustomEventArgs : EventArgs
{
public String CustomProperty1;
public String CustomProperty2;
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
if(OnCustomClickEvent != null)
{
OnCustomClickEvent(this, new CustomEventArgs());
}
}
public event OnCustomClickEventHandler OnCustomClickEvent;
public delegate void OnCustomClickEventHandler(object sender , CustomEventArgs eventArgs);
}
You could use the Tag property of the button. You can add a string value to this property from the designer properties window and then pick it up within the handler as so:
private void MyButton_Click(object sender, EventArgs e)
{
string tagValue = ((Button) sender).Tag;
if(tag == "blah")
{
// Do something
}
}
Not sure if this exists in Winforms but it does in WPF: There is a "tag" object on all controls which you can attach any object to. You could save the object that you want to pass and then in the event handler read it back out of the sender object.
private void Button_Click(object sender, RoutedEventArgs e)
{
var note = (sender as FrameworkElement).Tag as Note;
//Do something with note here
}
You can't use your own custom event argument class for a predefined event handler signature. At least, the custom event argument type will never be utilised by any default calls to the handler (which will only ever be of type EventArgs in the case of a button); you could, potentially, call the handler yourself, passing your custom type, however, you would need to have logic in order to cast it back from an EventArgs into that which it had been cast from.
As a possible solution (depending on your situation), consider a composite type to encapsulate the items you require, as with your event argument type, but keep the required instance as an accessible variable which can be utilised from within the event handler, or, at least, by the method/s which the even handler invokes.
For example, define your type...
public class MyType
{
public object AccessibleItem { get; set; }
}
And, in your form class...
private MyType MyTypeInstance = new MyType();
private void Button_Click(object sender, EventArgs e)
{
//here we can set the item, if needs be...
MyTypeInstance.AccessibleItem = new Anything();
//or access the item to use as required...
DoSomeMagicWithMyObject(MyTypeInstance.AccessibleItem);
}
EDIT:
Okay, looking at your current code I can only offer you this for now (it doesn't add the items to the forms control container and it uses a variable iterator within Linq (which I think is either frowned upon or just down-right wrong (?), but hey...):
private void BuildButtonToObjectDictionary()
{
int counter = 0;
var assembly = Assembly.LoadFile(#"c:\components.dll");
var buttonToObjectDictionary = (
from type in assembly.GetTypes()
where type.IsClass && !type.IsAbstract
select new
{
Button = new Button
{
Name = type.Name,
Text = type.Name,
Size = new Size(95, 25),
Location = new Point(175 + (counter * 100), 10),
UseVisualStyleBackColor = true
},
Item = Activator.CreateInstance(type),
Index = counter++
});
}
Would need to see more code to give a better answer, but you could create an event that takes your CompArgs as a parameter and is fired when the buttonEvent is captured
If you open yourForm.Designer.cs file you will see all the code auto generated by VS. Here you can alter the method called when clicking on the button (or add a second method).
this.yourButton.Location = new System.Drawing.Point(211, 51);
this.yourButton.Name = "yourButton";
this.yourButton.Size = new System.Drawing.Size(75, 23);
this.yourButton.TabIndex = 0;
this.yourButton.Text = "yourButton";
this.yourButton.UseVisualStyleBackColor = true;
this.yourButton.Click += new System.EventHandler(this.yourMethodHere(object1, object2);
Hope this helps.

Categories