OVH Cloud OVH Cloud

Créer une ropriété personnalisée pour une form

3 réponses
Avatar
Novice
Bonjour à tous

Je cherche le moyen de créer une nouvelle propriété personnalisée dans une
form

Si quelqu'un a une idée, elle serait le bienvenu

Merci

Denis

3 réponses

Avatar
Zoury
Salut Denis! :O)

Il existe le mot clé Property qui permet de créer des propriétés
personnalisées sur des classes. Heureusement pour toi un formulaire est un
type de classe spécial.

Ex :
'***
' Form1
' 1 CommandButton
Option Explicit

Private Sub Command1_Click()

Dim frm As Form2

Set frm = New Form2

' utilise le Property Let
frm.MaProprieteCustom = "Test"

' utilise le Property Get
Debug.Print frm.MaProprieteCustom

Call frm.Show

End Sub
'***
'***
' Form2
Option Explicit

Private m_sMaProprieteCustom As String

Public Property Get MaProprieteCustom() As String
MaProprieteCustom = m_sMaProprieteCustom
End Property

Public Property Let MaProprieteCustom(ByRef sMaProprieteCustom As String)
m_sMaProprieteCustom = sMaProprieteCustom
End Property

Private Sub Form_Load()
Me.Caption = m_sMaProprieteCustom
End Sub
'***
Dans l'exemple ci-haut, j'ajoute la propriété MaNouvellePropriete au
formulaire Form2.


Si tu travailles avec des objets, tu devrais implémenté Property Set au lieu
de Let comme ceci :
'***
Private m_rsDataSource As ADODB.Recorset

Public Property Get DataSource() As ADODB.Recordset
Set DataSource = m_rsDataSource
End Property

Public Property Set DataSource(ByRef rs As ADODB.Recordset)
Set m_rsDataSrouce = rs
End Property
'***


Si tu veux que ta propriété soit Read-Only tu n'a qu'à implémenter le
Property Get sans Property Let/Set.


Dernière p'tite chose pendant que j'y suis, tu peux faire des propriétés à
plusieurs paramètres comme c'est le cas pour un tableau :
'***
' Form1
Option Explicit

Private Sub Command1_Click()

Dim frm As Form2

Set frm = New Form2
frm.Liste(0) = "Mon item préféré"
Debug.Print frm.Liste(0), frm.Liste(1)
Call frm.Show

End Sub
'***
'***
' Form2
Option Explicit

Private m_sListe() As String

Public Property Get Liste(ByRef lIndex As Long) As String
Liste = m_sListe(lIndex)
End Property

Public Property Let Liste(ByRef lIndex As Long, ByRef sValeur As String)
m_sListe(lIndex) = sValeur
End Property

Private Sub Form_Initialize()
ReDim m_sListe(2) As String
m_sListe(0) = "Item 1"
m_sListe(1) = "Item 2"
m_sListe(2) = "Item 3"
End Sub

Private Sub Form_Unload(Cancel As Integer)
Erase m_sListe
End Sub
'***

--
Cordialement
Yanick Lefebvre - MVP pour Visual Basic
http://faq.vb.free.fr/?rubrique=0 - http://www.mvps.org/vbnet/
http://www.mentalis.org/agnet/apiguide.shtml - http://www.mztools.com/

"Novice" a écrit dans le message de
news:_iDHc.41116$
Bonjour à tous

Je cherche le moyen de créer une nouvelle propriété personnalisée dans une
form

Si quelqu'un a une idée, elle serait le bienvenu

Merci

Denis




Avatar
Novice
Merci infiniement Zoury

Cela me sera utile!

Dernière question
Comment faire pour créer une propriété personnalisé dans l'objet Ambient
afin qu'elle déclenche l'événement UserControl_AmbientChanged dans un
UserControl??

Merci Encore

Denis

"Zoury" a écrit dans le message de
news:
Salut Denis! :O)

Il existe le mot clé Property qui permet de créer des propriétés
personnalisées sur des classes. Heureusement pour toi un formulaire est un
type de classe spécial.

Ex :
'***
' Form1
' 1 CommandButton
Option Explicit

Private Sub Command1_Click()

Dim frm As Form2

Set frm = New Form2

' utilise le Property Let
frm.MaProprieteCustom = "Test"

' utilise le Property Get
Debug.Print frm.MaProprieteCustom

Call frm.Show

End Sub
'***
'***
' Form2
Option Explicit

Private m_sMaProprieteCustom As String

Public Property Get MaProprieteCustom() As String
MaProprieteCustom = m_sMaProprieteCustom
End Property

Public Property Let MaProprieteCustom(ByRef sMaProprieteCustom As String)
m_sMaProprieteCustom = sMaProprieteCustom
End Property

Private Sub Form_Load()
Me.Caption = m_sMaProprieteCustom
End Sub
'***
Dans l'exemple ci-haut, j'ajoute la propriété MaNouvellePropriete au
formulaire Form2.


Si tu travailles avec des objets, tu devrais implémenté Property Set au


lieu
de Let comme ceci :
'***
Private m_rsDataSource As ADODB.Recorset

Public Property Get DataSource() As ADODB.Recordset
Set DataSource = m_rsDataSource
End Property

Public Property Set DataSource(ByRef rs As ADODB.Recordset)
Set m_rsDataSrouce = rs
End Property
'***


Si tu veux que ta propriété soit Read-Only tu n'a qu'à implémenter le
Property Get sans Property Let/Set.


Dernière p'tite chose pendant que j'y suis, tu peux faire des propriétés à
plusieurs paramètres comme c'est le cas pour un tableau :
'***
' Form1
Option Explicit

Private Sub Command1_Click()

Dim frm As Form2

Set frm = New Form2
frm.Liste(0) = "Mon item préféré"
Debug.Print frm.Liste(0), frm.Liste(1)
Call frm.Show

End Sub
'***
'***
' Form2
Option Explicit

Private m_sListe() As String

Public Property Get Liste(ByRef lIndex As Long) As String
Liste = m_sListe(lIndex)
End Property

Public Property Let Liste(ByRef lIndex As Long, ByRef sValeur As String)
m_sListe(lIndex) = sValeur
End Property

Private Sub Form_Initialize()
ReDim m_sListe(2) As String
m_sListe(0) = "Item 1"
m_sListe(1) = "Item 2"
m_sListe(2) = "Item 3"
End Sub

Private Sub Form_Unload(Cancel As Integer)
Erase m_sListe
End Sub
'***

--
Cordialement
Yanick Lefebvre - MVP pour Visual Basic
http://faq.vb.free.fr/?rubrique=0 - http://www.mvps.org/vbnet/
http://www.mentalis.org/agnet/apiguide.shtml - http://www.mztools.com/

"Novice" a écrit dans le message de
news:_iDHc.41116$
> Bonjour à tous
>
> Je cherche le moyen de créer une nouvelle propriété personnalisée dans


une
> form
>
> Si quelqu'un a une idée, elle serait le bienvenu
>
> Merci
>
> Denis
>
>




Avatar
Zoury
> Comment faire pour créer une propriété personnalisé dans l'objet Ambient
afin qu'elle déclenche l'événement UserControl_AmbientChanged dans un
UserControl??



Regarde plutôt du côté de la méthode PropertyChanged()..

Je te conseilles de lire ce document qui décrit en long et en large comment
implémenter un UserControl, comment celui-ci "vit" en mémoire, comment le
rendre persistant, etc... La section nommé [Adding Properties to Control]
concerne directement le propos de tes questions. :O)
http://msdn.microsoft.com/library/en-us/vbcon98/html/vbconpersistingpropertiesofyourolecontrol.asp

--
Cordialement
Yanick Lefebvre - MVP pour Visual Basic
http://faq.vb.free.fr/?rubrique=0 - http://www.mvps.org/vbnet/
http://www.mentalis.org/agnet/apiguide.shtml - http://www.mztools.com/