Changes to Control Functionality

In addition to changes in the names of the properties, methods, and events, the functionality of the controls might also differ. For example, Windows Forms controls have a different implementation for handling data access and fonts.

Fonts

Using VBA, you can set the font of a control by setting the font properties directly. In Visual Basic 2005, you must create an instance of a System.Drawing.Font object whenever you want to programmatically set the font property of a control. Listing 4.16 shows how to change the text and font used on a CommandButton in VBA and a Button in Visual Basic 2005. When you create a Font object in Visual Basic 2005, you must choose one of ten overloaded constructors. In Listing 4.16, we pass the font size and style when we instantiate the Font object and then assign it to the Font property of the Button.

Listing 4.16. Setting the font on a control ' VBA

Private Sub CommandButton1_Click()

Me.CommandButton1.Caption = "Sample" With Me.CommandButton1.Font .Name = "Courier New" .Size = 8 .Italic = True End With End Sub

' Visual Basic 2005

Private Sub Button1_Click (ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click

Me.Button1.Text = "Sample"

Dim myFont as new System.Drawing.Font("Courier New", _

8, FontStyle.Italic) Me.Button1.Font = myFont

End Sub

Colors

In VBA, colors are of type Long, and there are eight constants that can be used: vbBlack, vbRed, vbGreen, vbYellow, vbBlue, vbMagenta, vbCyan, and vbWhite. In Visual Studio 2005, colors are of type Color, and there are more than 100 choices. Why set text to blue when you can choose DarkSlateBlue or DeepSkyBlue, as shown in Figure 4.9?

iT) Color.DarkOrchid

LH Color.DarkRed

IZI Color.DarkSalmon

IZI Color.DarkSeaGreen

LH Color.DarkSlateBlue

CD Color.DarkSlateGray

CD Color.DarkTurquoise

LH Color.DarkViolet

L=3 Color.DeepPink

|TI BBBBS^^

V

Figure 4.9. Setting the ForeColor of a Button to DeepSkyBlue

Controls inherit the colors of their parents (the form) in Visual Basic 2005. Listing 4.17 shows that to set the ForeColor of a CommandButton on a UserForm in VBA, you must set the ForeColor of the control; setting the ForeColor of the UserForm does not affect the color of any of its controls. However, in Visual Basic 2005, if you set the ForeColor property of the Windows Form, all the controls on the form automatically inherit the color setting.

Listing 4.17. Inheriting ForeColor property settings ' VBA

Private Sub CommandButton1_Click()

Me.ForeColor = vbBlue End Sub

' Visual Basic 2005

