By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,399 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

dynamic label array generation

P: 3
Hi,

Firstly apologies for asking a similar question to one I know has been covered before but I'm quite new to visual basic and dont really understand the answers or how to get the next step. Im looking to dynamically generate a label array with three label 'boxes' per array.

Labelx(0)
Labelx(1)
Labelx(2)

The number of arrays 'x' is given by an integer value. To complicate matters slightly tho the labels are to be created within a frame on a userform. So far I have a basic attempt (very similar to another thread here!) to get even one appearing but its missing an object definition I think (tho I cant seem to find out which I need!).

x=2
For i = 1 To x
'Dim ctlNew As New UF_Inventory_Manager.Frame1.Label !!!!!!
With ctlNew
.Name = "Label" & x & Trim(CStr(i))
.Text = .Name
.Visible = True
.Width = 1335
.Top = 720
.Left = 120
End With
UF_Inventory_Manager.Controls.Add (ctlNew)
next


Any help is greatly appreciated.
Oct 26 '06 #1
Share this Question
Share on Google+
5 Replies


Expert 5K+
P: 8,434
Hi. You could try this:
  • Create a new form
  • On your form, create a frame (default name Frame1, I guess).
  • In the frame, create a label (default name Label1, I guess).
  • Set the Index property of the label to 0.
  • Set the Visible property of the label to False.
  • Add this code to the form
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Form_Click()
    2.   Dim I As Long
    3.   For I = 1 To 5
    4.     Load Label1(I)
    5.     With Label1(I)
    6.       .Move I * 180, I * 180
    7.       .Caption = "Label #" & I
    8.       .Visible = True
    9.     End With
    10.   Next
    11. End Sub
  • Run your project, and click on the form (outside the frame)
Oct 27 '06 #2

P: 3
Hi. You could try this:
  • Create a new form
  • On your form, create a frame (default name Frame1, I guess).
  • In the frame, create a label (default name Label1, I guess).
  • Set the Index property of the label to 0.
  • Set the Visible property of the label to False.
  • Add this code to the form
    Expand|Select|Wrap|Line Numbers
    1. Private Sub Form_Click()
    2.   Dim I As Long
    3.   For I = 1 To 5
    4.     Load Label1(I)
    5.     With Label1(I)
    6.       .Move I * 180, I * 180
    7.       .Caption = "Label #" & I
    8.       .Visible = True
    9.     End With
    10.   Next
    11. End Sub
  • Run your project, and click on the form (outside the frame)

Hi thanks for the reply, still no joy tho I'm afraid says it doesnt like Load Label (Am I missing a blatently obvious reference definition?). Im trying to go down the lines of your suggestion by copying existing hidden labels then pasting and repositioning them into the UF. Kinda crude but if it works at the moment I dont mind. Only snag is I cant seem to find out how to do this copy/paste action if its even possible!? Any ideas would be greatly appreciated.
Oct 31 '06 #3

Expert 5K+
P: 8,434
Keep these points in mind:
  • Check the spelling of the control's name - you might have got it wrong in the code.
  • To be able to use load, I believe the label must be a control array.
  • To make it an array, it must have a value in the Index property. Blank is not the same as zero.
If all else fails, try starting with a fresh project and going through step by step from scratch. Once it works (confident, aren't I :)) then you can play around with it.
Oct 31 '06 #4

P: 3
Yes, yes you are! For good reason too me thinks. Just did exactly what you said with it in a plain UF and worked a treat only thing is it only moves Label1(1) correctly and the rest go off the screen but I reckon that can be sorted with either a mod to the .move or by specifying a .top and .left. Hmmmm now all I have to do is massage that into my code.

As I have a series of Labels Label1(0),Label2(0) do you know of a way I can define these as at the moment I've got 'Load Label & x & (i)' where x is an integer and i the index value but it doesnt like that.

Thanks for all your help!
Oct 31 '06 #5

Expert 5K+
P: 8,434
Yes, yes you are! For good reason too me thinks. Just did exactly what you said with it in a plain UF and worked a treat only thing is it only moves Label1(1) correctly and the rest go off the screen but I reckon that can be sorted with either a mod to the .move or by specifying a .top and .left. Hmmmm now all I have to do is massage that into my code.

As I have a series of Labels Label1(0),Label2(0) do you know of a way I can define these as at the moment I've got 'Load Label & x & (i)' where x is an integer and i the index value but it doesnt like that.

Thanks for all your help!
Hm... don't think I can help with the "& x &" business, as I'm not sure I understand what you mean.

As for the label going off the screen, as far as I know the .Move method should produce exactly the same results as setting .Top and .Left properties, except that it accomplished in a single action rather than two. It's probably just something to do with your default scale mode, or something. If you're not sure where you want to position something, just shove a control there manually, then check what the Top and Left values are (shown on the "standard" toolbar, if we're talking about VB6). This one should be a simple matter of fiddling around to get the feel for it.
Oct 31 '06 #6

Post your reply

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