How to: Set and Retrieve Properties

When working with forms and controls in Visual Basic, you can set their properties programmatically at run time, or you can set them in design mode using the Properties window. The properties of most other objects, such as objects from assemblies or objects you create, can only be set programmatically.

Properties you can set and read are called read-write properties. Properties you can read but not modify are called read-only properties. Properties you can write but not read are called write-only properties.

You set the value of a property when you want to change the appearance or behavior of an object. For example, you change the Text property of a text box control to modify the contents of the text box.

You get the value of a property when you want to find the state of an object before your code performs additional actions, such as assigning the value to another object. For example, you can return the Text property of a text box control to determine the contents of the text box before running code that might change the value.

To set property values

  • Use the following syntax:

    Object.property=expression

    The following statements provide examples of how to set properties:

    ' Set the Top property to 200 twips.
    TextBox1.Top = 200
    ' Display the text box.
    TextBox1.Visible = True 
    ' Display 'hello' in the text box.
    TextBox1.Text = "hello"
    

    Note

    You can also set a property by passing it to ByRef parameters, in which case the property is modified by the result returned by the ByRef parameter.

To get property values

  • Use the following syntax:

    variable=Object.property

    You can also get a property value as part of a more complex expression, without assigning the property to a variable. The following code changes the Top property of a radio button control:

    RadioButton1.Top += 20
    

See Also

Tasks

How to: Perform Actions with Methods

Concepts

Objects from Visual Basic and Other Sources

Other Resources

Creating and Using Objects