Trucos Definir propiedades que usan los CommonDialog

VB5, VB6

Es fácil añadir los diálogos comunes LoadPicture y Font en la lista de propiedades de un UserControl. El truco está en definir las propiedades como StdPicture o StdFont, respectivamente. Por ejemplo, puedes pegar este código en un nuevo UserControl para cargar una imagen en la propiedad Picture del UserControl y cambiar la fuente de un textbox que hayas incluído. Abre un nuevo proyecto, añade un ActiveX control (UserControl), añade un textbox dentro de él y pega este código :

Option Explicit
Private Sub UserControl_InitProperties()
    ' Start with some sample text
    Text1.Text = "Some sample text"
End Sub

Private Sub UserControl_ReadProperties(PropBag _
    As PropertyBag)
    ' Restore any changes we made during design mode
    Set UserControl.Picture = _
        PropBag.ReadProperty("Image", Nothing)
    Set Text1.Font = PropBag.ReadProperty("Font", Nothing)
    Text1.Text = PropBag.ReadProperty("Text", _
        "Some sample text")
End Sub

Private Sub UserControl_WriteProperties(PropBag _
    As PropertyBag)
    ' Save any changes we made during design mode
    PropBag.WriteProperty "Image", _
        UserControl.Picture, Nothing
    PropBag.WriteProperty "Font", Text1.Font, Nothing
    PropBag.WriteProperty "Text", Text1.Text, _
        "Some sample text"
End Sub
Public Property Get Image() As StdPicture
    ' return the UserControl's image (if any)
    Set Image = UserControl.Picture
End Property
Public Property Set Image(ByVal newBackground As StdPicture)
    ' change the UserControl's background image
    Set UserControl.Picture = newBackground
    PropertyChanged "Image"
End Property
Public Property Get Font() As StdFont
    ' get the current textbox font details
    Set Font = Text1.Font
End Property
Public Property Set Font(ByVal newFont As StdFont)
    ' update the textbox font details
    Set Text1.Font = newFont
    PropertyChanged "Font"
End Property

Cierra la ventana de diseño del UserControl y añade un nuevo proyecto (exe estandard).
Añade una copia del recién creado UserControl en el formulario asegurándote de que el control es suficientemente grande para que quepa el textbox y verifica la lista de propiedades. Aparte de las estandard que provee VB la lista contiene dos nuevas : Image y Font. Por ejemplo, si cambias la propiedad Font la fuente del texto que aparece en el textbox cambia inmediatamente y si seleccionas una imagen aparece en la propiedad picture del usercontrol

-John Cullen, Pedroucos, Portugal



Trucos Trucos

Visual Basic Página de Visual Basic

Página principal Página principal

www.jrubi.com