简体   繁体   中英

SelectedIndexChange event not firing when using through reflection

I have a windows form with a listview control. I have a selectedIndex changed event where i am performing some action. Through reflection I am trying to set the value of the list view. But the event is not getting fired. Any help will be helpfull.

Edit

The event looks like

    private void LV1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (LV1.SelectedItems.Count>0)
        {
            if (LV1.SelectedItems[0].Text.ToString() == "testing")
            {
               // Do some work.
            }
        }

    }

I am using relection in another project and changing the selected item as follows

Assembly a = Assembly.LoadFrom(exePath);
Type formType = a.GetType(formName);
testForm = (Form)a.CreateInstance(formType.FullName);
if (testForm != null)
        {
            Type t1 = testForm.GetType();
            FieldInfo fi = t1.GetField(controlName, flags);
            object ctrl = fi.GetValue(testForm);
            ListView l1 = (ListView)ctrl;    
            l1.Items[0].Selected = true;

        }

Automating another application is fun howver not a trivial task. There's a few options but I guess most of them is out of scope for you. One would be to programatically take over the mouse and keyboard and trough those channels manage the program. Another way would be to manipulate memory, As I said neither are trivial to implement and very easily broken if the aplpication is updated.

I would suggest instead of trying to automate the application to look for infliction points. Are there any service endpoints you could automate and achieve the same result? any API or dll's used by the application you could use instead?

If you really have to automate the application there do exist several frameworks for doing that (usually build for testing purposes). The only one I can think off right now is made by Assima as is ment for training purposes.

I think your problem is here:

testForm = (Form)a.CreateInstance(formType.FullName);

You are creating a new instance of the form. I'm assuming your main project is an exe that runs an shows the form. Your second project is then another exe that runs and wants to change the selected item. By creating a new instance of the form you will be changing the selected item on the new form, not the old one.

What you need to do is somehow pass the form object over to the secondary project. possibly some static method that gets a singleton instance of the form maybe.

I'm still not entirely sure why you are using reflection, you could just give the second project a reference to the first.

The first question I'd ask is: why are you using reflection here? Just set the value through the public API. If you are messing underneath the public API, then yes: it is entirely possible that some events won't get fired.

Perhaps if you could show us exactly how you are doing this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM