I was wondering why it is not possible to set some properties in the winform constructor. For example, I have an extensible splitter (devcomponents library), and when I set the property Expandedto false, nothing happens in the constructor, but when I put the same code in the form load event, it works as expected. I put this code after the InitializeComponent () method.
Another situation, although a little different, is this. When you dynamically create combobox in a class and you set the selected index to another and then the first element, and then return this combo box, the selected index is set to 1 on the form.
Why is this?
Code snippet in the first situation (updated):
if (_tabId != -1)
{
this.SuspendLayout();
expandableSplitter1.SuspendLayout();
expandableSplitter1.Expanded = false;
expandableSplitter1.Enabled = false;
tabControl1.Tabs.Clear();
QuestionTab tab = new QuestionTab(_tabId);
TabItem tabItem = tabControl1.CreateTab(tab.Description);
tabItem.Tag = tab;
tabControl1.SelectedTabIndex = 0;
TabItem_Click(tabItem, null);
expandableSplitter1.ResumeLayout(true);
this.ResumeLayout(true);
}
:
public Control GenerateList(Question question)
{
ComboBox cmb = new ComboBox();
cmb.SuspendLayout();
cmb.Name = "cmb";
cmb.DisplayMember = "Answer";
cmb.ValueMember = "Id";
cmb.DataSource = answers;
cmb.Dock = DockStyle.Top;
cmb.SelectedValue = 3;
cmb.DropDownStyle = ComboBoxStyle.DropDownList;
cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
cmb.ResumeLayout(true);
return cmb;
}