Re: Can I mimic a btn_Click() ?

From: Alex Feinman [MVP] (public_news_at_alexfeinman.com)
Date: 03/09/05


Date: Tue, 8 Mar 2005 19:16:16 -0800

See if this helps:
http://www.opennetcf.org/PermaLink.aspx?guid=4cf118ee-5668-452e-af2c-f3a0c6d43a7f

In particular you want to read the part "Emulating event firing"
In the sample code instead of searching an event with type of
ListChangedEventHandler, you will need to search for an event with f.Name =
"Click"

  private void SimulateClick(Button button1)
  {
   // TO simulate the delegate invocation we obtain it's invocation list
   // and walk it, invoking each item in the list
   Type t = typeof(Control);
   FieldInfo fi = t.GetField("Click",
BindingFlags.NonPublic|BindingFlags.Instance);
   MulticastDelegate d = fi.GetValue(button1) as MulticastDelegate;
   Delegate[] list = d.GetInvocationList();
   // It is important to cast each member to an appropriate delegate type
   // For example for the KeyDown event we would replace EventHandler
   // with KeyEvenHandler and new EventArgs() with new KeyHandlerEventArgs()
   foreach(EventHandler del in list)
   {
    del.Invoke(button1, new EventArgs());
   }
  }

-- 
Alex Feinman
---
Visit http://www.opennetcf.org
"Rick" <richard.ferber@gte.net> wrote in message 
news:SbtXd.30491$QQ3.659@trnddc02...
> Hi,
>
> I have an application at work that I've inherited from a former employee.
> This app runs on a Windows CE .NET environment on an LCD screen.
> The app has several buttons on the screen that
> are clickable and have event handlers. These work fine. What I did was set
> up a UDP link from a PC to the CE device and I want to send commands from 
> my
> PC instead of clicking the button on the device. This doesn't work very
> well. It works if the action does not affect the display such as testing
> LEDs on the device. If I have operations that remove buttons from the 
> screen
> and make others appear, the buttons appear to be dead. For example my form
> gave me this event hander for the button which works well.
>
> this.m_oCalibrateLCDBtn.Click += new
> System.EventHandler(this.CalibrateLCD_Click);
>
> When I receive the remote command from my PC I try to do a similar thing
> which isn't working.
>
> CalibrateLCD_Click(this.m_oCalibrateLCDBtn, dummyEventArgs);
>
> I must be missing something from the form that the event handler has but I
> don't know what it is. Some other points are: Other remote UDP command are
> OK as long as the display is not affected. Once I'm in the
> CalibrateLCD_Click() event handler my sender and "this" look OK in my
> "Autos" window in both cases. My EventArgs is empty which is why I was 
> able
> to create a dummy. However once I start some operations that affect the
> display (removing buttons such as setting enabled and visible to false) my
> sender and my "this" get clobbered in my "Autos" window. It says 
> "undefined
> value". If I try to debug this with breakpoints things get even more 
> messed
> up. I had better luck with MessageBox(es) to debug but still am not having
> any luck. I was told to try the PerformClick() method but this is not 
> available on
> the CE.
>
> If anyone has any ideas on this I would appreciate advice.
> Rick
>
>
>