Anyone have knowledge on how to make a label during run time which it depends on the user on how many label should be made i have tried some codes but it won't work.

Option Explicit
Dim m_memo, m_size, m_equation, m_jobs As String
Dim i As Integer
Dim lblfield As Label

Private Sub Form_Load()
    Form1.Width = 5055
    Form1.Height = 3600
    Frame1.Visible = True
    Frame2.Visible = False
    

End Sub
Private Sub txtmemo_KeyDown(KeyCode As Integer, Shift As Integer)
  
     If KeyCode = 13 Then
        m_memo = txtmemo.Text
        txtsize.SetFocus
     End If
End Sub
Private Sub txtsize_KeyDown(KeyCode As Integer, Shift As Integer)
   
     If KeyCode = 13 Then
        m_size = txtsize.Text
        txtjobs.SetFocus
     End If
End Sub
Private Sub txtjobs_KeyDown(KeyCode As Integer, Shift As Integer)
   
     If KeyCode = 13 Then
        m_jobs = txtjobs.Text
     End If
End Sub
Private Sub cmdsub_Click()
Dim lbl As Control
    Frame1.Visible = False
    Frame2.Visible = True
    Form1.Width = 8640
    Form1.Height = 4530
    
        If m_memo Mod 2 = 0 Then
            m_equation = m_memo / m_size
        Else
            MsgBox "invalid memory size,ERROR"
        End If
 
End Sub
Private Sub Frame2_DragDrop(Source As Control, X As Single, Y As Single)
    
  '--it displays nothing unless if i put it on the submit button but it also gives an error..but i want it to display into another frame that's why i put the code here..
    Dim lbl As Control
  
     
    For i = 0 To m_equation
     '-- down here i don't know if i get it correct i want to display it into another frame after i click the submit button
         Set lbl = Me.Controls.Add("forms.label.l") 
            With lbl
             .Top = 10
             .Left = 5
             .Height = m_memo
             .Width = 40
             .BackColor = &HFF&
             .ForeColor = &H8000&
             .Caption = "lbl" + (i)
             .Visible = True
   End With

hope you could help me with this one
regards
androidz

Recommended Answers

All 17 Replies

I didn't see your code. see the attach file. I think it will help you.

what do you mean you didnt see my code? my code was there

your parameter is missing on line 56 Set lbl = Me.Controls.Add("forms.label.l") and also your top, left and width is too small for label, don't forget for m_memo value from user input..

For i = 0 To m_equation
     
         Set lbl = Me.Controls.Add("vb.label","Label" & i) 
            With lbl
             .Top = 1000 ' 10 too small
             .Left = 1000 ' 5 too small
             .Height = m_memo ' user input??
             .Width = 1200 ' 40 too small
             .BackColor = &HFF&
             .ForeColor = &H8000&
             .Caption = "lbl" + (i)
             .Visible = True
   End With

The above code is incorrect AND all the lables will not be visible except for the last one added because the top and lefts is in exactly the same place, defying the object...

Add one label to your form and set its index to "0", its visible property to False.
Don't worry about its placement, it gets done in the code.

Option Explicit

Private Sub cmdAddControls_Click()

  Dim cnt As Integer
  Dim m_equation As Long
  
  m_equation = 6
  
  For cnt = 0 To m_equation - 1
   
     'control(0) already exists; don't try to load it!
      If cnt > 0 Then Load MyLabel(cnt)
      
     'position and show
      With MyLabel(cnt)
         .Move 300, 200 + (cnt * 300), 3500, 285
         .Caption = "Label added - index #" & cnt
         .Visible = True
      End With
   Next
   
   cmdAddControls.Caption = "Add my labels"
   
   cmdAddControls.Move MyLabel(0).Left, _
                 MyLabel(m_equation - 1).Top + _
                 MyLabel(m_equation - 1).Height + 230
End Sub

@Andre,,right,,i forgot to move a new label for they new location

No prob, we all stumble once in a while, me normally after too many beers.;) Whats your excuse?:)

i have too many tasks this week and today is crowded day..
even for taking my lunch time (actually, i forgot to have a lunch :(), so i try to reply questions without testing.. ;) and forget some basic things..

tnx guys i will try your codes..hope it works for me :)

reply questions without testing.. and forget some basic things..

I know that feeling VERY well....:)

@Androids, let us know, thanks.

hi guys i've tried your codes but it seems there's a prob

@jx_man your code given run smoothly but the problem is wont display any label. its an error free but it does not display the labels on the frame i want to display. i am only using only one form and uses frames to display other data or gui.i think the only problem is how to display it..

@andre i tried also your code but theres an error on the word Mylabel. i tried to initialize it into Mylabel as label but another error will occur when it comes to this line

With MyLabel(cnt)  '<---here it says wrong parameter..i think your using an array. but still it wont work when i change it to array it gives another error.      
.Move 300, 200 + (cnt * 300), 3500, 285        
 .Caption = "Label added - index #" & cnt         
.Visible = True     
 End With   
Next    
cmdAddControls.Caption = "Add my labels"    
cmdAddControls.Move MyLabel(0).Left, _   '<-- here also an error occured         
 MyLabel(m_equation - 1).Top + _                
 MyLabel(m_equation - 1).Height + 230

