I was wondering why it isn't possible to set some properties in the constructor of a winform. For example, I have an Expandable splitter (devcomponents library) and when i set the property Expanded
to false, in the constructor nothing happens, but when I put the same code in the form load event, it works like expected. I am putting this code after the InitializeComponent() method.
Another situation, slighty different though, is this. When you dynamically create a combobox in a class and you set the selected index other then the first item and then return this combobox, the selected index is set back to 1 on the form.
Why is this?
Code snippet on the first situation (updated):
// This works in the form_load event but not in the form constructor (after InitializeComponent())
if (_tabId != -1)
{
this.SuspendLayout();
expandableSplitter1.SuspendLayout();
expandableSplitter1.Expanded = false;
expandableSplitter1.Enabled = false;
// Hide all tabs, except the selected tab
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);
}
Code snippet for the second situation:
public Control GenerateList(Question question)
{
// some code is omitted
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; // not the first index
cmb.DropDownStyle = ComboBoxStyle.DropDownList;
cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
cmb.ResumeLayout(true);
return cmb;
}