Faire un clone des paramètres d'un picturebox en .Net ?

Résolu
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 - 15 janv. 2011 à 22:45
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 - 23 janv. 2011 à 17:52
Le code fonctionne bien.
J'ai juste un objet PictureBox appelé picCounter que je clone
en créant un nouvel objet en le décalant vers la gauche.
My.Resources.Compteur_0 c'est mon image du chiffre 0
My.Resources.Compteur_1 c'est mon image du chiffre 1

Ma question:
Est il possible de faire un clone des paramètres de mon picture source,
pour les coller sur mon nouvel objet picturebox ?
(voir en dessous du commentaire: 'C'est très lourd le paramétrages !')

Le code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'J'affiche une image du chiffre 0.
        picCounter.BackgroundImage = My.Resources.Compteur_0

        'Je créer un nouvel objet PictureBox
        Dim newCompteur As New PictureBox

        'Je charge l'image du chiffre 1.
        newCompteur.BackgroundImage = My.Resources.Compteur_1

        'C'est très lourd le paramétrages !
        newCompteur.Visible = False
        newCompteur.BackgroundImageLayout = ImageLayout.None
        newCompteur.BackColor = Color.Transparent
        newCompteur.Left = picCounter.Left - 19
        newCompteur.Top = picCounter.Top
        newCompteur.Width = picCounter.Width
        newCompteur.Height = picCounter.Height
        newCompteur.Visible = True

        'J'ajoute mon objet aux controles et je l'affiche
        Controls.Add(newCompteur)
        newCompteur.Show()
    End Sub

11 réponses

Utilisateur anonyme
18 janv. 2011 à 18:19
Salut,
Je viens peut-être un peu tard mais il me semble que l'héritage serait le mieux approprié pour ce que tu veut faire.
Il s'agit de créer une classe clsDigit par exemple héritant de la classe PictureBox standard

Public Class clsDigit
    Inherits PictureBox

    Sub New(ByVal X As Integer, ByVal Y As Integer, ByVal Image As Image)
        With Me
            .Location = New Point(X, Y)
            .BackColor = Color.Red
            .Width = 10
            .Image = Image
            ' ..... ainsi de suite
        End With
    End Sub
End Class


et de les intégrer ensuite au chargement du formulaire :

Dim pct1 As New clsDigit(0, 0, CType(My.Resources.image1, Image))
Dim pct2 As New clsDigit(20, 0, CType(My.Resources.image2, Image))


Me.Controls.Add(pct1)
Me.Controls.Add(pct2)

A+
3
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
16 janv. 2011 à 12:48
Allez aidez moi :(

Est il possible d'énumérer les propriétés de mon objet picturebox source pour que je puisse les utiliser pour mon nouvel objet picturebox ?
0
NHenry Messages postés 15113 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 22 avril 2024 159
16 janv. 2011 à 13:11
Bonjour,

Je pense qu'avec la "reflection", c'est possible, mais je ne sais pas clairement comment tu pourrais faire.

0
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
16 janv. 2011 à 13:15
Merci pour l'indice Henry; si j'y arrive je partagerai le code source ^^
Heureux de te lire; ++
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
16 janv. 2011 à 23:26
Re Henry !

Je suis crevé !!!
Le clone fonctionne, le soucis c'est que l'object drawing contenu dans mon clone (picObjet.Backgroundimage) est H.S ???

Il ne veut pas charger l'image du chiffre 1 !??
Bizarre non ???

A quoi ressemble mon test:


Ne sachant toujours pas utiliser les Lambda et connaissant a peine la Reflection; Voila mon code:
Public Class Form1

    Function ClonePictureBox(ByVal PictureBoxSrc As PictureBox) As PictureBox
        Dim PictureBoxDst As New PictureBox
        Dim PictureBoxDstProp As System.Reflection.PropertyInfo() = PictureBoxDst.GetType.GetProperties
        Dim PictureBoxSrcProp As System.Reflection.PropertyInfo() = PictureBoxSrc.GetType.GetProperties

        If PictureBoxDstProp.Length <> PictureBoxSrcProp.Length Then
            MessageBox.Show("Les propriétées entre les deux PictureBox n'ont pas le même nombre de paramètre !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return Nothing
        End If



        For Each SourceProperty As System.Reflection.PropertyInfo In PictureBoxSrc.GetType.GetProperties
            For Each DestinationProperty As System.Reflection.PropertyInfo In PictureBoxDst.GetType.GetProperties
                If DestinationProperty.CanWrite = True Then
                    Dim dstOBJ As Object = DestinationProperty.GetValue(PictureBoxDst, Nothing)
                    Dim srcOBJ As Object = SourceProperty.GetValue(PictureBoxSrc, Nothing)
                    If (DestinationProperty.Name SourceProperty.Name) AndAlso (DestinationProperty.PropertyType SourceProperty.PropertyType) Then
                        DestinationProperty.SetValue(PictureBoxDst, srcOBJ, Nothing)
                        Exit For
                    End If
                End If
            Next
        Next
        
        Dim gParent As Control = PictureBoxSrc.Parent
        gParent.Controls.Add(PictureBoxDst)
        Return PictureBoxDst
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
        Dim ChoixBouton As System.Windows.Forms.Button = sender

        Select Case ChoixBouton.Name
            Case "Button1"
                Me.PictureBox1.BackgroundImage = My.Resources.Image_0

            Case "Button2"
                Dim picObjet As PictureBox = ClonePictureBox(PictureBox1)
                picObjet.BackgroundImage = My.Resources.Image_1
                picObjet.Left = PictureBox1.Left + PictureBox1.Width
        End Select
        
    End Sub
End Class
0
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
17 janv. 2011 à 00:33
J'arrête pour ce soir, j'ai encore essayé un tas de méthode...
J'y suis presque mais en vain !!!

Je clique sur le bouton 1
Je clique sur le bouton 2
je minimise la fenêtre et la ré-ouvre, résultat impressionnant !!!????


Code actuel:
Public Class Form1

    Class Clone
        Friend Shared PictureBoxDst As PictureBox

        Sub New()
            Clone.PictureBoxDst = New PictureBox
        End Sub

        Protected Overrides Sub Finalize()
            Clone.PictureBoxDst = Nothing
            MyBase.Finalize()
        End Sub

        Friend Shared Function ClonePictureBox(ByVal PictureBoxSrc As PictureBox) As PictureBox
            Dim PictureBoxDstProp As System.Reflection.PropertyInfo() = Clone.PictureBoxDst.GetType.GetProperties
            Dim PictureBoxSrcProp As System.Reflection.PropertyInfo() = PictureBoxSrc.GetType.GetProperties

            If PictureBoxDstProp.Length <> PictureBoxSrcProp.Length Then
                MessageBox.Show("Les propriétées entre les deux PictureBox n'ont pas le même nombre de paramètre !", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return Nothing
            End If

            Dim dstOBJ As Object = Nothing
            Dim srcOBJ As Object = Nothing
            For Each SourceProperty As System.Reflection.PropertyInfo In PictureBoxSrcProp
                For Each DestinationProperty As System.Reflection.PropertyInfo In PictureBoxDstProp
                    If DestinationProperty.CanWrite = True Then
                        dstOBJ = DestinationProperty.GetValue(Clone.PictureBoxDst, Nothing)
                        srcOBJ = SourceProperty.GetValue(PictureBoxSrc, Nothing)
                        If (DestinationProperty.Name SourceProperty.Name) AndAlso (DestinationProperty.PropertyType SourceProperty.PropertyType) Then
                            DestinationProperty.SetValue(Clone.PictureBoxDst, srcOBJ, Nothing)
                            Exit For
                        End If
                    End If
                Next
            Next

            Dim gParent As Control = PictureBoxSrc.Parent
            gParent.Controls.Add(Clone.PictureBoxDst)
            Return Clone.PictureBoxDst
        End Function
    End Class

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
        Dim ChoixBouton As System.Windows.Forms.Button = sender

        Select Case ChoixBouton.Name
            Case "Button1"
                Me.PictureBox1.BackgroundImage = My.Resources.Image_0

            Case "Button2"
                Dim NewClone As New Clone
                Dim NewPicture As PictureBox = Clone.ClonePictureBox(PictureBox1)
                Clone.PictureBoxDst.Image = My.Resources.Image_1
                Clone.PictureBoxDst.Left = PictureBox1.Left + PictureBox1.Width
        End Select

    End Sub
End Class

0
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
17 janv. 2011 à 08:37
Je sèche, aidez moi svp !!!

Public Class Form1

    Shared Sub ClonePictureBox(ByVal PictureBoxSrc As System.Windows.Forms.PictureBox, ByRef PictureBoxDst As System.Windows.Forms.PictureBox)
        Dim srcOBJ As Object = Nothing
        For Each SourceProperty As System.Reflection.PropertyInfo In PictureBoxSrc.GetType.GetProperties
            For Each DestinationProperty As System.Reflection.PropertyInfo In PictureBoxDst.GetType.GetProperties
                If DestinationProperty.CanWrite True And DestinationProperty.CanRead True Then
                    If (DestinationProperty.Name SourceProperty.Name) And (DestinationProperty.PropertyType SourceProperty.PropertyType) Then
                        srcOBJ = SourceProperty.GetValue(PictureBoxSrc, Nothing)
                        DestinationProperty.SetValue(PictureBoxDst, srcOBJ, Nothing)
                        Exit For
                    End If
                End If
            Next
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
        Dim ChoixBouton As System.Windows.Forms.Button = sender

        Select Case ChoixBouton.Name
            Case "Button1"
                Me.PictureBox1.BackgroundImage = My.Resources.Image_0

            Case "Button2"
                Dim NewPicture As New System.Windows.Forms.PictureBox
                ClonePictureBox(Me.PictureBox1, NewPicture)
                NewPicture = DirectCast(NewPicture, PictureBox)
                NewPicture.BackgroundImage = My.Resources.Image_1
                NewPicture.Left = PictureBox1.Left + PictureBox1.Width
        End Select

    End Sub

End Class

0
NHenry Messages postés 15113 Date d'inscription vendredi 14 mars 2003 Statut Modérateur Dernière intervention 22 avril 2024 159
17 janv. 2011 à 09:14
Bonjour,

Pourquoi ne pas faire une liste des noms de propriétés à copier (comme ça tu peux maitriser les propriétés désirées) ?
Sinon, j'essayerais de regarder ça ce soir.

0
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
17 janv. 2011 à 16:32
0
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
23 janv. 2011 à 15:42
Je reviens sur ce sujet avec du retard ^^
Tu me valide une méthode que je commence seulement à utiliser, extra !.

Le new me créer un type Picturebox de la classe clsDigit qui hérite des propriétés de ce type.

merci Banana32 !!!!!!!!!!!

Code utilisé:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim CompteurIndex As New List(Of Integer)
        Dim CompteurMax As Integer = 6 '999999
        Dim DecalLeft As Integer = 507
        Dim DecalTop As Integer = 135
        For i As Integer = CompteurMax To 1 Step -1
            CompteurIndex.Add(DecalLeft)
            DecalLeft -= 15
        Next

        Dim pct1 As New clsDigit(My.Resources.Compteur_0, CompteurIndex.Item(0), DecalTop) 'xxxxx0
        Dim pct2 As New clsDigit(My.Resources.Compteur_1, CompteurIndex.Item(1), DecalTop) 'xxxx10

        Me.Controls.Add(pct1)
        Me.Controls.Add(pct2)
    End Sub

    Public Class clsDigit
        Inherits PictureBox

        Sub New(ByVal ThisCounterImage As Bitmap, ByVal DecalLeft As Integer, ByVal DecalTop As Integer)
            Dim CounterImageConv As Image = CType(ThisCounterImage, Image)

            With Me
                .Visible = False
                .Width = ThisCounterImage.Width
                .Height = ThisCounterImage.Height
                .Location = New Point(DecalLeft, DecalTop)
                .BackColor = Color.Transparent
                .BackgroundImage = CounterImageConv
                .BackgroundImageLayout = ImageLayout.None
                .Left = DecalLeft
                .Visible = True
            End With
        End Sub
    End Class
0
Duke49 Messages postés 552 Date d'inscription jeudi 12 octobre 2006 Statut Non membre Dernière intervention 24 janvier 2023 4
23 janv. 2011 à 17:52
Banana32, ton chef-d'œuvre ;)
++ l'ami !

http://www.vbfrance.com/codes/VB10-COMPTEUR-GRAPHIQUE_52748.aspx
0
Rejoignez-nous