Visual Basic Explorer
Visual Basic Explorer
 Navigation
 Home


 Coding
 Source Code

 FAQ Center

 VB Tips

 Downloads

 ToolBox

 Tutorials

 VB Games

 VB News

 VB Award

 VB Forums



 Affiliates
 Planet Source Code

 Rent a Coder

 DirectX4VB


 Misc
 Search

 Feedback

 Advertise

 About


Need to hire
a VB coder?

Please support our sponsor:

 Home 
 Site Map 
 Forums 
 News 
 Feedback 

DisconnectAddIn

When the developer uses the Add-In Manager to remove an add-in from the Add-Ins menu, the Add-In Manager invokes the add-in's DisconnectAddIn subroutine. This routine should remove any menu items and commands created by the ConnectAddIn subroutine. PropertySetter's PropSet class only needs to perform only two tasks in DisconnectAddIn. First, it uses the event connection ID saved in subroutine ConnectAddIn to disconnect the event handler from the add-in's menu item. Second, it removes the menu item from the Add-Ins menu.

Sub DisconnectAddIn(mode As Integer)

' Disconnect the event handlers.

SetPropLine.DisconnectEvents SetPropID

' Remove the command.

VBInstance.AddInMenu.MenuItems.Remove SetPropLine

End Sub

AfterClick

The AfterClick subroutine provides the add-in's functionality. It can perform such tasks as creating new controls, moving controls around on a form, and adding new forms to the project. The PropertySetter add-in displays and sets property values for the selected controls. Its AfterClick subroutine is relatively simple. First, AfterClick sets the prop_set variable in the SetterDialog form to indicate the currently running PropSet class instance. The SetterDialog will use this instance later to obtain information about the controls that the developer has selected. Subroutine AfterClick then presents the SetterDialog. The SetterDialog performs all of the rest of the work.

Public Sub AfterClick()

' Present the dialog.

Set SetterDialog.prop_setter = Me

SetterDialog.Show vbModal

Unload SetterDialog

End Sub

SetterDialog is also fairly simple. It uses functions provided by the PropSet class to perform all of the interesting work. For example, when the dialog is first loaded, it executes the following code to create the initial list of selected controls. Code to manipulate the selected controls is kept within the PropSet class, and SetterDialog deals exclusively with user interface issues.

Private Sub Form_Load()

Dim values As String

values = prop_setter.GetValues("")

ValuesText.Text = values

End Sub

The value of the rather lengthy variable VBInstance.ActiveProject.ActiveForm.SelectedControlTemplates contains a collection of the control templates for the currently selected controls, on the active form, in the active project, in the current instance of the Visual Basic development environment. Note that this collection does not hold the actual controls; it holds ControlTemplate objects that describe the controls.

ControlTemplate objects have a property named Properties. This is a collection of Property objects. The Property objects represent the actual properties of the controls themselves. The add-in can access a control's property by using the property's name as an index into the Properties collection just as it might index into any other collection. For example, to set the Name property of a control with template object control_template to "BigBox," an add-in could use either of the following two statements.

control_template.Properties.Item("Name") = "BigBox"

control_template.Properties("Name") = "BigBox"

Using this collection of selected control templates, the add-in can perform operations on all of the selected controls. The following code aligns the selected controls on the left by setting all of their Left properties to 1440.

Dim ctl As VBIDE.ControlTemplate

For Each ctl In _

VBInstance.ActiveProject.ActiveForm.SelectedControlTemplates

ctl.Properties("Left") = 1440

Next ctl

The GetValues function in PropertySetter's PropSet class uses a similar technique to obtain property values for the selected controls. It uses the On Error Resume Next statement to protect itself in cases where a control does not support the specified property. For example, this allows it to safely attempt to read the value of the Text property for a collection of controls even though some of the selected controls may not support the Text property.

The SetValues function is very similar. It first sets the value of the indicated property for all of the selected controls. Then it uses the GetValues function to return a revised list of the property values.

Public Function GetValues(prop_name As String) As String

Dim ctl As Object

Dim controls As VBIDE.SelectedControlTemplates

Dim txt As String

Dim value As String

Dim field_name As String

Dim idx As String

GetValues = ""

If NoControls() Then Exit Function

Set controls = VBInstance.ActiveProject. _

ActiveForm.SelectedControlTemplates

txt = ""

For Each ctl In controls

field_name = ctl.Properties("Name")

' Deal with indexes if the control

' is part of a control array.

idx = ctl.Properties("Index")

If idx <> "-1" Then _

field_name = field_name & _

"(" & idx & ")"

txt = txt & field_name & _

Space$(20 - Len(field_name))

On Error Resume Next

value = ctl.Properties(prop_name)

If Err.Number = 438 Then

' Property not supported.

value = ""

ElseIf Err.Number <> 0 Then

' Some other error.

Beep

MsgBox "Error reading property." & _

Error.Description, _

vbOKOnly + vbInformation, _

"Property Error"

Exit Function

End If

On Error GoTo 0

txt = txt & value & vbCrLf

Next ctl

GetValues = txt

End Function

Public Function SetValues(prop_name As String, _

prop_value As String) As String

Dim ctl As Object

Dim controls As VBIDE.SelectedControlTemplates

Dim txt As String

Dim field_name As String

Dim idx As String

SetValues = ""

If NoControls() Then Exit Function

Set controls = VBInstance.ActiveProject. _

ActiveForm.SelectedControlTemplates

For Each ctl In controls

On Error Resume Next

ctl.Properties(prop_name) = prop_value

If Err.Number <> 0 And Err.Number <> 438 Then

' Some error other than an unsupported property.

Beep

MsgBox "Error setting property." & _

Error.Description, _

vbOKOnly + vbInformation, _

"Property Error"

Exit Function

End If

On Error GoTo 0

Next ctl

SetValues = GetValues(prop_name)

End Function

These routines must be careful when accessing SelectedControlTemplates. This collection is deeply buried within objects contained in other objects contained in other objects. If any of the objects in the chain are not initialized, the program could run into trouble. For example, if no form is currently active, then VBInstance.ActiveProject.ActiveForm will have the value Nothing. In that case, the program cannot access the SelectedControlTemplates collection.

To avoid accessing the properties of an object that is not initialized, both GetValues and SetValues use function NoControls to decide if it is safe to access the SelectedControlTemplates collection. NoControls returns true if there are no controls selected and false otherwise.

Function NoControls() As Boolean

' Assume there will be trouble.

NoControls = True

' Sneak up on the collection of selected

' controls so we don't try to access an

' object that is Nothing.

If VBInstance.ActiveProject Is Nothing Then _

Exit Function

If VBInstance.ActiveProject.ActiveForm _

Is Nothing Then _

Exit Function

If VBInstance.ActiveProject.ActiveForm. _

SelectedControlTemplates.Count < 1 Then _

Exit Function

NoControls = False

End Function


[Home Page] [Tutorial Page] [Main Add-In Page] [Next Lesson] [Prev Lesson]





Home | About | What's New | Source Code | FAQ | Tips & Tricks | Downloads | ToolBox | Tutorials | Game Programming | VB Award | Search | VB Forums | Feedback | VBNews | Copyright & Disclaimer | Advertise | Privacy Policy |

Quick searches: Site Search | Advanced Site Search 

Copyright 2002 by Exhedra Solutions, Inc.
By using this site you agree to its terms and conditions
VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc.