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 :

Indexation des pictureBox avec Tag. Peut-on faire mieux?


Sujet :

VB.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éprouvé Avatar de excalybur
    Homme Profil pro
    Chargé d'affaire
    Inscrit en
    Novembre 2014
    Messages
    317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Chargé d'affaire
    Secteur : Conseil

    Informations forums :
    Inscription : Novembre 2014
    Messages : 317
    Par défaut Indexation des pictureBox avec Tag. Peut-on faire mieux?
    Bonsoir,

    En relisant les FAQ récentes j'ai compris qu'il fallait déjouer cette impossibilité d'indexer en utilisant Tag. J'ai affecté Tag de chaque image avec les n°1 à 64 (64 cases d'un échiquier) => OK ça marche voir le code suivant

    A noter que les mouvement de la souris utilise mousedown et mousemouve ainsi que dragdrop sur le picturebox final.
    GiveFeedback est utilisé pour adapter l'image bmp d'une pièce d'échec appelé RN au curseur de la souris pour voir son mouvement.

    Le code :

    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
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    Public Class Form1
     
        Dim MouseIsDown As Boolean = False
        Dim CursorImage As Image
        Dim isDragging As Boolean = False
        Dim CurrentX As Integer, CurrentY As Integer
        Dim CaseEch(64) As ButtonBase
        Dim aaa As String
        Dim Pieces As String
        Dim LocationX(64) As Integer
        Dim LocationY(64) As Integer
     
     
        Private Property mypic As Point
     
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox1.AllowDrop = True
            PictureBox2.AllowDrop = True
            PictureBox3.AllowDrop = True
            PictureBox4.AllowDrop = True
            PictureBox5.AllowDrop = True
            PictureBox6.AllowDrop = True
            PictureBox7.AllowDrop = True
            PictureBox8.AllowDrop = True
            PictureBox9.AllowDrop = True
     
     
     Public Sub RB_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RB.MouseDown
            MouseIsDown = True
        End Sub
     
     
        Public Sub RB_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RB.MouseMove
            If MouseIsDown Then
                If RB.Image Is Nothing Then Exit Sub
                RB.DoDragDrop(RB.Image, DragDropEffects.Move)
            Else
                MouseIsDown = False
            End If
        End Sub
     
     
        Public Sub RB_GiveFeedback(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) Handles RB.GiveFeedback
            e.UseDefaultCursors = False
            Dim myPic As New Bitmap(CType(sender, PictureBox).Image)
            CursorImage = myPic.GetThumbnailImage(32, 32, Nothing, IntPtr.Zero)
            Cursor.Current = New Cursor(CType(CursorImage, System.Drawing.Bitmap).GetHicon())
            Pieces = "RB"
        End Sub
     
     
        Public Sub RN_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RN.MouseDown
            MouseIsDown = True
        End Sub
     
     
        Public Sub RN_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RN.MouseMove
            If MouseIsDown Then
                If RN.Image Is Nothing Then Exit Sub
                RN.DoDragDrop(RN.Image, DragDropEffects.Move)
            Else
                MouseIsDown = False
            End If
        End Sub
     
     
        Public Sub RN_GiveFeedback(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.GiveFeedbackEventArgs) Handles RN.GiveFeedback
            e.UseDefaultCursors = False
            Dim myPic As New Bitmap(CType(sender, PictureBox).Image)
            CursorImage = myPic.GetThumbnailImage(32, 32, Nothing, IntPtr.Zero)
            Cursor.Current = New Cursor(CType(CursorImage, System.Drawing.Bitmap).GetHicon())
            Pieces = "RN"
        End Sub
     
     
     Public Sub PictureBox_DragDrop(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragDrop, PictureBox2.DragDrop, PictureBox3.DragDrop, PictureBox4.DragDrop, PictureBox5.DragDrop, PictureBox6.DragDrop, PictureBox7.DragDrop, PictureBox8.DragDrop
     
            Dim clicked As PictureBox = CType(sender, PictureBox)
            Tag = sender.tag
     
            If Tag = 1 Then PictureBox1.Image = e.Data.GetData(DataFormats.Bitmap) : LocationX(Tag) = PictureBox1.Location.X : LocationY(Tag) = PictureBox1.Location.Y
            If Tag = 2 Then PictureBox2.Image = e.Data.GetData(DataFormats.Bitmap) : LocationX(Tag) = PictureBox2.Location.X : LocationY(Tag) = PictureBox2.Location.Y
            If Tag = 3 Then PictureBox3.Image = e.Data.GetData(DataFormats.Bitmap) : LocationX(Tag) = PictureBox3.Location.X : LocationY(Tag) = PictureBox3.Location.Y
            If Tag = 4 Then PictureBox4.Image = e.Data.GetData(DataFormats.Bitmap) : LocationX(Tag) = PictureBox4.Location.X : LocationY(Tag) = PictureBox4.Location.Y
            If Tag = 5 Then PictureBox5.Image = e.Data.GetData(DataFormats.Bitmap) : LocationX(Tag) = PictureBox5.Location.X : LocationY(Tag) = PictureBox5.Location.Y
            If Tag = 6 Then PictureBox6.Image = e.Data.GetData(DataFormats.Bitmap) : LocationX(Tag) = PictureBox6.Location.X : LocationY(Tag) = PictureBox6.Location.Y
            If Tag = 7 Then PictureBox7.Image = e.Data.GetData(DataFormats.Bitmap) : LocationX(Tag) = PictureBox7.Location.X : LocationY(Tag) = PictureBox7.Location.Y
            If Tag = 8 Then PictureBox8.Image = e.Data.GetData(DataFormats.Bitmap) : LocationX(Tag) = PictureBox8.Location.X : LocationY(Tag) = PictureBox8.Location.Y
     
     
     
            TextBox1.Text = "Tag: " + Tag.ToString + "    Piece: " + Pieces + "              Location X,Y :  " + LocationX(Tag).ToString + " , " + LocationY(Tag).ToString
        End Sub
     
     
    End Class
    Ce code est donné pour les 8 premières cases dans le sub PictureBox_DragDrop, il n'est pas complet (volontairement) et vous comprenez tout de suite pourquoi : L'écriture de 64 fois cette ligne à modifier chaque fois est fastidieuse : " If Tag = 8 Then PictureBox8.Image = e.Data.GetData(DataFormats.Bitmap) : LocationX(Tag) = PictureBox8.Location.X : LocationY(Tag) = PictureBox8.Location.Y"

    L'utilisation de Tag a bien contribué à réduire l'écriture, l'idéal serait de pouvoir inclure la ligne précédente dans une boucle for/next de 1 à 64 par exemple mais comment l'indexer totalement les Picturebox et leurs propriétés ? D'autant qu'il va se rajouter beaucoup données supplémentaire sur cette ligne!

    Pour info une photo de la feuille de capture et les propriétés mise à chaque case de 1 à 64 (Tag)

    Nom : Ecranchess.JPG
Affichages : 681
Taille : 113,9 Ko

    Merci pour vos idées et suggestions.

  2. #2
    Membre éprouvé Avatar de excalybur
    Homme Profil pro
    Chargé d'affaire
    Inscrit en
    Novembre 2014
    Messages
    317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Chargé d'affaire
    Secteur : Conseil

    Informations forums :
    Inscription : Novembre 2014
    Messages : 317
    Par défaut
    Re

    Je réponds à ma question !

    L'indexation des PictureBox est bien possible !

    L'astuce :

    1/ Déclarer un objet indexé que je nomme PicZ :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    Public Class Form1
     
        Dim MouseIsDown As Boolean = False
        Dim CursorImage As Image
        Dim isDragging As Boolean = False
        Dim CurrentX As Integer, CurrentY As Integer
        Dim CaseEch(64) As ButtonBase
        Dim aaa As String
        Dim Pieces As String
        Dim LocationX(64) As Integer
        Dim LocationY(64) As Integer
        Dim PicZ(64) As Object
     
        Private Property mypic As Point
    2/ Affecter dans le Form1_Load chaque PictureBox à son objet indexé PicZ() et affecter à chaque PicZ() l'état AllowDrop= True :

    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
     
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
     
            PicZ(1) = PictureBox1
            PicZ(2) = PictureBox2
            PicZ(3) = PictureBox3
            PicZ(4) = PictureBox4
            PicZ(5) = PictureBox5
            PicZ(6) = PictureBox6
            PicZ(7) = PictureBox7
            PicZ(8) = PictureBox8
            PicZ(9) = PictureBox9
    'etc jusqu'à 64 .... 
     
            For n = 1 To 64
                PicZ(n).allowdrop = True
     
            Next
        End Sub
    3/ Créer la fameuse boucle for next dans le sub picturebox_DragDrop :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     Public Sub PictureBox_DragDrop(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragDrop, PictureBox2.DragDrop, PictureBox3.DragDrop, PictureBox4.DragDrop, PictureBox5.DragDrop, PictureBox6.DragDrop, PictureBox7.DragDrop, PictureBox8.DragDrop
     
            Dim clicked As PictureBox = CType(sender, PictureBox)
     
     
            Tag = sender.tag
     
            For m = 1 To 64
                If Tag = m Then PicZ(Tag).Image = e.Data.GetData(DataFormats.Bitmap) : LocationX(Tag) = PicZ(Tag).Location.X : LocationY(Tag) = PicZ(Tag).Location.Y
            Next m
            TextBox1.Text = "Tag: " + Tag.ToString + "    Piece: " + Pieces + "              Location X,Y :  " + LocationX(Tag).ToString + " , " + LocationY(Tag).ToString
        End Sub
    Voila ! c'était si simple.

    Allez je marque Résolu.

  3. #3
    Membre averti
    Homme Profil pro
    passionné
    Inscrit en
    Janvier 2016
    Messages
    22
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 55
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : passionné
    Secteur : Administration - Collectivité locale

    Informations forums :
    Inscription : Janvier 2016
    Messages : 22
    Par défaut
    je me suis posé cette même question il y apeu :

    La réponse reçue était peut être encore plus pratique que la tienne :

    http://www.developpez.net/forums/d15...jets-indexees/


  4. #4
    Expert éminent Avatar de Pol63
    Homme Profil pro
    .NET / SQL SERVER
    Inscrit en
    Avril 2007
    Messages
    14 197
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : .NET / SQL SERVER

    Informations forums :
    Inscription : Avril 2007
    Messages : 14 197
    Par défaut
    c'est ni fait ni à faire

    quand on a beaucoup de chose à poser, on ne perd pas de temps à les poser à la main, et en ajoutant une ligne de code par objet pour le mettre dans une collection

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    private _Pcts as new list(of picturebox)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    for i as integer = 1 to 64
       dim p as new pictubox
       _pcts.Add(p)
       ' placement (définir p.top et p.left)
       me.controls.add(p)
       ' addhandler ... (= ajouter une gestion des évènements sur les controles créés par code)
    next
    10 lignes de code et tu as tes 64 cases de posées, et elles sont indexées dans une collection pour les retrouver partout


    il convient donc d'apprendre un peu le langage et ce qu'on peut faire avec avant de coder
    Cours complets, tutos et autres FAQ ici : C# - VB.NET

  5. #5
    Membre éprouvé Avatar de excalybur
    Homme Profil pro
    Chargé d'affaire
    Inscrit en
    Novembre 2014
    Messages
    317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Chargé d'affaire
    Secteur : Conseil

    Informations forums :
    Inscription : Novembre 2014
    Messages : 317
    Par défaut
    Bonjour

    Merci pour vos réponse. William44af je me suis un peu servi de ton projet que j'ai lu dans les FAQ pour comprendre l'astuce que j'ai exposé.

    Ceci étant dit ce langage VB2012 est génial et son principale intérêt est de faire court en programmation d'où le la question de mon titre. Soyez indulgent je ne peut pas tout apprendre en 2 mois à raison de 5 ou 6 heurs par semaine !

    Je teste les méthodes et à bientôt pour les résultats.

  6. #6
    Membre éprouvé Avatar de excalybur
    Homme Profil pro
    Chargé d'affaire
    Inscrit en
    Novembre 2014
    Messages
    317
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Var (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Chargé d'affaire
    Secteur : Conseil

    Informations forums :
    Inscription : Novembre 2014
    Messages : 317
    Par défaut
    Bonsoir,

    J'ai passé pas mal de temps sur la proposition de Pol63 (" Private _Pcts As New List(Of PictureBox).....) sans trouver la bonne façon de l'écrire.

    Quelqu'un aurait-il une solution à me proposer car là je sèche et la documentation de Microsoft n'est pas loquace en exemples ?

    En vous remerciant.

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

Discussions similaires

  1. comparer des strings avec retour ligne, comment faire?
    Par paf26 dans le forum Macros et VBA Excel
    Réponses: 5
    Dernier message: 16/12/2015, 13h54
  2. Mon ami printmap() peut-il faire mieux ?
    Par tbkgeo dans le forum IGN API Géoportail
    Réponses: 1
    Dernier message: 20/03/2013, 18h01
  3. Réponses: 8
    Dernier message: 04/06/2012, 17h07
  4. [Tableaux] Indexation des images avec scandir
    Par Gunner4902 dans le forum Langage
    Réponses: 17
    Dernier message: 04/03/2008, 14h19
  5. Peut-on faire des colonnes avec "div"
    Par lodan dans le forum Balisage (X)HTML et validation W3C
    Réponses: 8
    Dernier message: 30/07/2006, 06h56

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