Setting Properties of Object at Run Time - Sample Code

Friday, September 4, 2009 ·

Setting Object Properties at Run Time








In this posting I'll write a sample program of VB6 that demonstrates how to set object properties at run time. However some of property must be set at design time. Okay, follow below instructions:

  • Open your Visual Basic 6 application
  • Select a standard exe project type (let with default name Form1 and Project1)
  • Add your Form1 with 7 Labels, 6 TextBoxes, a CommandButton and a ComboBox
    Your design maybe look like the image on the top of this posting.
  • Change the Names of the TextBoxes into txtFName, txtAddr1, txtAddr2, txtProvince, txtCity and txtCountry
  • Change the Name of the ComboBox into cboGender
  • Change the Name of the CommandButton into cmdRegister
  • Double-click anywhere on your Form1 area to create a Sub Form_Load()
  • Write below program coding in the Sub Form_Load() area
    'setting Form1 caption
    Form1.Caption = "New Member's Registration Form"
    
    'setting the properties of Title (Label1)
    With Label1
    .Caption = "New Member's Registration Form"
    .FontSize = 18
    .FontUnderline = True
    .Left = 360
    .Top = 120
    .Width = 5775
    .Height = 495
    End With
    
    'setting the properties of Full Name label (Label2)
    With Label2
    .Caption = "Full Name"
    .FontSize = 10
    .Top = 960
    .Left = 480
    .Height = 375
    .Width = 1215
    End With
    
    'setting the properties of Gender label (Label3)
    With Label3
    .Caption = "Gender"
    .FontSize = 10
    .Top = 1320
    .Left = 480
    .Height = 375
    .Width = 1215
    End With
    
    'setting the properties of Address1 label (Label4)
    With Label4
    .Caption = "Adress1"
    .FontSize = 10
    .Top = 1680
    .Left = 480
    .Height = 375
    .Width = 1215
    End With
    
    'setting the properties of Address2 label (Label5)
    With Label5
    .Caption = "Adress2"
    .FontSize = 10
    .Top = 2040
    .Left = 480
    .Height = 375
    .Width = 1215
    End With
    
    'setting the properties of City label (Label6)
    With Label6
    .Caption = "City"
    .FontSize = 10
    .Top = 2400
    .Left = 480
    .Height = 375
    .Width = 1215
    End With
    
    'setting the properties of Province label (Label7)
    With Label7
    .Caption = "Province"
    .FontSize = 10
    .Top = 2760
    .Left = 480
    .Height = 375
    .Width = 1215
    End With
    
    'setting the properties of Country label (Label8)
    With Label8
    .Caption = "Country"
    .FontSize = 10
    .Top = 3120
    .Left = 480
    .Height = 375
    .Width = 1215
    End With
    
    'setting the properties of txtFName TextBox
    With txtFName
    .Text = ""
    .FontSize = 10
    .Top = 960
    .Left = 1680
    .Height = 330
    .Width = 4335
    End With
    
    'setting the properties of cboGender ComboBox
    With cboGender
    .Text = ""
    .FontSize = 10
    .Top = 1320
    .Left = 1680
    '      .Height = 330
    .Width = 1575
    .AddItem "Female"
    .AddItem "Male"
    End With
    
    'setting the properties of txtAddr1 TextBox
    With txtAddr1
    .Text = ""
    .FontSize = 10
    .Top = 1680
    .Left = 1680
    .Height = 330
    .Width = 4335
    End With
    
    'setting the properties of txtAddr2 TextBox
    With txtAddr2
    .Text = ""
    .FontSize = 10
    .Top = 2040
    .Left = 1680
    .Height = 330
    .Width = 4335
    End With
    
    'setting the properties of txtCity TextBox
    With txtCity
    .Text = ""
    .FontSize = 10
    .Top = 2400
    .Left = 1680
    .Height = 330
    .Width = 4335
    End With
    
    'setting the properties of txtProvince TextBox
    With txtProvince
    .Text = ""
    .FontSize = 10
    .Top = 2760
    .Left = 1680
    .Height = 330
    .Width = 4335
    End With
    
    'setting the properties of txtCountry TextBox
    With txtCountry
    .Text = ""
    .FontSize = 10
    .Top = 3120
    .Left = 1680
    .Height = 330
    .Width = 4335
    End With
    
    'setting the properties of cmdRegister CommandButton
    With cmdRegister
    .Caption = "Register"
    .FontSize = 14
    .Top = 3720
    .Left = 2880
    .Height = 495
    .Width = 1695
    End With


  • Test your program by running it.
    Your program should like below screenshot.
    Setting Object Properties at Run Time


  • 1 comments:

    Free Template said...
    September 5, 2009 at 11:01 AM  

    it's nice blog content and nice creation greeting ,I hope always happy ,health and
    sucees for you and GBU

    Post a Comment