Click here to Skip to main content
15,895,192 members
Home / Discussions / C#
   

C#

 
QuestionA 2D graphics via Direct3D - gaming engin Pin
HRiazi10-Sep-06 0:37
HRiazi10-Sep-06 0:37 
AnswerRe: A 2D graphics via Direct3D - gaming engin Pin
Nader Elshehabi10-Sep-06 1:00
Nader Elshehabi10-Sep-06 1:00 
AnswerRe: A 2D graphics via Direct3D - gaming engin Pin
Arjun "Mjolnir" Bahree10-Sep-06 3:46
Arjun "Mjolnir" Bahree10-Sep-06 3:46 
AnswerRe: A 2D graphics via Direct3D - gaming engin Pin
Judah Gabriel Himango10-Sep-06 7:37
sponsorJudah Gabriel Himango10-Sep-06 7:37 
QuestionShow a still image in wondows media player Pin
Yasamin Mokri9-Sep-06 21:26
Yasamin Mokri9-Sep-06 21:26 
AnswerRe: Show a still image in wondows media player Pin
Nader Elshehabi10-Sep-06 2:40
Nader Elshehabi10-Sep-06 2:40 
AnswerRe: Show a still image in wondows media player Pin
Arjun "Mjolnir" Bahree10-Sep-06 3:50
Arjun "Mjolnir" Bahree10-Sep-06 3:50 
Questionreflection + AddEventHandler() Pin
g00fyman9-Sep-06 18:48
g00fyman9-Sep-06 18:48 
hi all,

I have a custom ASP.NET server control (don't worry the question is primarily about reflection, not ASP.NET) that I want to dynamically wire up events to using the designer.

Basically it works like this:

I have my Control on the Page, and through the Property Browser in the Designer, you can set the 'Trigger' to show the Control via a dropdown list of the Controls ID's on the Page. (similar to the ControlToValidate Property of a Validator Control).

Then in code I dynamically wireup the event like so:

private void RegisterControlEvent()
{
  Control control = this.NamingContainer.FindControl(ControlTrigger);
  if (control == null) { return; }

  string eventName = ControlCoverter.GetEventName(control);
  if (eventName == string.Empty) { return; }

  EventInfo ei = control.GetType().GetEvent(eventName);
  Delegate del = Delegate.CreateDelegate(ei.EventHandlerType, this, "Control_TriggerEvent");
  ei.AddEventHandler(control, del);
}


now this works fine, fo a System.Web.UI.WebControls.Button because the Click event Delegate is the same as mine, which is (simplified for brevity)

void Control_TriggerEvent(object sender, EventArgs e)
{
  Show();
}


but I need to be able to dynamically detect the event Delegate and change the function accordingly, like for an System.Web.UI.WebControls.ImageMap the required delegate is

protected void ImageMap2_Click(object sender, ImageMapEventArgs e)
{

}


so the button event works fine, but not for anything having a different delegate, the objective is to be able to support any WebControl / Control that has an event registered within my ControlConverter class.


can anyone help me on acheiving this please?

my ControlConverter class is as follows:

public class ControlCoverter : ControlIDConverter
  {
    public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
      if (context == null) { return null; }

      IDesignerHost host1 = (IDesignerHost)context.GetService(typeof(IDesignerHost));
      if (host1 == null) { return null; }

      string[] textArray1 = this.GetControls(host1, context.Instance);
      if (textArray1 == null) { return null; }

      return new TypeConverter.StandardValuesCollection(textArray1);
    }

    // if a Control Supports the following events then it is allowed to trigger the MessageBox
    private static readonly string[] EventTriggers = new string[]
      {
        "Click", "SelectedIndexChanged", "CheckChanged", "ServerClick", "ServerChange",
      };

    private string[] GetControls(IDesignerHost host, object instance)
    {
      IContainer container1 = host.Container;
      IComponent component1 = instance as IComponent;
      if ((component1 != null) && (component1.Site != null)) { container1 = component1.Site.Container; }
      if (container1 == null) { return null; }

      ComponentCollection collection1 = container1.Components;
      ArrayList list1 = new ArrayList();

      foreach (IComponent component2 in collection1)
      {
        Control control1 = component2 as Control;

        // see if the control has an event that is in our 'known events' list
        EventInfo[] eventInfo = control1.GetType().GetEvents();
        bool supportsEvent = false;
        foreach (EventInfo ei in eventInfo)
        {
          string eventName = ei.Name;
          if (Array.IndexOf<string>(EventTriggers, eventName) != -1)
          {
            supportsEvent = true;
            break;
          }
        }

        if ((((control1 != null) && (control1 != instance)) && ((control1 != host.RootComponent) && (control1.ID != null))) &&
            ((control1.ID.Length > 0) && this.FilterControl(control1)) &&
             (supportsEvent))
        {
          list1.Add(control1.ID);
        }
      }
      list1.Sort(Comparer.Default);
      return (string[])list1.ToArray(typeof(string));
    }

    public static string GetEventName(Control control)
    {
      EventInfo[] eventInfo = control.GetType().GetEvents();
      foreach (EventInfo ei in eventInfo)
      {
        string eventName = ei.Name;
        if (Array.IndexOf<string>(EventTriggers, eventName) != -1)
        {
          return eventName;
        }
      }
      return string.Empty;
    }
  }


