468,301 Members | 1,492 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,301 developers. It's quick & easy.

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?.

Expand|Select|Wrap|Line Numbers
  1. Imports System.Math
  2.  
  3. Public Class frmMatrix
  4.     Dim lblKolom() As System.Windows.Forms.Label
  5.  
  6.     Private Sub btnBereken_Click(....
  7.         Dim intPicX, intPicY, intPixelWaarde, intTellerX, intTellerY As Integer
  8.         Dim intMatrix(,) As Integer
  9.         Dim strKolom As String = ""
  10.         Dim strPixel As String
  11.  
  12.  
  13.         intPicX = Rnd() * 5 + 4
  14.         intPicY = Rnd() * 5 + 4
  15.         ReDim intMatrix(intPicX - 1, intPicY - 1)
  16.         ReDim lblKolom(intPicX - 1)
  17.  
  18.         For intTellerX = 0 To intPicX - 1
  19.             Me.lblKolom(intTellerX) = New System.Windows.Forms.Label
  20.             Me.lblKolom(intTellerX).Location = New System.Drawing.Point(12, 49 + (intPicX * 24))
  21.             Me.lblKolom(intTellerX).Name = "Kolom" + Str(intTellerX)
  22.             Me.lblKolom(intTellerX).TabIndex = intTellerX + 2
  23.             Me.lblKolom(intTellerX).AutoSize = True
  24.             Me.lblKolom(intTellerX).Visible = True
  25.             Me.lblKolom(intTellerX).TextAlign = ContentAlignment.MiddleCenter
  26.             Me.lblKolom(intTellerX).BackColor = Color.CadetBlue
  27.             Me.lblKolom(intTellerX).Show()
  28.  
  29.         Next intTellerX
  30.  
  31.         For intTellerX = 0 To intPicX - 1
  32.             For intTellerY = 0 To intPicY - 1
  33.                ...
  34.             Next intTellerY
  35.             lblKolom(intTellerX).Text = strKolom
  36.             ...
  37.         Next intTellerX
  38.     End Sub
  39.  
  40.     Private Sub Form1_Load(...
  41.         Randomize()
  42.     End Sub
  43.  
  44. End Class
Thanks i.a.
Nov 13 '06 #1
10 3811
willakawill
1,646 1GB
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?.

Expand|Select|Wrap|Line Numbers
  1. Imports System.Math
  2.  
  3. Public Class frmMatrix
  4.     Dim lblKolom() As System.Windows.Forms.Label
  5.  
  6.     Private Sub btnBereken_Click(....
  7.         Dim intPicX, intPicY, intPixelWaarde, intTellerX, intTellerY As Integer
  8.         Dim intMatrix(,) As Integer
  9.         Dim strKolom As String = ""
  10.         Dim strPixel As String
  11.  
  12.  
  13.         intPicX = Rnd() * 5 + 4
  14.         intPicY = Rnd() * 5 + 4
  15.         ReDim intMatrix(intPicX - 1, intPicY - 1)
  16.         ReDim lblKolom(intPicX - 1)
  17.  
  18.         For intTellerX = 0 To intPicX - 1
  19.             Me.lblKolom(intTellerX) = New System.Windows.Forms.Label
  20.             Me.lblKolom(intTellerX).Location = New System.Drawing.Point(12, 49 + (intPicX * 24))
  21.             Me.lblKolom(intTellerX).Name = "Kolom" + Str(intTellerX)
  22.             Me.lblKolom(intTellerX).TabIndex = intTellerX + 2
  23.             Me.lblKolom(intTellerX).AutoSize = True
  24.             Me.lblKolom(intTellerX).Visible = True
  25.             Me.lblKolom(intTellerX).TextAlign = ContentAlignment.MiddleCenter
  26.             Me.lblKolom(intTellerX).BackColor = Color.CadetBlue
  27.             Me.lblKolom(intTellerX).Show()
  28.  
  29.         Next intTellerX
  30.  
  31.         For intTellerX = 0 To intPicX - 1
  32.             For intTellerY = 0 To intPicY - 1
  33.                ...
  34.             Next intTellerY
  35.             lblKolom(intTellerX).Text = strKolom
  36.             ...
  37.         Next intTellerX
  38.     End Sub
  39.  
  40.     Private Sub Form1_Load(...
  41.         Randomize()
  42.     End Sub
  43.  
  44. 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(intTellerX).Location = New System.Drawing.Point(12, 49 + (intPicX * 24))
Seems to position each label with the same coordinates

hope this helps :)
Nov 13 '06 #2
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...
Nov 14 '06 #3
LacrosseB0ss
113 100+
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.
Nov 14 '06 #4
willakawill
1,646 1GB
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.
Nov 14 '06 #5
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.
Nov 15 '06 #6
willakawill
1,646 1GB
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.
Nov 15 '06 #7
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 :-/
Nov 15 '06 #8
Killer42
8,434 Expert 8TB
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.
Nov 15 '06 #9
AricC
1,892 Expert 1GB
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.
Nov 21 '06 #10
willakawill
1,646 1GB
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...
Nov 21 '06 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Tom Rathbun | last post: by
83 posts views Thread by Alexander Zatvornitskiy | last post: by
18 posts views Thread by Tinus | last post: by
9 posts views Thread by Shapper | last post: by
5 posts views Thread by Mel | last post: by
4 posts views Thread by Macneed | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.