문제

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?

도움이 되었습니까?

해결책

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top