what i did is i combine your codes into this one

For i = 0 To m_equation   
If i > 0 Then Load lbl(i)'<--without this line it gives error free but no display. the error says "unable to load or unload this object"    
Set lbl = Me.Controls.Add("vb.label","Label" & i)             
With lbl             
.Top = 1000 ' 10 too small             
.Left = 1000 ' 5 too small             
.Height = m_memo ' user input??             
.Width = 1200 ' 40 too small             
.BackColor = &HFF&             
.ForeColor = &H8000&             
.Caption = "lbl" + (i)             
.Visible = True 
.Move 300, 200 + (i * 300), 3500, 285 '--this is the one from andret in order to move my labels  
End With 
             
   End With

i dont have any clues on how to display it on the frame after i clicked the button..thats the only problem left.

Post me your entire code window page's code, from the top to bottom. The MyLabel has not been declared. I want to see where and how you are using the code. I have tested it and it created and placed all fine.

Option Explicit
Dim m_memo, m_size, m_equation, m_jobs As String
Dim i As Integer

Private Sub Form_Load()    
Form1.Width = 5055    
Form1.Height = 3600    
Frame1.Visible = True    
Frame2.Visible = False  
End Sub
Private Sub txtmemo_KeyDown(KeyCode As Integer, Shift As Integer)      
If KeyCode = 13 Then       
 m_memo = txtmemo.Text        
txtsize.SetFocus     
End If
End Sub
Private Sub txtsize_KeyDown(KeyCode As Integer, Shift As Integer)      
If KeyCode = 13 Then        
m_size = txtsize.Text       
 txtjobs.SetFocus     
End If
End Sub
Private Sub txtjobs_KeyDown(KeyCode As Integer, Shift As Integer)      
If KeyCode = 13 Then       
 m_jobs = txtjobs.Text     
End If
End Sub
Private Sub cmdsub_Click()
Dim lbl As Control    
Frame1.Visible = False    
Frame2.Visible = True    
Form1.Width = 8640    
Form1.Height = 4530         
If m_memo Mod 2 = 0 Then           
 m_equation = m_memo / m_size        
Else            
MsgBox "invalid memory size,ERROR"        
End If 
End Sub
Private Sub Frame2_DragDrop(Source As Control, X As Single, Y As Single)   
Dim lbl As Control      WithOption Explicit
Dim m_memo, m_size, m_equation, m_jobs As String
Dim i As Integer
Dim lblfield As Label

Private Sub Form_Load()
    Form1.Width = 5055
    Form1.Height = 3600
    Frame1.Visible = True
    Frame2.Visible = False
    

End Sub
Private Sub txtmemo_KeyDown(KeyCode As Integer, Shift As Integer)
  
     If KeyCode = 13 Then
        m_memo = txtmemo.Text
        txtsize.SetFocus
     End If
End Sub
Private Sub txtsize_KeyDown(KeyCode As Integer, Shift As Integer)
   
     If KeyCode = 13 Then
        m_size = txtsize.Text
        txtjobs.SetFocus
     End If
End Sub
Private Sub txtjobs_KeyDown(KeyCode As Integer, Shift As Integer)
   
     If KeyCode = 13 Then
        m_jobs = txtjobs.Text
     End If
End Sub
Private Sub cmdsub_Click()
Dim lbl As Control
    Frame1.Visible = False
    Frame2.Visible = True
    Form1.Width = 8640
    Form1.Height = 4530
    
        If m_memo Mod 2 = 0 Then
            m_equation = m_memo / m_size
        Else
            MsgBox "invalid memory size,ERROR"
        End If
 
End Sub
Private Sub Frame2_DragDrop(Source As Control, X As Single, Y As Single)
dim lbl as label

For i = 0 To m_equation
     'If i > 0 Then Load lbl
         Set lbl = Me.Controls.Add("vb.label", "label" & i)
            With lbl
             .Top = 1000 ' 10 
             .Left = 1000 ' 5 
             .Height = m_size ' 
             .Width = 1200 ' 40 
             .BackColor = &H0&
             .ForeColor = &H8000&
             .Caption = "flote" & i
             .BorderStyle = 1
             .Visible = True
             .Enabled = True
             .Move 300, 200 + (i * 300), 3500, 285
              
          End With
   next i
     thats all my code there..the frame2 is where i want to display my labels 

end sub

I'm still working through your code.

This part -

With MyLabel(cnt) '<---here it says wrong parameter..i think your using an array. but still it wont work when i change it to array it gives another error.
.Move 300, 200 + (cnt * 300), 3500, 285
.Caption = "Label added - index #" & cnt
.Visible = True
End With
Next

where the error occurred, did you add a label to your form called MyLabel and have you set its index to 0? That is why you got the error.

thanks to both of you guys i already got it.it worked i also made a textbox the same with label but is there any way that i can get the value what the user will input on that textbox i made at run time?

Yes, you can get the input, but thats a whole new question. Lets keep the answers to the topic, in this case the creation of labels during run time. Please open a new thread with your question. I'll answer there.:)

Also, please mark this as solved, found at the bottom of the page, thanks.:)

oops sorry i forgot to mark it. ok see you there guys tnx

Thanks. See you there in 5.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.