Question

i need to instantiate forms using activator because i need to iterate all form's controls to set a property. For this procedure i'm using the code below.

using (Form frm = (Form)Activator.CreateInstance(type))
                    {
                        using (SqlConnection cn = new SqlConnection(Program.connectionString))
                        {

                            cn.Open();
                            General.ProcessControls(cn, frm, frm.GetType().Name);
                            cn.Close();
                        }
                    }

All good till here. The code above runs like a charm for all forms except one in which i have a combobox with autocomplete mode setted in SuggestAppend mode. In this form my application crashes with InvocationTargetException into function InitializeComponent() when it tries to set the autocomplete mode. Below there is the line of code where my application crashes.

 this.comboBoxAreaType.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
        this.comboBoxAreaType.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems; 

Is there any way to solve this problem?

Was it helpful?

Solution

agent-j your comments was very helpfull to understand what is going wrong.

I add the command bellow to set my thread to STA AppartmentState just before the call of thread start() and now it works like a charm.

thread.SetApartmentState(ApartmentState.STA);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top