| 本日在winform中设置了combox的暗昧查询功能的相关属性之后, 复制代码this.comboBoxPM.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;this.comboBoxPM.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
在运行时,报了如下错误  通过探求相关资料,相识到了STA单位模式和COM对象,总的来说:winform中,很多控件是COM对象,如许的对象只能被sta模式中的线程所调用。
 解决办法有两种: 第一种:在主方法上加上[STAThread]特性标签 复制代码        [STAThread]        static void Main()        {            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            Application.Run(new LogInForm());        }
第二种:将线程的单位状态设置为单线程单位,thread.SetApartmentState(ApartmentState.STA); 复制代码Thread th = new Thread(()=> new Form1().ShowDialog());th.SetApartmentState(ApartmentState.STA);th.Start();
终极我利用第二种方式,解决了这个标题,由于我的主窗体是在登岸窗体中以线程的方式打开的(不以线程的方式打开新的窗体的话,就不能关闭当前的窗体了。),一开始没有设置单线程单位模式,以是报了错,从前也没遇到过如许的标题,花了一些时间终于解决了这个标题,记载一下,以免忘记。 |