Jalaj

January 10, 2007

Creating Custom ActiveX Controls - 2

Filed under: COM, Visual Basic — Jalaj @ 1:54 pm

The ActiveX Control created in my last post Creating Custom ActiveX Controls had limited functionality i.e. you could get the filename that was selected by the control but could not set a default one. Add to it, the Dialog Title, Default Extensions, File Filter could have made your control more useful. And more what if the same control could be used for Save As… dialog to.

Lets take all these aims for our current post.

First of define private variables that will keep the current values of the properties. m_DialogType will keep value 1 or 2 depending on whether it is “open” Dialog or the “Save” one. To faciliate a dropdown in the property box for the control let’s create Enum for it.

Dim m_FileName As String
Dim m_DialogType As Integer
Dim m_DefaultExt As String
Dim m_DialogTitle As String
Dim m_Filter As String

Enum PropDialogType
    [File Open Dialog] = 1
    [File Save Dialog] = 2
End Enum

Now define steps to be taken when a property is read or written to through Property Let & Get for each of the property.

Public Property Let FileName(ByVal strFileName As String)

    txtFileName = strFileName
    m_FileName = strFileName
    PropertyChanged "FileName"

End Property

Public Property Get FileName() As String

    FileName = m_FileName

End Property

Public Property Let DialogType(ByVal intDialog As PropDialogType)

    m_DialogType = intDialog
    PropertyChanged "DialogType"

End Property

Public Property Get DialogType() As PropDialogType

    DialogType = m_DialogType

End Property

Public Property Let DefaultExt(ByVal strDefaultExt As String)

    m_DefaultExt = strDefaultExt
    PropertyChanged "DefaultExt"

End Property

Public Property Get DefaultExt() As String

    DefaultExt = m_DefaultExt

End Property

Public Property Let DialogTitle(ByVal strDialogTitle As String)

    m_DialogTitle = strDialogTitle
    PropertyChanged "DialogTitle"

End Property

Public Property Get DialogTitle() As String

    DialogTitle = m_DialogTitle

End Property

Public Property Let Filter(ByVal strFilter As String)

    m_Filter = strFilter
    PropertyChanged "Filter"

End Property

Public Property Get Filter() As String

    Filter = m_Filter

End Property

Now the code for opening the Dialog box when the button is pressed. The value passed to the DialogType property will determine whether the dialog box will be Open or Save.

Private Sub cmdOpen_Click()

    dlgOpen.DefaultExt = m_DefaultExt
    dlgOpen.DialogTitle = m_DialogTitle
    dlgOpen.Filter = m_Filter

    If m_DialogType = 1 Then
        dlgOpen.ShowOpen
    Else
        dlgOpen.ShowSave
    End If

    if dlgOpen.FileName <> "" Then
        txtFileName = dlgOpen.FileName
    End If

End Sub

Notice that the filename as recieved by the Dialog Box is written to the TextBox which can also be manually edit to change the file name. In order to ensure that the property gets changed with change in TextBox write code for Change event of the textBox.

Private Sub txtFileName_Change()

    m_FileName = txtFileName

End Sub

The DialogType property needs to be initially set to “Open” Type.

Private Sub UserControl_InitProperties()

    Me.DialogType = 1

End Sub

All the properties that are set during the design time needs to be written to the Visual Basic form using PropBag.WriteProperty which is read at runtime using PropBag.ReadProperty.

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)

    Call PropBag.WriteProperty("FileName", m_FileName, "")
    Call PropBag.WriteProperty("DialogType", m_DialogType, 1)
    Call PropBag.WriteProperty("DefaultExt", m_DefaultExt, "")
    Call PropBag.WriteProperty("DialogTitle", m_DialogTitle, "")
    Call PropBag.WriteProperty("Filter", m_Filter, "")

End Sub

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)

    txtFileName = PropBag.ReadProperty("FileName", "")
    m_DialogType = PropBag.ReadProperty("DialogType", 1)
    m_DefaultExt = PropBag.ReadProperty("DefaultExt", "")
    m_DialogTitle = PropBag.ReadProperty("DialogTitle", "")
    m_Filter = PropBag.ReadProperty("Filter", "")

End Sub

Now with this you have much larger control over the Control.

1 Comment »

  1. hi jagan,
    u pls give me about youth details in ur news paper congrats 4 ur no.1 newspaper in india my dream is u comes to politics………plz… urs malli royal..frm tpt.

    Comment by MALLIKARJUN ROYAL — May 9, 2008 @ 10:52 am

RSS feed for comments on this post.

Leave a comment

Blog at WordPress.com.