Question Adding Multiple Labels to Form

ent_ttu

Member
Joined
Jun 17, 2008
Messages
14
Programming Experience
1-3
First off, I'm pretty new to VB.NET, so I appreciate any assistance. And I have extensively searched for an answer, but I'm apparently not using the correct search terms.

I have a form that queries a database and returns a result set. What I would like to do is create X number of labels, called lblInv1, lblInv2, etc., until I have a label for each record returned.

I've verified that I'm getting the results, but when I display the form, only the first label shows; none of the others appear. I've tried several different ways, with no luck.

I've included my latest code below, and would appreciate any assistance. I'm getting pretty frustrated at this point.

Thanks in advance!

VB.NET:
Do While intLoopCount < intMaxRows
     createLabel(intLoopCount)
     intLoopCount = intLoopCount + 1
Loop

My sub function:
VB.NET:
    Private Sub createLabel(ByVal intLoopCount)
        Dim intStart As Integer
        Dim lbl As New Label
        Dim strLblId As String
        Dim strLblName As String

        strLblId = CStr(intLoopCount)
        strLblName = "lblInv" & strLblId

        MsgBox("subroutine no " & strLblName)

        lbl.Name = strLblName
        lbl.Text = "subroutine " & strLblName

        intStart = 570 + (intLoopCount * 100)

        lbl.Location = New Point(intStart, 338)
        lbl.Size = New Size(300, 300)

        Me.Controls.Add(lbl)
    End Sub
 
What is the initial value of intLoopCount? Why don't you use a for loop like this :

VB.NET:
For recordIndex As Integer = 0 To intMaxRows - 1
    createLabel(recordIndex)
Next

I assume that intMaxRows is the number of rows so I must to "-1". If it is the index of the last row, you must remove the "-1".

Also, run through this in the debugger and see if the loop runs the right number of times. Does the message box pop up the right number of times?

Btw, what I do when I need to have dynamically created controls on a form is I put them in a TableLayoutPanel that is on autosize and creates its rows automatically. It takes care of positioning the controls without the maths...
 
Thanks for the response!

The intLoopCount is initialized to 0. I have verified that the loop is firing the correct number of times; I get X number of pop-up boxes, and the value of the string changes with each one (i.e. lblInv0, lblInv1, etc).

I also replaced my loop with your suggested loop and ran the program; same results.

Thanks for the TableLayoutPanel suggestion. I'll do some research on it; maybe I can get that one to work. Once I figure out how to add multiple labels, I'm going to need to repeat the process with a dropdown box for each table row, as well. If I can figure out how to do that with the TableLayoutPanel, I'll take it.

In the mean time, any additional suggestions would be appreciated. And this may not matter, but I am using VS 2005, Professional Edition, with an MS Access db.

Thanks again!
 
Back
Top