programming with esskar

Just another WordPress.com weblog

Simulate a Click

with 5 comments

Small snippet to kinda simulate a click on a control

static public class ControlExtensions
{
        static public void SimulateClick(this Control control)
        {
            if (control != null)
            {
                MethodInfo method = typeof(Control).GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);
                if (method != null)
                    method.Invoke(control, new object[] { EventArgs.Empty });                
            }
        }
}

Does the job for me – so far.

Advertisement

Written by esskar

February 17, 2010 at 7:12 am

Posted in .NET programming

Tagged with , ,

5 Responses

Subscribe to comments with RSS.

  1. This is actually good for doing automated tests. Don’t you know if there is a way in webforms?

    ivowiblo

    February 18, 2010 at 11:15 am

    • yes, it may is. do you ask for way to automate tests on webforms?

      esskar

      February 18, 2010 at 11:20 am

  2. I mean, if webforms has a kind of OnClick method to call.

    ivowiblo

    February 18, 2010 at 6:51 pm

    • Well, oviously it has (it’s in Control), but it’s not public. But one could write a wrapper around the Control/Form classes to call those On{Action} functions via Reflection calls.

      esskar

      February 19, 2010 at 6:51 am

  3. Yeah, that’s what you’ve done with winforms.

    ivowiblo

    February 19, 2010 at 1:23 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.