From my main form, I call the following to open a new form
MyForm sth = new MyForm(); sth.show();
Everything works fine, but there is a combobox on this form, which when I switch my AutoCompleteMode to suggest and add, I got this exception showing the form:
The current thread must be set to single-threaded apartment (STA) before OLE calls can be made. Make sure your main function has the STAThreadAttribute marked on it.
I set this attribute in my main function as requested by the exception:
[STAThread] static void Main(string[] args) { ...
May I get some help to figure out what might be wrong.
Code example:
private void mainFormButtonCLick (object sender, EventArgs e) {
Designer:
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";
Data Source Setup
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; }
multithreading c # thread-safety winforms
santBart
source share