Private Sub Button1_Click (ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Buttonl.Click

Me.ForeColor = Color.Blue

Using VBA, you can also set the color using the RGB function, where you pass an integer value between 0 and 255 for each color component: red (R), green (G), and blue (B). For example, to set the ForeColor of a control to blue, you can set the property to RGB(0, 0, 255). In Visual Basic 2005, you can assign only a value of type System.Drawing.Color.

You can use the ColorTranslator class to convert an RGB value to a Color structure. Listing 4.18 shows how to set the ForeColor of a CommandButton control using an RGB function in VBA. It also shows how to translate the RGB value to a Color structure in Visual Basic 2005 so that it can be set to the ForeColor property of a Button control.

Listing 4.18. Using RGB in Vsual Basic 2005 ' VBA

Private Sub CommandButton1_Click()

Me.CommandButton1.ForeColor = RGB(0, 0, 255) End Sub

' Visual Basic 2005

Private Sub Button1_Click (ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click

Dim colorValue as Integer = RGB(0, 0, 255) Me.Button1.ForeColor = System.Drawing.ColorTranslator _ .FromOle(colorValue)

End Sub

You can also use the FromArgb method of a Color structure: Me.Button1.ForeColor = System.Drawing.Color.FromArgb(0, 0, 255)

Displaying a Form

To display a form in VBA, you call the Show method of the form. You can indicate whether to display the form as modal or modeless by passing vbModal or vbModeless. If you do not pass either parameter, the form shows as modal by default.

In Visual Basic 2005, forms are classes, and you must instantiate the form before you can display it. You call the Show method to display a modeless form, and you call the ShowDialog method to show the form as a modal dialog box. Listing 4.19 demonstrates how to show a modal UserForm using VBA, and a modal Windows Form using Visual Basic 2005. The code is called from the Click event handler of one form, which causes the second form to be displayed.

Listing 4.19. Displaying a form in VBA versus Visual Basic 2005 ' VBA

Private Sub CommandButton1_Click()

UserForm2.Show (vbModal) End Sub

' Visual Basic 2005

Private Sub Button1_Click (ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click

Dim myForm As New Form2 myForm.ShowDialog()

End Sub

Size and Location

In VBA, you adjust the size of a control by using the Height and Width properties. The unit of measurement for these properties is twips. In Visual Basic 2005, the Height and Width properties are combined into a single Size property, which is measured in pixels.

When you display a form, you often want to control exactly where it will appear on the screen. In VBA, you set the StartUpPosition property. For example, you can set the StartUpPosition property to Manual and then set the Top property to 0 and the Left property to 0, displaying the form in the upper-left corner of the application.

To manually control the position of a Windows Forms control using Visual Basic 2005, you use the StartPosition and Location properties. The location is calculated (in pixels) in relation to the upper-left portion of the display area. You can instead specify the location by using the form's Left and Top properties directly. Listing 4.20 shows how to display a UserForm and a Windows Form in the upper-left corner of the screen.

Listing 4.20. Setting the location of a UserForm and a Windows Form ' VBA

Private Sub UserForm_Initialize() Me.StartUpPosition = Manual Me.Left = 0 Me.Top = 0 End Sub

' Visual Basic 2005

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e _ As System.EventArgs) Handles MyBase.Load

Me.StartPosition = FormStartPosition.Manual Me.Location = New Point(0, 0)

End Sub

You can move the form to a new location by using these properties; or you can use the Move method in VBA, or the SetBounds method in Visual Basic 2005. Both of these techniques require that you indicate the location and size of the form. You can pass the Form's current height and width, as shown in Listing 4.21.

Listing 4.21. Changing the location of a UserForm and a Windows Form ' VBA

Private Sub CommandButton1_Click() Dim UserFormHeight As Integer Dim UserFormWidth As Integer

UserFormHeight = Me.Height UserFormWidth = Me.Width

Me.Move 0, 0, UserFormWidth, UserFormHeight End Sub

' Visual Basic 2005

Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim formHeight As Integer = Me.Height Dim formWidth As Integer = Me.Width Me.SetBounds(0, 0, formWidth, formHeight)

End Sub

Control Arrays

Control arrays are not supported in Visual Basic 2005; however, you can implement the same functionality by extending an event handler to handle multiple controls. Notice in Listing 4.21 that the Button1 Click event handler has a Handles clause at the end of the statement (Handles Buttonl.Click). To implement this event handler for multiple controls, you can add references to the controls in the Handles clause. For example, you can indicate that the Click event handler of Button1 should also handle the Click event of Button2, as shown in Listing 4.22.

Listing 4.22. Handling the events of multiple controls ' Visual Basic 2005

Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, _ Button2.Click

Dim formHeight As Integer = Me.Height Dim formWidth As Integer = Me.Width Me.SetBounds(0, 0, formWidth, formHeight)

Dynamic Controls

In addition to adding controls to a document or worksheet by dragging them from Toolbox, you can add Windows Forms controls programmat-ically. The way you add these controls to a document differs from the way you would add them to a Windows Form; VSTO provides a number of helper methods that eases the task of adding these controls to your documents. You will learn more about adding controls to a document in Chapter 8.

Data Binding

You can fill ActiveX controls with data in VBA by using an ActiveX Data Object (ADO). For example, you can fill a ComboBox with data by reading the ADO recordset, adding each row of the recordset to an array and then assigning the array to the List property of the control. Using Visual Basic 2005, you can bind a control directly to a data source using the control's DataBindings property. You can bind to a data source such as a column in a database, or to any structure that contains data, including XML. You can also bind any property of the control to a data source.

Windows Forms controls support either simple data binding or complex data binding. Simple data binding enables you to bind one element of data to a control. Complex data binding enables you to bind a control to more than one data element, and it is typically supported in controls that display lists of information.

Programmatically, you can data-bind a control using the Add method of the control's DataBindings property, passing three parameters: the property you want to bind the data to, the data source, and the data member. For example, to bind data to a text box, you could use code similar to this:

TextBox1.Databindings.Add("Text", ds, data.member)

It is much easier to bind data to controls at design time using the Data Sources window and dragging data-bound controls to the document or to a Windows Form. Data binding is covered in more detail in Chapters 6 and 7.

0 0

Post a comment