Wednesday, February 9, 2011

Dynamic Label Control

      While displaying data in  a label from a database we have to load label control dynamically on a form in VB6. I have generated a simple bill  for a small restaurant.Bill will be generated on selected menu from another form. First form is shown ablove.
Second form is shown below after selecting items




Code for loading dynamic label control with data in Vb6


Option Explicit
Dim i As Integer
Dim lblBill1 As Label
Dim lblBill2 As Label
Dim totalbill As Long
Dim totalAmount As String




Private Sub Form_Load()
Dim parsepamount As String
Dim tempbill As String
'Creating dynamic Control Array
For i = 1 To frmMenu.lstSelectMenu.ListCount
Load lblBill(i)
lblBill(i).Left = 2980
lblBill(i).Top = i * 350
lblBill(i).Visible = True
lblBill(i).Caption = frmMenu.lstSelectMenu.List(i - 1)
parsepamount = Right(lblBill(i).Caption, 3)
tempbill = Trim(parsepamount)
totalbill = CLng(tempbill) + totalbill
'MsgBox (totalbill)
Next
totalAmount = CStr(totalbill)
Set lblBill1 = Controls.Add("Vb.Label", "lblBill1")
lblBill1.Left = 2900
lblBill1.Top = (frmMenu.lstSelectMenu.ListCount + 1) * 350
lblBill1.Visible = True
lblBill1.Caption = "--------------------------"
Set lblBill2 = Controls.Add("Vb.Label", "lblBill2")
lblBill2.Left = 2900
lblBill2.Top = (frmMenu.lstSelectMenu.ListCount + 3) * 350
lblBill2.Visible = True
lblBill2.Caption = "Total Bill: " & totalAmount
totalbill = 0
End Sub