VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



This sample program demonstrates how to add controls on a form using only code at run-time.

by Junel Corales (2 Submissions)
Category: Custom Controls/Forms/Menus
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Tue 15th August 2000
Date Added: Mon 8th February 2021
Rating: (1 Votes)

This sample program demonstrates how to add controls on a form using only code at run-time.

API Declarations


'Note: this program is my own work and please don't take credit for it
'For any suggestions and comments, just e-mail me at [email protected]
'this sample program demonstrate on how to add controls on the form using codes
'it also demonstrate on how to restart your pc within your programs.


'Note: This is the declaration section of form1-name it frmtimer


Option Explicit

'declares the controls
Private WithEvents txttime As TextBox
Private WithEvents mytimer As Timer
Private WithEvents mylabel As Label
Private WithEvents cmdclose As CommandButton
Private WithEvents StatusBar1 As StatusBar

Private Declare Function ExitWindowsEx Lib "user32" _
(ByVal uFlags As Long, _
ByVal dwReserved As Long) _
As Long

Const EWX_REBOOT = 2

'*******************************

'Note: Declaration Section of form2-name it frmgoodbye
Option Explicit

Private WithEvents mytimer As Timer
Private WithEvents mylabel As Label




Rate This sample program demonstrates how to add controls on a form using only code at run-time.



Private Sub form_load()
    Set txttime = Controls.Add("vb.textbox", "txttime")
    Set mytimer = Controls.Add("vb.Timer", "mytimer")
    Set mylabel = Controls.Add("vb.label", "mylabel")
    Set cmdclose = Controls.Add("vb.commandbutton", "cmdclose")
    Set StatusBar1 = Controls.Add("MScomctlLib.sbarctrl.2", "statusbar1")
    With cmdclose
        .Caption = "&Close"
        .Visible = True
        .Top = 2000
        .Left = (frmtimer.ScaleWidth - cmdclose.Width) \ 2
    End With
        
    With mylabel
        .AutoSize = True
        .Visible = True
        .Top = 500
        .Left = 1300
        .Caption = "This is done with pure coding"
    End With
    With txttime
        .Height = 315
        .Visible = True
        .Top = 1000
        .Left = (frmtimer.ScaleWidth - txttime.Width) \ 2
        .Alignment = vbCenter
        .Locked = True
    End With
    With mytimer
        .Enabled = True
        .Interval = 500
    End With
    With frmtimer
        .Caption = "Coding"
        .Top = (Screen.Height - Me.Height) \ 2
        .Left = (Screen.Width - Me.Width) \ 2
    End With
    With StatusBar1
        .Panels.Add 2, , , sbrDate
        .Panels(2).Alignment = sbrCenter
        .Panels.Add 3, , , sbrTime
        .Panels(3).Alignment = sbrCenter
        .Visible = True
        .Style = sbrNormal
        .Panels(1).AutoSize = sbrSpring
        .Panels(1).Alignment = sbrLeft
        .Panels(1).Style = sbrText
        .Panels(1).Text = "JSC"
    End With
End Sub
Sub cmdclose_click()
    Dim response As Integer
    response = MsgBox("are you sure you want to quit?", vbYesNo + vbQuestion)
    If response = vbYes Then
        frmgoodbye.Show
        Me.Hide
        ExitWindowsEx EWX_REBOOT, 0
    End If
End Sub


Sub mytimer_timer()
    'displays the time
    txttime.Text = Time
End Sub


'*************************
'Coding of frmgoodbye
Private Sub form_load()
    
    With frmgoodbye
        .BorderStyle = 3
        .Height = 1500
        .Left = 1000
        .MousePointer = vbHourglass
        .Top = (Screen.Height - Me.Height) \ 2
        .Left = (Screen.Width - Me.Width) \ 2
    
    End With
    
    Set mylabel = Controls.Add("vb.label", "mylabel")
    Set mytimer = Controls.Add("vb.timer", "mytimer")
    
    With mytimer
        .Enabled = True
        .Interval = 10000
    End With
    
    With mylabel
        .Visible = True
        .AutoSize = True
        .Caption = "Bye-bye!"
        .FontSize = 24
        .FontBold = True
        .Font = "copperplate Gothic Bold"
        .Top = (frmgoodbye.ScaleHeight - mylabel.Height) \ 2
        .Left = (frmgoodbye.ScaleWidth - mylabel.Width) \ 2
    End With
End Sub
Sub mytimer_timer()
    Me.Show
    Screen.MousePointer = vbHourglass
    
    End
End Sub




Download this snippet    Add to My Saved Code

This sample program demonstrates how to add controls on a form using only code at run-time. Comments

No comments have been posted about This sample program demonstrates how to add controls on a form using only code at run-time.. Why not be the first to post a comment about This sample program demonstrates how to add controls on a form using only code at run-time..

Post your comment

Subject:
Message:
0/1000 characters