Hey, i'm in a bit of a deadline here, so i need your help. I'm trying to develop an application that, upon selection of an event and its respective "listener" (a button), fires the event name and the current time. Thing is, so i can print the event name i must be able to pass the event name to the handler function. I've done this:
Dictionary<Generico,Control> lst = new Dictionary<Generico,Control>();
where Generico is an inner class:
public class Generico : FormRecorder//Inner class
{
private EventInfo nome;
private bool rec;
public Generico(EventInfo e,bool isrec)
{
nome = e;
rec = isrec;
}
public String getName()
{
return nome.Name;
}
public EventInfo getEvtInfo()
{
return nome;
}
public bool getRec(){
return rec;
}
public override string ToString()
{
return (nome.Name + ":");
}
}
And here's the code thats giving me problems...if i select just one event and one button, it all works fine. but if i select more that one event/button, it just works for the first event assigned.
private void chkLstBx2_ItemCheck(object sender, ItemCheckEventArgs ice)
{
for(int i = 0; i<chkLstBx1.Items.Count; i++){
if(chkLstBx1.GetItemCheckState(i) == CheckState.Checked){
EventInfo[] eventList = f.Controls[i].GetType().GetEvents(); //eventos associados ao botao seleccionado
foreach(EventInfo e in eventList){
if (chkLstBx2.Items[ice.Index].Equals((e.Name)))
{
Type eventHandlerType = e.EventHandlerType;
Generico g = new Generico(e, isRec);
MethodInfo mi = this.GetType().GetMethod("GenericMethod", BindingFlags.Instance | BindingFlags.Public |
BindingFlags.NonPublic);
Delegate d = Delegate.CreateDelegate(eventHandlerType,this,mi,true);
e.AddEventHandler(f.Controls[i], d);
lst.Add(g,f.Controls[i]); //Nao posso adicionar duas keys iguais
break;
}
}
}
}
}
public void GenericMethod(Object s, EventArgs evtargs) //o sender é o botao que dispara o evento
{
//Ler as assinaturas dos handlers dos eventos de outros tipos
//Construir chamada ao método onEvent utilizando os casts necessários
Control ctrl = s as Control;
if (ctrl != null)
{
listBox1.Items.Add(lst[ctrl].Name.ToString() + DateTime.Now.TimeOfDay);
}
}
I dont know how i can get info from the fired event in the handler method. Please help!! thanks ;)