private List<EventInfo> GetAllTheEventsTheControlCanPublish(Control control)
{
return control.GetType().GetEvents().ToList();
}
The question is: how to get a list of ONLY the events a control is has published? I have the simple winform application:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace GettingAllEventsOfAControl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
_listOfSelectableValues = new List<int>() { 3, 5, 8, 11, 15 };
foreach (int currentValue in _listOfSelectableValues)
{
comboBox1.Items.Add(currentValue);
}
comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
comboBox1.Click += new EventHandler(comboBox1_Click);
_allTheEventsTheControlCanPublish = GetAllTheEventsTheControlCanPublish(comboBox1);
}
private List<int> _listOfSelectableValues;
private List<EventInfo> _allTheEventsTheControlCanPublish;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox1_Click(object sender, EventArgs e)
{
}
private List<EventInfo> GetAllTheEventsTheControlCanPublish(Control control)
{
return control.GetType().GetEvents().ToList();
}
}
}
I would like to have a method like:
private List<EventInfo> GetPublishedEvents(Control control)
{
return ...;
}
that will return a list of 2 items in this case:
- SelectedIndexChanged
- Click
Even though dr.null gave a way around, my question is still valid:
Given a control, is there a way to get a list of objects of type EventInfo of only the events this control has actually published?
To ask the question in a different form:
The method GetPublishedEvents(Control control) returns a list of objects of type EventInfo of ALL the events the specific control can publish.
Is there a field or a property of the object EventInfo whose value changes if the respective control has actually published this specific event?
I could then loop through all the objects in the list allTheEventsTheControlCanPublish and inspect the value of this field or property and thus find out which events are actually published.