Picturebox index en vb .net

Résolu
pcpunch Messages postés 1243 Date d'inscription mardi 7 mai 2002 Statut Membre Dernière intervention 18 février 2019 - 12 sept. 2015 à 12:50
pcpunch Messages postés 1243 Date d'inscription mardi 7 mai 2002 Statut Membre Dernière intervention 18 février 2019 - 13 sept. 2015 à 14:18
Bonjour,

Je cherche comment générer des picturebox en vb2010, comme on le fesait en vb6 avec un index.
Je dois generer des picturebox et pouvoir recupérer l index au clic de cette PB
j'ai bien trouver ce code :

Dim Pictureboxes As List(Of PictureBox) = New List(Of PictureBox)
For I As Integer = 0 To 9
Dim P As PictureBox = New PictureBox
P.Size = Boite.Size
P.SizeMode = PictureBoxSizeMode.StretchImage
P.Location = New Point(I * 100 + 20, Boite.Top)
P.BorderStyle = BorderStyle.FixedSingle

Pictureboxes.Add(P)
P.Image = Boite.Image
Me.Controls.Add(P)
Next I

Mais je ne sais pas comment récupérer le clic sur une des picturebox crée dynamiquement.

De plus je cherche a ecrire un chiffre dans la picture box générer ?

4 réponses

cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 137
12 sept. 2015 à 14:47
Bonjour,

Voici un exemple à adapter:

mettre dans le Form un panel de 284 x260 avec ce code:

Public Class Form1
    Dim image As PictureBox
    Dim x, y, diff, dify As Int32
    Dim vignette As Integer

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim i As Int32
        Panel1.Controls.Clear()
        y = 0
        x = 0
        For i = 1 To 9
                    
         image = New PictureBox

            image.Width = 100
            image.Height = 100
            image.BorderStyle = BorderStyle.Fixed3D
            image.Visible = True
            image.Name = "mapicture " & i
            vignette = 90
            image.SetBounds(x, y, vignette, vignette)
            x += vignette
            'vérifier s'il ya suffisamment d'espace dans l'axe x pour ajouter une image
            If x >= Panel1.Width Or (x + vignette) > Panel1.Width Then
                diff = Panel1.Width - x
                'S'il n'y a pas assez d'espace, commencer une nouvelle ligne ci-dessous
                y += vignette
                x = 0
            End If
            If ((y + vignette) > Panel1.Height) Then
                dify = Panel1.Height - y
            End If

            AddHandler image.MouseDown, AddressOf PictureBox_MouseDown
            AddHandler image.MouseMove, AddressOf PictureBox_MouseMove
            AddHandler image.DoubleClick, AddressOf PictureBox_DoubleClick
            'ajouter une image
            Panel1.Controls.Add(image)
        Next i
    End Sub
    Private Sub PictureBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            x = e.X
            y = e.Y
        End If
    End Sub

    Private Sub PictureBox_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            sender.Left += (e.X - x)
            sender.Top += (e.Y - y)
        End If
    End Sub
    Private Sub PictureBox_DoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        ' On récupère le contrôle cliqué ...
        Dim pictTmp As PictureBox = DirectCast(sender, PictureBox)
        MsgBox(pictTmp.Name)
    End Sub
End Class

2
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 137
12 sept. 2015 à 16:00
La pictureBox n'a pas destination a recevoir du texte. Il faut utiliser une TextBox.
Tu peux faire la même chose avec des TextBox dynamiques, comme ceci:

Public Class Form1
    Dim texte As TextBox
    Dim x, y, diff, dify As Int32
    Dim vignette As Integer
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim i As Int32
        Panel1.Controls.Clear()
        y = 0
        x = 0
        For i = 1 To 9
            'création TextBox
            texte = New TextBox()
            texte.Multiline = True
            texte.Width = 100
            texte.Height = 100
            texte.BorderStyle = BorderStyle.Fixed3D
            texte.Visible = True
            texte.Name = "montexte " & i
            vignette = 90
            texte.SetBounds(x, y, vignette, vignette)
            x += vignette
            'vérifier s'il ya suffisamment d'espace dans l'axe x pour ajouter une image
            If x >= Panel1.Width Or (x + vignette) > Panel1.Width Then
                diff = Panel1.Width - x
                'S'il n'y a pas assez d'espace, commencer une nouvelle ligne ci-dessous
                y += vignette
                x = 0
            End If
            If ((y + vignette) > Panel1.Height) Then
                dify = Panel1.Height - y
            End If
            AddHandler texte.DoubleClick, AddressOf TextBox_DoubleClick
            'ajouter une textbox
            Panel1.Controls.Add(texte)
        Next i
    End Sub
    Private Sub TextBox_DoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim objFont As System.Drawing.Font
        objFont = New System.Drawing.Font("Arial", 48, FontStyle.Bold Or FontStyle.Italic)
        ' On récupère le contrôle cliqué ...
        Dim textTmp As TextBox = DirectCast(sender, TextBox)
        textTmp.Name = Replace(textTmp.Name, "montexte", "")
        textTmp.Text = textTmp.Name
        textTmp.TextAlign = HorizontalAlignment.Center
        textTmp.Font = objFont
    End Sub

1
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 137
12 sept. 2015 à 16:01
Toujours dans un panel de 284 x 260
0
pcpunch Messages postés 1243 Date d'inscription mardi 7 mai 2002 Statut Membre Dernière intervention 18 février 2019 5
12 sept. 2015 à 15:03
merci beaucoup c'est exactement ça !
me reste juste a écrire le chiffre dans la picturebox .
0
pcpunch Messages postés 1243 Date d'inscription mardi 7 mai 2002 Statut Membre Dernière intervention 18 février 2019 5
13 sept. 2015 à 14:18
Merci beaucoup, nikel !!!
0
Rejoignez-nous