Hello I think I've discovered some kind of bug here: I have a ListBox control. I fill its content through DataSource property. First time I set its width equal to ListBox.PreferredSize.Width, no matter what the real preferred width is, it is set to 120. From the second time use of PreferredSize.Width it works fine: the width is changed according to the content.
class MyCtrl:UserControl
{
ListBox lb;
public MyCtrl()
{
...
lb = new ListBox();
lb.Visible = false;
lb.Width = 50; //initially the width is set to 50
this.Controls.Add(lb);
...
}
public ShowMyListBox(string[] data)
{
lb.DataSource = data;
lb.Width = lb.PreferredSize.Width;
//even though initially the Width is set to 50, lb.PreferredSize.Width returns 120 on the first call
//but the following calls returns reliable preferred widths.
lb.Visible = true;
}
}
Interesting... Is it a bug? Is there any way to fix it?