VB .NET dynamic number of controls...

G

Guest

Is it possible to dynamically add controls (speciifcally
System.Windows.Forms.ProgressBar and System.Windows.Forms.Label) to a panel
to a form and have them display (and fucntion) at run time?

I've been trying todo this by creating arrays of progressbars and labels.
Then for each item in the root nods of a treeView that I need to track
progress on, I create a bar and label and add them to a panel. The problem is
that when I display the panel, its blank... no labels, no progress bars.
Here's a code snippet:

'pnlProgress declared above...
'x declared above...
Dim pgb(x) As System.Windows.Forms.ProgressBar
Dim lbl(x) As System.Windows.Forms.Label
Dim tn As myTreeNode
Dim i As Integer = 0

For Each tn In Me.treeView1.Nodes
'Set up progress bars
pgb(i) = New System.Windows.Forms.ProgressBar
pgb(i).Dock = DockStyle.Fill
pgb(i).Minimum = 0
pgb(i).Maximum = 100
pgb(i).Value = 50 just to test the bar
pgb(i).Visible = True

'Set up labels
lbl(i) = New System.Windows.Forms.Label
lbl(i).Text = tn.getSlideName() & " content progress"
lbl(i).Dock = DockStyle.Left
lbl(i).Visible = True

Me.pnlProgress.Controls.Add(pgb(i))
Me.pnlProgress.Controls.Add(lbl(i))

i += 1
Next

Do I have to declare all the objects as "Friend WithEvents " at the top of
the form before I begin (no longer dynamic...)

I pretty stuck on this... Is there something I'm missing? (hopefully
something simple)

Any help would be appreciated...

Charles
 
M

MstrControl

Hi Charles,

How's Camilla? (sorry... I couldn't help myself... :)

OK, jokes apart, it's very easy to add controls at runtime anywhere in
dotnet. Even hooking up events to any sub or function (wich was a
nightmare in VB6).

All you have to do is simply follow the lead of the designer... Check
the code the designer generates when you add a control.

Regards,

Paulo
 
G

Guest

Ok, so now that I know its possible - other than the erronous Dock values
below (just assume they are all docked to the top) - why isn't the code below
working for me? I modeled it after what the form designer created!
 
C

Cor Ligthert [MVP]

Probably are they docked each over each other.

In your situation I would first test it with docking anything

Cor
 

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