Adding UserControl

H

hula

Hello

i have a problem with adding usercontrols to a panel.
I use a panel to add up to 20 usercontrols to a panel that has
autoscrol enabled.
The number of usercontrols to add, and the data that is passed to the
controls is determined upon selection of a combobox in the owner form.

Now most time everything works ok, but sometimes if the number of
controls to add is reduced (say from 10 controls to 2) my new added
controls are not shown at the right place, but somewhere to low in the
panel.
I use the .Top property to place the new control in the panel.
Here's the code:

//first remove all
for (int i = 0; i < panel.Controls.Count; i++) {
panel.Controls.RemoveAt(i);
}

//now add the new controls
int old_y=0;
Random r = new Random();
for (int i = 0; i < r.Next(10); i++) {
MyUserControl myCtrl = new MyUserControl();
panel.Controls.Add(myCtrl);
myCtrl.Top = old_y;
old_y += myCtrl.Height + 10;
}
panel.Refresh();

Can anyone tell me what i'm doing wrong?

_thanks in advance
 
H

hula

hmmmm.

I just figured out that not all controls are drawn correctly but
somehow invisible.
I think i'll have to read some more on adding controls dynamicly
 
M

Marc Gravell

Your RemoveAt code looks dodgy - either you need to keep removing at 0,
or you need to work downwards, not upwards. Much better, however, is to
call Clear(). Note that neither method will itself Dispose() anything -
you might want to handle that separately. Personally I use something
like:

// (note I haven't prefixed with panel., but you probably would need
to))
Control[] disposeThese = new Control[Controls.Count];
Controls.CopyTo(disposeThese, 0);
Controls.Clear();
foreach(Control disposeThis in disposeThese) disposeThis.Dispose();

Also - running r.Next(10) repeatedly will give you very little chance
of having very many... the average will *not* be 5, as you are calling
it lots of times; better to do
int count = r.Next(10);
for(int i = 1; i <= count; i++) {
// do something; note 1-based here for compatibility with your code
}

The Add code should be fine, but for performance you may wish to
SuspendLayout() during this, else use AddRange with an array of the
controls at the end. Other than that it looks fine. You might want to
look at using the stated Margin of each instance, rather that using 10
each time, but your approach will at least be consistent, so no problem
there.

Marc
 
H

hula

Marc said:
Your RemoveAt code looks dodgy - either you need to keep removing at 0,
or you need to work downwards, not upwards. Much better, however, is to
call Clear(). Note that neither method will itself Dispose() anything -
you might want to handle that separately. Personally I use something
like:

// (note I haven't prefixed with panel., but you probably would need
to))
Control[] disposeThese = new Control[Controls.Count];
Controls.CopyTo(disposeThese, 0);
Controls.Clear();
foreach(Control disposeThis in disposeThese) disposeThis.Dispose();

Also - running r.Next(10) repeatedly will give you very little chance
of having very many... the average will *not* be 5, as you are calling
it lots of times; better to do
int count = r.Next(10);
for(int i = 1; i <= count; i++) {
// do something; note 1-based here for compatibility with your code
}

The Add code should be fine, but for performance you may wish to
SuspendLayout() during this, else use AddRange with an array of the
controls at the end. Other than that it looks fine. You might want to
look at using the stated Margin of each instance, rather that using 10
each time, but your approach will at least be consistent, so no problem
there.

Marc

Many thanks Marc.

Runs great now. It was the Dispose() which i did not call at all.
 
M

Marc Gravell

Actually, I doube Dispose() would cause this problem; in most cases this
just makes the cleanup more deterministic (specifically in terms of Win32
handles), which can be useful if you are creating lots of dynamic controls.
My guess to the original problem would be the clear-down - i.e. you were
actually leaving some of the *previous* controls in place from the RemoveAt
line.

But! I'm just glad you have it sorted; happy coding ;-p

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top