ListBox.PreferredSize.Width interesting event
-
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?
-
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?
Until the control has been fully initialized the preferred size cannot be calculated so the default value is returned. Handling the
Load
event of the user control and testingPreferredSize
in there gives the correct value so you may be better of doing your list box initialization in there.Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)