Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Setting and Retrieving Properties

When working with forms and controls in Visual Basic .NET, 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:

TextBox1.Top = 200   ' Sets the Top property to 200 twips.
TextBox1.Visible = True   ' Displays the text box.
TextBox1.Text = "hello"   ' Displays 'hello' in the text box.
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:

Protected Sub RadioButton1_CheckedChanged(ByVal sender As Object, _
      ByVal e As System.EventArgs)
   ' [statements]
   RadioButton1.Top += 20
   ' [statements]
End Sub

See Also

The Basics of Working with Objects | Performing Actions with Methods | Objects from Visual Basic and Other Sources | Creating and Using Objects

© 2015 Microsoft