Weirdest problem with Windows forms control.
Date: 09/05/05
(C Sharp) Keywords: no keywords
Hi guys,
I have faced this weirdest problem with C#.NET 2.0 (Windows Forms).
I have a class that creates an instance of a (Control) Slider() and passes an instance of CheckedListBox() to it. I set the size of CheckedListBox() to (160,100).
The Slider() it its constructor adds the CheckedListBox() that was passed to it to its Contols collection (this.Controls.Add(PassedControl);)
Then I add the Slider() to the form as well.
It looks something like this:
class a
{
using ...;
pulic a()
{
CheckedListBox box = new CheckedListBox();
box.Size = new Size(160,100);
myPanel x = new myPanel(box);
Contols.Add(x);
}
}
Where the myPanel looks something like this:
Class myPanel : System.Windows.Forms.Panel{
protected Control inner;
public myPanel(Contol _ctrl){
inner = _ctrl;
this.Controls.Add(inner);
}
}
My problem is that once the CheckedListBox() is added to the panel, it will change its size a bit. I guess .NET does this to actually make its size be x*Item.Height; so that it looks good or something.
Now, somewhere in the constructor for the myPanel I am trying to get the actual size of the CheckedListBox() and here the weird part starts.
It will always return 100. But the real size of it is 85.
The way I figured this out is this - add "int x = inner.size.height;" and then put a break right after this line.
You will see the both the x and inner.size.height are 100;
now - add the inner to quick watch and it will somehow recalculate the size and the inner.size.height will become 85 but x will remain 100.
Am I missing something or gone mad or what? But when I run this it will not return its real size unless I do the trick with quick watch for "inner". Both the inner.ClientSize and inner.Bounds return same 100 unless "quick watched".
I tried tricks with ResumeLayout() and PerformLayout() and Refresh() and Invalidate() - these don't seem to be helping a lot.
Any ideas, this really is killing me now?
And thanks a lot.
Source: http://www.livejournal.com/community/csharp/34128.html