IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

Evenement move sur des objets dynamique


Sujet :

VB.NET

  1. #1
    Membre confirmé Avatar de thierry007
    Homme Profil pro
    Autodidacte
    Inscrit en
    Août 2006
    Messages
    876
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Autodidacte
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2006
    Messages : 876
    Points : 457
    Points
    457
    Par défaut Evenement move sur des objets dynamique
    BOnjour a tout le monde

    Dans mon projet j'ai 2 objets créés dynamiquement ( 1 objet pictubox et 1 objet panel )

    Lorsque je bouge l'objet pictubox, je souhaiterais que l'objet panel bouge de la même façon. Y a t'il une solution a cela

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
     
    private sub ceationObjet()
     
      pict(1) = New PbTimbres
      panelpict(1) = New PanelPage
     
     FrmPage.Panel1.Controls.Add(pict(1))
     FrmPage.Panel1.Controls.Add(Panelpict(1))
     
    end sub
     
     
     
     
    Public Class PbTimbres
          Inherits PictureBox
          Dim x, y As Integer
          Private Sub Me_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
     
                If e.Button = Windows.Forms.MouseButtons.Left Then
     
                      x = e.X
                      y = e.Y
     
                End If
     
          End Sub
     
          Private Sub Me_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
     
                If e.Button = Windows.Forms.MouseButtons.Left Then
     
                      sender.Left += (e.X - x)
                      sender.Top += (e.Y - y)
     
                      FrmPage.LblPage1.Text = Me.Left & "/" & Me.Top
     
                End If
     
          End Sub
    End Class
     
    Public Class PanelPage
     
     
    End Class
    La Connaissance est comme la joie elle s'accroît en la partageant!

  2. #2
    Membre éprouvé

    Homme Profil pro
    Inscrit en
    Mars 2012
    Messages
    691
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Israël

    Informations forums :
    Inscription : Mars 2012
    Messages : 691
    Points : 929
    Points
    929
    Par défaut
    Salut

    je n'ai pas bien compris essaie le code suivant
    en glissant le carré bleu avec la souris appuyé

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    Public Class Form1
        Private mypic As New PictureBox
        Private lastmouselocation As New Point
        Private mypanel As New Panel
        Private Sub Init()
            With mypic
                .Height = 100
                .Width = 100
                .BorderStyle = BorderStyle.Fixed3D
                .BackColor = Color.DarkBlue
                .Location = New Point(0, 0)
                AddHandler mypic.MouseMove, AddressOf mypicmousemove
                AddHandler mypic.MouseDown, AddressOf mypicMouseDown
     
            End With
            With mypanel
                .Height = 100
                .Width = 100
                .BorderStyle = BorderStyle.Fixed3D
                .BackColor = Color.DarkRed
                .Location = New Point(Me.Width \ 4, Me.Height \ 4)
     
            End With
            Me.Controls.Add(mypic)
            Me.Controls.Add(mypanel)
        End Sub
        Private Sub mypicMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            lastmouselocation = e.Location
     
        End Sub
        Private Sub mypicMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            Dim ptpic As New Point
            Dim ptpan As New Point
            ptpic = e.Location
            ptpan = e.Location
            If e.Button = Windows.Forms.MouseButtons.Left Then
                ptpic.X = (ptpic.X + DirectCast(sender, PictureBox).Left) - lastmouselocation.X
                ptpic.Y = (ptpic.Y + DirectCast(sender, PictureBox).Top) - lastmouselocation.Y
                ptpan.X = (ptpan.X + mypanel.Left) - (lastmouselocation.X)
                ptpan.Y = (ptpan.Y + mypanel.Top) - (lastmouselocation.Y)
                DirectCast(sender, PictureBox).Location = ptpic
                mypanel.Location = ptpan
     
            End If
        End Sub
     
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Init()
        End Sub
    End Class

  3. #3
    Membre confirmé Avatar de thierry007
    Homme Profil pro
    Autodidacte
    Inscrit en
    Août 2006
    Messages
    876
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Autodidacte
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2006
    Messages : 876
    Points : 457
    Points
    457
    Par défaut
    Le principe est bien là, différence, c'est que je crées les objet pour les placer sur une form autre que celle qui les crées

    Explication


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    Public class Form1
    définition des objets avec leurs propriétés
    Création sur Form2
    end class
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    Public class Form2
     
    Gestion du déplacement des objets
    end class
    Le nombre d'ojets peut être diférent
    mais le nombre de panel ( dynamique ) et le même que les picturebox
    La Connaissance est comme la joie elle s'accroît en la partageant!

  4. #4
    Membre éprouvé

    Homme Profil pro
    Inscrit en
    Mars 2012
    Messages
    691
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Israël

    Informations forums :
    Inscription : Mars 2012
    Messages : 691
    Points : 929
    Points
    929
    Par défaut
    Pas si simple enfin pour moi
    question tu bouges un picturebox à la fois et tu veux que la panel correspondante (qui à le meme index que la picturebox ??) bouge de la meme
    façon
    je teste dans une form
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
     Public Class Form1
        Private listpics(20) As PictureBox
        Private lastmouselocation(20) As Point
        Private listpanels(20) As Panel
        Private Sub Init()
            Dim leftpic As Integer
            Dim toppic As Integer
            Dim leftpan As Integer
            Dim toppan As Integer
            leftpic = 0
            toppic = 0
            leftpan = 0
            toppan = Me.Height \ 2
            For iter = 0 To listpics.Length - 1
                Dim mypic As New PictureBox
                With mypic
                    .Height = 30
                    .Width = 30
                    .BorderStyle = BorderStyle.Fixed3D
                    .BackColor = Color.DarkBlue
                    .Location = New Point(leftpic, toppic)
                    .Tag = iter.ToString
                    AddHandler mypic.MouseMove, AddressOf mypicMouseMove
                    AddHandler mypic.MouseDown, AddressOf mypicMouseDown
     
                End With
                listpics(iter) = mypic
                leftpic += mypic.Width + 3
                If leftpic > Me.Width - mypic.Width Then
                    leftpic = 0
                    toppic += mypic.Height + 3
                End If
                Me.Controls.Add(mypic)
                Dim mypanel As New Panel
     
                With mypanel
                    .Height = 30
                    .Width = 30
                    .BorderStyle = BorderStyle.Fixed3D
                    .BackColor = Color.DarkRed
                    .Location = New Point(leftpan, toppan)
                    .Tag = iter.ToString
                End With
                listpanels(iter) = mypanel
                leftpan += mypanel.Width + 3
                If leftpan > Me.Width - mypanel.Width Then
                    leftpan = 0
                    toppan += mypanel.Height + 3
                End If
                Me.Controls.Add(mypanel)
            Next
     
     
     
     
        End Sub
        Private Sub mypicMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            lastmouselocation(Convert.ToInt32(DirectCast(sender, PictureBox).Tag)) = e.Location
        End Sub
        Private Sub mypicMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            Dim ptpic As New Point
            Dim ptpan As New Point
            ptpic = e.Location
            ptpan = e.Location
            If e.Button = Windows.Forms.MouseButtons.Left Then
                ' ptpic.X = ptpic.X + DirectCast(sender, PictureBox).Left - lastmouselocation.X
     
                ptpic.X = ptpic.X + DirectCast(sender, PictureBox).Left - lastmouselocation(Convert.ToInt32(DirectCast(sender, PictureBox).Tag)).X
                ptpic.Y = ptpic.Y + DirectCast(sender, PictureBox).Top - lastmouselocation(Convert.ToInt32(DirectCast(sender, PictureBox).Tag)).Y
                ptpan.X = ptpan.X + listpanels(Convert.ToInt32(DirectCast(sender, PictureBox).Tag)).Left - (lastmouselocation(Convert.ToInt32(DirectCast(sender, PictureBox).Tag)).X)
                ptpan.Y = ptpan.Y + listpanels(Convert.ToInt32(DirectCast(sender, PictureBox).Tag)).Top - (lastmouselocation(Convert.ToInt32(DirectCast(sender, PictureBox).Tag)).Y)
     
                DirectCast(sender, PictureBox).Location = ptpic
                listpanels(Convert.ToInt32(DirectCast(sender, PictureBox).Tag)).Location = ptpan
     
            End If
        End Sub
     
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Init()
     
        End Sub
     
     
    End Class
    enfin ça ne correspond pas à ta demande puisque tu as deux forms

  5. #5
    Membre confirmé Avatar de thierry007
    Homme Profil pro
    Autodidacte
    Inscrit en
    Août 2006
    Messages
    876
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Autodidacte
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2006
    Messages : 876
    Points : 457
    Points
    457
    Par défaut
    C'est là qu'est l'os

    C'est sur cette form que je déplace les objets
    Sinon ton idée au début était exelente

    Peut être que quelqu'un de plus expérimenté aura une idée
    La Connaissance est comme la joie elle s'accroît en la partageant!

  6. #6
    Membre éclairé Avatar de -N4w4k-
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2011
    Messages
    545
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Industrie

    Informations forums :
    Inscription : Novembre 2011
    Messages : 545
    Points : 801
    Points
    801
    Par défaut
    Salut Thierry,

    je peux te proposer une solution (temporaire je pense, parce que conceptuellement.. ) qui est de créer une petite classe qui fait la liaison entre tes deux objets.. Vois plutôt:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    Public Class LiaisonPictPanel
     
        Private WithEvents PictureBox As PictureBox
        Private WithEvents Panel As Panel
     
        Private Origine As Point
     
        Sub New(ByVal pict As PictureBox, ByVal panel As Panel)
            Me.PictureBox = pict
            Me.Panel = panel
            Me.Origine = PictureBox.Location
        End Sub
     
        Private Sub PictureBox_Move(ByVal sender As Object, ByVal e As EventArgs) Handles PictureBox.Move
            Panel.Location += New Point(PictureBox.Location.X - Origine.X, PictureBox.Location.Y - Origine.Y)
            Origine = PictureBox.Location
        End Sub
     
     
    End Class
    Tu n'as plus qu'à faire:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    private sub ceationObjet()
     
      pict(1) = New PbTimbres
      panelpict(1) = New PanelPage
     
     FrmPage.Panel1.Controls.Add(pict(1))
     FrmPage.Panel1.Controls.Add(Panelpict(1))
     Dim l As New LiaisonPictPanel(pict(1), Panelpict(1)) 'Pas très beau mais ca marche :P
    end sub
    J’ai des questions à toutes vos réponses!

  7. #7
    Membre éprouvé

    Homme Profil pro
    Inscrit en
    Mars 2012
    Messages
    691
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Israël

    Informations forums :
    Inscription : Mars 2012
    Messages : 691
    Points : 929
    Points
    929
    Par défaut
    Salut

    je n'ai surement pas bien compris
    tu crées les objets dans form1 mais tu les places dans form2
    et tu bouges les picturebox et sa panel dans form2 ?

    encore un essaie

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    Public Class Form1
           Public listpics(20) As PictureBox
           Public lastmouselocation(20) As Point
            Public listpanels(20) As Panel
            Private Sub Init()
               Dim leftpic As Integer
               Dim toppic As Integer
               Dim leftpan As Integer
               Dim toppan As Integer
               leftpic = 0
               toppic = 0
               leftpan = 0
              toppan = Me.Height \ 2
              For iter = 0 To listpics.Length - 1
                Dim mypic As New PictureBox
                With mypic
                    .Height = 30
                    .Width = 30
                    .BorderStyle = BorderStyle.Fixed3D
                    .BackColor = Color.DarkBlue
                    .Location = New Point(leftpic, toppic)
                    .Tag = iter.ToString
                    AddHandler mypic.MouseMove, AddressOf Form2.mypicMouseMove
                    AddHandler mypic.MouseDown, AddressOf Form2.mypicMouseDown
     
                End With
                listpics(iter) = mypic
                leftpic += mypic.Width + 3
                If leftpic > Me.Width - mypic.Width Then
                    leftpic = 0
                    toppic += mypic.Height + 3
                End If
                Form2.Controls.Add(mypic)
                Dim mypanel As New Panel
     
                With mypanel
                    .Height = 30
                    .Width = 30
                    .BorderStyle = BorderStyle.Fixed3D
                    .BackColor = Color.DarkRed
                    .Location = New Point(leftpan, toppan)
                    .Tag = iter.ToString
                End With
                listpanels(iter) = mypanel
                leftpan += mypanel.Width + 3
                If leftpan > Me.Width - mypanel.Width Then
                    leftpan = 0
                    toppan += mypanel.Height + 3
                End If
                Form2.Controls.Add(mypanel)
            Next
     
        End Sub
     
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Init()
            Form2.Show()
            Me.Hide()
        End Sub
    End Class
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    Public Class Form2
     Public Sub mypicMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            Form1.lastmouselocation(Convert.ToInt32(DirectCast(sender, PictureBox).Tag)) = e.Location
        End Sub
        Public Sub mypicMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            Dim ptpic As New Point
            Dim ptpan As New Point
            ptpic = e.Location
            ptpan = e.Location
            If e.Button = Windows.Forms.MouseButtons.Left Then
                ' ptpic.X = ptpic.X + DirectCast(sender, PictureBox).Left - lastmouselocation.X
     
                ptpic.X = ptpic.X + DirectCast(sender, PictureBox).Left - Form1.lastmouselocation(Convert.ToInt32(DirectCast(sender, PictureBox).Tag)).X
                ptpic.Y = ptpic.Y + DirectCast(sender, PictureBox).Top - Form1.lastmouselocation(Convert.ToInt32(DirectCast(sender, PictureBox).Tag)).Y
                ptpan.X = ptpan.X + Form1.listpanels(Convert.ToInt32(DirectCast(sender, PictureBox).Tag)).Left - (Form1.lastmouselocation(Convert.ToInt32(DirectCast(sender, PictureBox).Tag)).X)
                ptpan.Y = ptpan.Y + Form1.listpanels(Convert.ToInt32(DirectCast(sender, PictureBox).Tag)).Top - (Form1.lastmouselocation(Convert.ToInt32(DirectCast(sender, PictureBox).Tag)).Y)
     
                DirectCast(sender, PictureBox).Location = ptpic
                Form1.listpanels(Convert.ToInt32(DirectCast(sender, PictureBox).Tag)).Location = ptpan
     
            End If
        End Sub
     End Class

  8. #8
    Membre confirmé Avatar de thierry007
    Homme Profil pro
    Autodidacte
    Inscrit en
    Août 2006
    Messages
    876
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Autodidacte
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2006
    Messages : 876
    Points : 457
    Points
    457
    Par défaut
    Citation Envoyé par shayw Voir le message
    Salut

    je n'ai surement pas bien compris
    tu crées les objets dans form1 mais tu les places dans form2
    et tu bouges les picturebox et sa panel dans form2 ?

    [/code]
    C'est exactement cela
    La Connaissance est comme la joie elle s'accroît en la partageant!

  9. #9
    Membre confirmé Avatar de thierry007
    Homme Profil pro
    Autodidacte
    Inscrit en
    Août 2006
    Messages
    876
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Autodidacte
    Secteur : Industrie

    Informations forums :
    Inscription : Août 2006
    Messages : 876
    Points : 457
    Points
    457
    Par défaut
    Citation Envoyé par -N4w4k- Voir le message
    Salut Thierry,

    je peux te proposer une solution (temporaire je pense, parce que conceptuellement.. ) qui est de créer une petite classe qui fait la liaison entre tes deux objets.. Vois plutôt:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    Public Class LiaisonPictPanel
     
        Private WithEvents PictureBox As PictureBox
        Private WithEvents Panel As Panel
     
        Private Origine As Point
     
        Sub New(ByVal pict As PictureBox, ByVal panel As Panel)
            Me.PictureBox = pict
            Me.Panel = panel
            Me.Origine = PictureBox.Location
        End Sub
     
        Private Sub PictureBox_Move(ByVal sender As Object, ByVal e As EventArgs) Handles PictureBox.Move
            Panel.Location += New Point(PictureBox.Location.X - Origine.X, PictureBox.Location.Y - Origine.Y)
            Origine = PictureBox.Location
        End Sub
     
     
    End Class
    Tu n'as plus qu'à faire:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    private sub ceationObjet()
     
      pict(1) = New PbTimbres
      panelpict(1) = New PanelPage
     
     FrmPage.Panel1.Controls.Add(pict(1))
     FrmPage.Panel1.Controls.Add(Panelpict(1))
     Dim l As New LiaisonPictPanel(pict(1), Panelpict(1)) 'Pas très beau mais ca marche :P
    end sub
    Comme tu le dis , sa marche
    La Connaissance est comme la joie elle s'accroît en la partageant!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Débutant] Ajouter des évènements sur des objets dynamiques
    Par fdthierry dans le forum VB.NET
    Réponses: 6
    Dernier message: 30/09/2011, 17h30
  2. Réponses: 3
    Dernier message: 09/01/2007, 15h27
  3. Réponses: 2
    Dernier message: 02/08/2006, 23h54
  4. Réponses: 9
    Dernier message: 31/05/2006, 11h56
  5. Réponses: 6
    Dernier message: 18/04/2005, 21h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo