从我的主窗体调用以下代码以打开一个新窗体:
MyForm sth = new MyForm();
sth.show();
一切工作都很顺利,但是这个表单有一个组合框,当我将其AutoCompleteMode设置为suggest和append时,在显示表单时出现了以下异常:
在进行OLE调用之前,当前线程必须设置为单线程公寓 (STA) 模式。确保您的Main函数上已经标记了STAThreadAttribute。
我按照异常提示要求,在我的主函数上设置了此属性:
[STAThread]
static void Main(string[] args)
{ ...
我可以帮忙理解可能出了什么问题。以下是样例代码:
private void mainFormButtonCLick (object sender, EventArgs e)
{
// System.Threading.Thread.CurrentThread.SetApartmentState(ApartmentState.STA); ?
MyForm form = new MyForm();
form.show();
}
设计师:
this.myCombo.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
this.myCombo.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.myCombo.FormattingEnabled = true;
this.myCombo.Location = new System.Drawing.Point(20, 12);
this.myCombo.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.myCombo.Name = "myCombo";
this.myCombo.Size = new System.Drawing.Size(430, 28);
this.myCombo.Sorted = true;
this.myCombo.TabIndex = 0; phrase";
设置数据源
public MyForm(List<string> elem)
{
InitializeComponent();
populateColorsComboBox();
PopulateComboBox(elem);
}
public void PopulateComboBox(List<string> list )
{
this.myCombo.DataSource = null;
this.myCombo.DisplayMember = "text";
this.myCombo.DataSource = list;
}
form.Show()
之前添加if (Thread.CurrentThread.GetApartmentState() != ApartmentState.MTA) MessageBox.Show("Not STA");
,它会显示消息框吗? - T. Fabre