Hi
We have bought a third party component (a linklabel columnstyle for a
datagrid) which sometimes acts a little stupid. It should raise a specific
event at times (when I click on it the first time), but fails to do so.

I've been in contact with the suplier of this component, but they don't
prioritize the problem yet, so I have to fix this myself.
We use this component about 60-70 places, so the fix should encapsulated
in an overload of the component.

I've made a simple example, try to make one button trigger the Click event
of another button. I know that I could call button2.PerformClick() but the
component isn't a button.

using System;
using System.Windows.Forms;

using System.Reflection;
using System.Security;


namespace Test
{
 /// <summary>
 /// Summary description for FakeEvent.
 /// </summary>
 public class FakeEvent : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;

  public FakeEvent()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();
  }

  #region Windows Form Designer generated code
  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point
(176, 64);
   this.button1.Name = "button1";
   this.button1.TabIndex = 0;
   this.button1.Text = "button1";
   this.button1.Click += new System.EventHandler
(this.button1_Click);
   //
   // button2
   //
   this.button2.Location = new System.Drawing.Point
(168, 168);
   this.button2.Name = "button2";
   this.button2.TabIndex = 1;
   this.button2.Text = "button2";
   this.button2.Click += new System.EventHandler
(this.button2_Click);
   //
   // FakeEvent
   //
   this.AutoScaleBaseSize = new System.Drawing.Size
(5, 13);
   this.ClientSize = new System.Drawing.Size(292,
271);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.Name = "FakeEvent";
   this.Text = "FakeEvent";
   this.ResumeLayout(false);

  }
  #endregion

  private void button1_Click(object sender, System.EventArgs
e)
  {
   BindingFlags bindingFlags = BindingFlags.Instance
| BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.InvokeMethod;
   Type myTypeBindingFlags = typeof(
System.Windows.Forms.Button );
   EventInfo eventInfo = myTypeBindingFlags.GetEvent
( "Click", bindingFlags );
   MethodInfo myMethod = eventInfo.GetRaiseMethod(
true );

   // problem is, myMethod is always null.
   if( myMethod != null )
   {
    System.EventArgs ea = new System.EventArgs
();

    // Invoke the event.
    myMethod.Invoke(
     button2
     , bindingFlags
     , new FUNBinder()
     , new Object[] {this, ea}
     ,
System.Globalization.CultureInfo.CurrentCulture);
   }
  }


  private void button2_Click(object sender, System.EventArgs
e)
  {
   MessageBox.Show( this, "Success!" );
  }
 }
}


So, if only I could make clicking the first button show the success
box...Any suggestions why myMethod always is null?

Regards,
tfn

===================================
This list is hosted by DevelopMentor�  http://www.develop.com
NEW! ASP.NET courses you may be interested in:

2 Days of ASP.NET, 29 Sept 2003, in Redmond
http://www.develop.com/courses/2daspdotnet

Guerrilla ASP.NET, 13 Oct 2003, in Boston
http://www.develop.com/courses/gaspdotnet

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to