kind regards,
g00fy
AnswerRe: reflection + AddEventHandler() Pin
Jakub Mller10-Sep-06 22:22
Jakub Mller10-Sep-06 22:22 
GeneralUnable to Debug VC# application Pin
keencomputer9-Sep-06 18:39
keencomputer9-Sep-06 18:39 
GeneralRe: Unable to Debug VC# application Pin
Christian Graus9-Sep-06 19:07
protectorChristian Graus9-Sep-06 19:07 
GeneralRe: Unable to Debug VC# application Pin
keencomputer9-Sep-06 19:19
keencomputer9-Sep-06 19:19 
GeneralRe: Unable to Debug VC# application Pin
Christian Graus9-Sep-06 19:53
protectorChristian Graus9-Sep-06 19:53 
GeneralRe: Unable to Debug VC# application Pin
Arjun "Mjolnir" Bahree10-Sep-06 3:34
Arjun "Mjolnir" Bahree10-Sep-06 3:34 
Questionquestion about a WEIRD problem in sending mail using smtp client. Pin
Green Fuze9-Sep-06 14:44
Green Fuze9-Sep-06 14:44 
AnswerRe: question about a WEIRD problem in sending mail using smtp client. Pin
Nader Elshehabi9-Sep-06 17:30
Nader Elshehabi9-Sep-06 17:30 
GeneralRe: question about a WEIRD problem in sending mail using smtp client. Pin
Green Fuze9-Sep-06 21:57
Green Fuze9-Sep-06 21:57 
GeneralRe: question about a WEIRD problem in sending mail using smtp client. Pin
Nader Elshehabi10-Sep-06 0:10
Nader Elshehabi10-Sep-06 0:10 
GeneralRe: question about a WEIRD problem in sending mail using smtp client. Pin
Green Fuze10-Sep-06 0:17
Green Fuze10-Sep-06 0:17 
GeneralRe: question about a WEIRD problem in sending mail using smtp client. Pin
Nader Elshehabi10-Sep-06 0:50
Nader Elshehabi10-Sep-06 0:50 
GeneralRe: question about a WEIRD problem in sending mail using smtp client. Pin
Green Fuze10-Sep-06 1:43
Green Fuze10-Sep-06 1:43 
JokeRe: question about a WEIRD problem in sending mail using smtp client. Pin
Nader Elshehabi10-Sep-06 2:28
Nader Elshehabi10-Sep-06 2:28 
GeneralRe: question about a WEIRD problem in sending mail using smtp client. Pin
Green Fuze10-Sep-06 2:33
Green Fuze10-Sep-06 2:33 
GeneralRe: question about a WEIRD problem in sending mail using smtp client. Pin
Nader Elshehabi10-Sep-06 2:48
Nader Elshehabi10-Sep-06 2:48 
GeneralRe: question about a WEIRD problem in sending mail using smtp client. Pin
Arjun "Mjolnir" Bahree10-Sep-06 3:16
Arjun "Mjolnir" Bahree10-Sep-06 3:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.