Create variable amount of labels (Visual Basic newbie)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MCourbois
    New Member
    • Nov 2006
    • 5

    Create variable amount of labels (Visual Basic newbie)

    Hi,

    I'm trying to create a programme which generates a matrix with variable sized rows and columns. Now to reduce the amount of useless code i want to have it so that the labels are automatically generated.

    I've tried creating an array of labels.

    The following code seems to work when i debug it but the labels do not show up on the form after running. It does not stall or crash it just executes the program and that's it. What code do i need to make the labels appear on the form?

    Or may it be that they just fall of the screen? Maybe i chose the coordinates wrong?.

    Code:
    Imports System.Math
    
    Public Class frmMatrix
        Dim lblKolom() As System.Windows.Forms.Label
    
        Private Sub btnBereken_Click(....
            Dim intPicX, intPicY, intPixelWaarde, intTellerX, intTellerY As Integer
            Dim intMatrix(,) As Integer
            Dim strKolom As String = ""
            Dim strPixel As String
    
    
            intPicX = Rnd() * 5 + 4
            intPicY = Rnd() * 5 + 4
            ReDim intMatrix(intPicX - 1, intPicY - 1)
            ReDim lblKolom(intPicX - 1)
    
            For intTellerX = 0 To intPicX - 1
                Me.lblKolom(intTellerX) = New System.Windows.Forms.Label
                Me.lblKolom(intTellerX).Location = New System.Drawing.Point(12, 49 + (intPicX * 24))
                Me.lblKolom(intTellerX).Name = "Kolom" + Str(intTellerX)
                Me.lblKolom(intTellerX).TabIndex = intTellerX + 2
                Me.lblKolom(intTellerX).AutoSize = True
                Me.lblKolom(intTellerX).Visible = True
                Me.lblKolom(intTellerX).TextAlign = ContentAlignment.MiddleCenter
                Me.lblKolom(intTellerX).BackColor = Color.CadetBlue
                Me.lblKolom(intTellerX).Show()
    
            Next intTellerX
    
            For intTellerX = 0 To intPicX - 1
                For intTellerY = 0 To intPicY - 1
                   ...
                Next intTellerY
                lblKolom(intTellerX).Text = strKolom
                ...
            Next intTellerX
        End Sub
    
        Private Sub Form1_Load(...
            Randomize()
        End Sub
    
    End Class
    Thanks i.a.
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Originally posted by MCourbois
    Hi,

    I'm trying to create a programme which generates a matrix with variable sized rows and columns. Now to reduce the amount of useless code i want to have it so that the labels are automatically generated.

    I've tried creating an array of labels.

    The following code seems to work when i debug it but the labels do not show up on the form after running. It does not stall or crash it just executes the program and that's it. What code do i need to make the labels appear on the form?

    Or may it be that they just fall of the screen? Maybe i chose the coordinates wrong?.

    Code:
    Imports System.Math
    
    Public Class frmMatrix
        Dim lblKolom() As System.Windows.Forms.Label
    
        Private Sub btnBereken_Click(....
            Dim intPicX, intPicY, intPixelWaarde, intTellerX, intTellerY As Integer
            Dim intMatrix(,) As Integer
            Dim strKolom As String = ""
            Dim strPixel As String
    
    
            intPicX = Rnd() * 5 + 4
            intPicY = Rnd() * 5 + 4
            ReDim intMatrix(intPicX - 1, intPicY - 1)
            ReDim lblKolom(intPicX - 1)
    
            For intTellerX = 0 To intPicX - 1
                Me.lblKolom(intTellerX) = New System.Windows.Forms.Label
                Me.lblKolom(intTellerX).Location = New System.Drawing.Point(12, 49 + (intPicX * 24))
                Me.lblKolom(intTellerX).Name = "Kolom" + Str(intTellerX)
                Me.lblKolom(intTellerX).TabIndex = intTellerX + 2
                Me.lblKolom(intTellerX).AutoSize = True
                Me.lblKolom(intTellerX).Visible = True
                Me.lblKolom(intTellerX).TextAlign = ContentAlignment.MiddleCenter
                Me.lblKolom(intTellerX).BackColor = Color.CadetBlue
                Me.lblKolom(intTellerX).Show()
    
            Next intTellerX
    
            For intTellerX = 0 To intPicX - 1
                For intTellerY = 0 To intPicY - 1
                   ...
                Next intTellerY
                lblKolom(intTellerX).Text = strKolom
                ...
            Next intTellerX
        End Sub
    
        Private Sub Form1_Load(...
            Randomize()
        End Sub
    
    End Class
    Thanks i.a.
    Hi. A couple of things spring to mind with this code.
    When you declare variables in this format:
    Dim intPicX, intPicY, intPixelWaarde, intTellerX, intTellerY As Integer
    only the last one, intTellerY is typed as an integer. The rest will be variants

    this part of the code;
    Me.lblKolom(int TellerX).Locati on = New System.Drawing. Point(12, 49 + (intPicX * 24))
    Seems to position each label with the same coordinates

    hope this helps :)

    Comment

    • MCourbois
      New Member
      • Nov 2006
      • 5

      #3
      Thanks, but if all the labels have the same position, i should still see the topmost one. I'm working with the 2005 version if this brings any extra info...

      Comment

      • LacrosseB0ss
        New Member
        • Oct 2006
        • 112

        #4
        if you're trying to position the labels in relation to each other, I think there's an attribute "Top" and "Left" or something referring to where the corners of the label will be.

        Then you can use lblName(i-1) to get the last label and use it that way. I could be thinking of a different version however. If I am, my apologies.

        Comment

        • willakawill
          Top Contributor
          • Oct 2006
          • 1646

          #5
          Originally posted by MCourbois
          Thanks, but if all the labels have the same position, i should still see the topmost one. I'm working with the 2005 version if this brings any extra info...
          OK. You may not see them yet and the logic of the code still positions them all in the same place.

          In VB6 you would set the Caption property of a label and not the Text property. This might be the 'invisible man' problem.

          Comment

          • MCourbois
            New Member
            • Nov 2006
            • 5

            #6
            But i set the backgroundcolor to Something blueish, that should show up pretty clear?

            I've also tried writing a procedure to create a label but the same thing happens. The programme goes through the entire code but nothings shows up.

            Dim intPicX, intPicY, intPixelWaarde, intTellerX, intTellerY As Integer
            only the last one, intTellerY is typed as an integer. The rest will be variants
            When i hover the mouse over each identical variable one of those yellow boxes shows up with ..... as integer.

            Comment

            • willakawill
              Top Contributor
              • Oct 2006
              • 1646

              #7
              Originally posted by MCourbois
              But i set the backgroundcolor to Something blueish, that should show up pretty clear?

              I've also tried writing a procedure to create a label but the same thing happens. The programme goes through the entire code but nothings shows up.



              When i hover the mouse over each identical variable one of those yellow boxes shows up with ..... as integer.
              Yes you are right about the declarations because you are using .NET
              You can't get away with that in VB6
              This is going to be a source of confusion for as long as you are asking a .NET question in this forum.

              Comment

              • MCourbois
                New Member
                • Nov 2006
                • 5

                #8
                Originally posted by willakawill
                Yes you are right about the declarations because you are using .NET
                You can't get away with that in VB6
                This is going to be a source of confusion for as long as you are asking a .NET question in this forum.
                Well i just learned something new. .NET <> regular VB

                So i have to be in the VB .NET forum. Oops, sry :-/

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by MCourbois
                  Well i just learned something new. .NET <> regular VB

                  So i have to be in the VB .NET forum. Oops, sry :-/
                  The VB forum covers both. You just need to let people know which you are talking about. Those who post most frequently here are VB6 developers, but we see posts about both versions. There are differences in syntax, but the logic will often be the same.

                  There is a .NET forum, but I don't know what happens over there. When in doubt, you could try posting to both.

                  Comment

                  • AricC
                    Recognized Expert Top Contributor
                    • Oct 2006
                    • 1885

                    #10
                    Pardon the heinousness of this but I am not at a PC with VS. You are trying to create a bunch of labels dynamically why don't you use the syntax Dim lblMyNewLabel As New Label(), make an array of those then use the label attributes to position them. Also, I believe in .Net to show your label/controls you need something like Me.Controls.Add (lblMyNewLabel)

                    Also, as a matter of good habit I like to declare all variables on a new line just a thought.

                    Comment

                    • willakawill
                      Top Contributor
                      • Oct 2006
                      • 1646

                      #11
                      Originally posted by Killer42
                      The VB forum covers both. You just need to let people know which you are talking about. Those who post most frequently here are VB6 developers, but we see posts about both versions. There are differences in syntax, but the logic will often be the same.

                      There is a .NET forum, but I don't know what happens over there. When in doubt, you could try posting to both.
                      In fact this is one area that is completely different in .NET
                      The ability to create a control array has disappeared. To find out how to get around this problem (which seems to be one of the most irritating in the switch to working with .NET) try reading this...

                      Comment

                      Working...