Label arrays

Kpop4life

Member
Joined
Jul 16, 2006
Messages
8
Programming Experience
Beginner
Hi, I'm trying to create a Class "Count" which will produce as many labels as stated in an argument to a given function.

The following is a function in the class, where the argument Ascend is given values from the main form by a 'for loop,' but when I run the program, it displays a simple '0'. Does anyone have a clue?


VB.NET:
Public Function Get_Number(ByVal Ascend As Integer) As Label
Labelx(Ascend).Location = New System.Drawing.Point(Ascend * 20, 20)
Labelx(Ascend).Text = CStr(Ascend)
Labelx(Ascend).Visible = True
Return (Labelx(Ascend))
End Function

Thanks,
Ben.
 
Last edited by a moderator:
I am not sure what i understand your question ...but if you want to create certain number of label controls at ones (in runtime) then you can do that as it follows:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] xLabel [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Label
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] arrLabels(0) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Label [/SIZE][SIZE=2][COLOR=#008000]'Prepare array to receive labels. Give this the necesary scope
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Get_Number([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] Ascend [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] Ascend = 1 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] 5
[/SIZE][SIZE=2][COLOR=#0000ff] ReDim [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Preserve[/COLOR][/SIZE][SIZE=2] arrLabels(Ascend) [/SIZE][SIZE=2][COLOR=#008000]'Expand array to receive next label
[/COLOR][/SIZE][SIZE=2]   xLabel = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Label [/SIZE][SIZE=2][COLOR=#008000]'Create instance of new label
[/COLOR][/SIZE][SIZE=2]   xLabel.Name = "Label" & Ascend.ToString [/SIZE][SIZE=2][COLOR=#008000]'Name label with sequential numbering
[/COLOR][/SIZE][SIZE=2]   xLabel.Width = 100
   xLabel.Text = "Label" & Ascend
   xLabel.Location = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Point(20, Ascend * 25)
[/SIZE][SIZE=2][COLOR=#0000ff]  Me[/COLOR][/SIZE][SIZE=2].Controls.Add(xLabel) [/SIZE][SIZE=2][COLOR=#008000]'Add new label to form
[/COLOR][/SIZE][SIZE=2]  arrLabels(Ascend) = xLabel [/SIZE][SIZE=2][COLOR=#008000]'Place reference to new label in appropriate element of array
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]  'The next line adds the handle of the new label to the appropriate event
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]  AddHandler[/COLOR][/SIZE][SIZE=2] xLabel.Click, [/SIZE][SIZE=2][COLOR=#0000ff]AddressOf[/COLOR][/SIZE][SIZE=2] Label1_Click
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] CreateLabelsButton_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] CreateLabelsButton.Click
    Get_Number(5)
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Label1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Label1.Click
   MessageBox.Show([/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](sender, Label).Name & " was clicked")
[/SIZE][SIZE=2][COLOR=#0000ff]  Me[/COLOR][/SIZE][SIZE=2].Close()
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

Regards ;)
 
Last edited:
I don't want to be misunderstood but it doesn't matter is it inside another or in current class ... all you need is to change the scope and instead Private you should use public (or other appropriate scopes) scope in order to access it from another class. Btw, would you mind to explain more about what you are working on as i don't seee any reason why you couldn't use the procedure (Sub) instead function.
Regards ;)
 
Hmmm, I agree that another class will be unnecessary, but I wish to make it so the user can create as many labels as he wants during runtime. For instance, user types '15' in a textbox, and it makes 15 labels, and if the user clicks on one of the labels, it will, for example, change font color.

I've tried using a for loop and nested a function, but later found it to be a newbie method.

Regards
 
Would you mind to check out the project i attached here?
Notice that you can add as many labels as you want entering value in the numericUpDown control. Just notice that if you want to recreate a new labels (a different number than present) you suppose to add code to remove the existing ones before you create new labels.

Regards ;)
 

Attachments

  • ClassCreateLabelInRuntime.zip
    53 KB · Views: 13
Back
Top