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

Dotnet Discussion :

Comment afficher des images et textes associés dans une ComboBox dans vb.net


Sujet :

Dotnet

  1. #1
    Candidat au Club
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Mars 2014
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : Bâtiment

    Informations forums :
    Inscription : Mars 2014
    Messages : 5
    Points : 4
    Points
    4
    Par défaut Comment afficher des images et textes associés dans une ComboBox dans vb.net
    bonjour
    mon probleme est comment faire apparaitre dans une combobox une liste d'images associées à des textes

    voici j'ai crée un model mais le resultat est loin de mes attentes car les icons ou images sont superposées et non listées l'une après les autres


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    Private Sub ComboBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
     
            If (e.Index <> -1) Then
     
                e.Graphics.DrawImage(ImageList1.Images(e.Index), e.Bounds.Left, e.Bounds.Top)
     
                e.Graphics.DrawString(Me.ComboBox1.Items(e.Index).ToString(), e.Font, System.Drawing.Brushes.Black, e.Bounds.Left + ImageList1.Images(e.Index).Width, e.Bounds.Top)
     
            End If
     
        End Sub
    merci
    Images attachées Images attachées  

  2. #2
    Membre expérimenté
    Homme Profil pro
    Développeur .Net / Delphi
    Inscrit en
    Juillet 2002
    Messages
    738
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Développeur .Net / Delphi
    Secteur : Finance

    Informations forums :
    Inscription : Juillet 2002
    Messages : 738
    Points : 1 745
    Points
    1 745
    Par défaut
    Bonjour,
    Tu peux définir la hauteur pour chaque item de la combo : Il faut définir la propriété DrawMode = DrawMode.OwnerDrawVariable puis implémenter l'event MeasureItem :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
    {
        e.ItemHeight = la hauteur voulue;
    }

  3. #3
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut
    bonjour
    mon problème est comment faire apparaitre dans une combobox une liste d'images associées à des textes
    Une image associe à un texte est nécessairement un type de Class Custom ...
    Comme un ComboBox ou un Listbox accepte dans leur prop Items(qui est un array) -pas que uniquement un string- mais un type Object,il s'ensuit que tu peux definir un , eh oui ,un Class StyledItem ou "item stylé"....

    Le seul problème à résoudre ensuite est créer un custom control Combo qui accepte ce type d'item un peu spécial et le dessine correctement...
    Le code qui suit fournit un exemple d'item style et son combo custom:

    code du class StyledItem :
    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
     
     
    <Serializable()>
    Public Class StyledItem
        ' The Color property.
        Private m_Color As Color = Drawing.Color.BlueViolet
        Public Property Color() As Color
            Get
                Return m_Color
            End Get
            Set(ByVal value As Color)
                m_Color = value
            End Set
        End Property
        ' The Text property.
        Private m_Text As String = "item"
        Public Property Text() As String
            Get
                Return m_Text
            End Get
            Set(ByVal value As String)
                m_Text = value
            End Set
        End Property
        ' The Font property.
        Private m_Font As Font = New Font("arial", 8.0F)
        Public Property Font() As Font
            Get
                Return m_Font
            End Get
            Set(ByVal value As Font)
                m_Font = value
            End Set
        End Property
        ' The Image property.
        Private m_Image As Image = New Bitmap(64, 64)
        Public Property Image() As Image
            Get
                Return m_Image
            End Get
            Set(ByVal value As Image)
                m_Image = value
            End Set
        End Property
        ' Display the object’s text.
        Public Overrides Function ToString() As String
            Return Me.Text
        End Function
    End Class
    code du Control Custom StyledCombox qui accepte le StyledItem:

    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
     
    Public Class StyledCombox
        Inherits ComboBox
        Public Shadows Property Items() As StyledItem()
            Get
                Dim itemArray(0 To MyBase.Items.Count - 1) As StyledItem
                For i As Integer = 0 To MyBase.Items.Count - 1
                    itemArray(i) = DirectCast(MyBase.Items(i), StyledItem)
                Next i
                Return itemArray
            End Get
            Set(ByVal value() As StyledItem)
                MyBase.Items.Clear()
                For Each styledItem As StyledItem In value
                    MyBase.Items.Add(styledItem)
                Next styledItem
            End Set
        End Property
        '' Specify OwnerDraw mode.
        Public Sub New()
            MyBase.New()
            InitializeComponent()
            ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
            MyBase.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
        End Sub
        ' Set the size for an item.
        Private Const ITEM_MARGIN As Integer = 2
        Private Sub StyledListBox_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Me.MeasureItem
     
            If e.Index < 0 Or e.Index >= MyBase.Items.Count Then Exit Sub
            Dim gr As Graphics = e.Graphics
            ' Convert the item into a StyledListBoxItem.
            Dim the_item As StyledItem =
                DirectCast(MyBase.Items(e.Index), StyledItem)
     
            ' Debug.WriteLine(“Measure: “ & the_item.Text)
            ' See how much room it will need.
            Dim h As Integer = 0
            Dim w As Integer = 0
            ' Allow room for the image if present.
            If the_item.Image IsNot Nothing Then
                w = the_item.Image.Width + 2 * ITEM_MARGIN
                h = the_item.Image.Height + 2 * ITEM_MARGIN
            End If
     
            ' Measure the text.
            If (the_item.Text IsNot Nothing) AndAlso (the_item.Text.Length > 0) Then
                Dim fnt As Font = the_item.Font
                If fnt Is Nothing Then fnt = Me.Parent.Font
                Dim item_size As SizeF = gr.MeasureString(the_item.Text, fnt)
                Dim txtHeight As Integer = CInt(item_size.Height + 2 * ITEM_MARGIN)
                If txtHeight > h Then h = txtHeight
                w += CInt(item_size.Width + 2 * ITEM_MARGIN)
            End If
            e.ItemWidth = w
            e.ItemHeight = h
        End Sub
        ' Draw the item.
     
        Private Sub StyledListBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
            If e.Index < 0 Or e.Index >= MyBase.Items.Count Then Exit Sub
            Dim gr As Graphics = e.Graphics
     
            ' Convert the item into a StyledListBoxItem.
            Dim the_item As StyledItem =
            DirectCast(MyBase.Items(e.Index), StyledItem)
     
            ' Debug.WriteLine(“Draw: “ & the_item.Text)
            ' Clear the item’s drawing area.
            e.DrawBackground()
            If Me.SelectedIndex = e.Index Then e.DrawFocusRectangle()
     
            ' Draw the image if present.
            Dim x As Integer = ITEM_MARGIN
            If the_item.Image IsNot Nothing Then
                Dim y As Integer =
                e.Bounds.Top + (e.Bounds.Height - the_item.Image.Height) \ 2
                gr.DrawImage(the_item.Image, x, y)
                x = the_item.Image.Width + 2 * ITEM_MARGIN
            End If
     
            ' Draw the text if present.
            If (the_item.Text IsNot Nothing) AndAlso (the_item.Text.Length > 0) Then
                Dim fnt As Font = the_item.Font
                If fnt Is Nothing Then fnt = Me.Parent.Font
                Dim item_size As SizeF =
                gr.MeasureString(the_item.Text, fnt)
                Dim y As Integer =
                e.Bounds.Top + CInt((e.Bounds.Height - item_size.Height) / 2)
                Using brush As New SolidBrush(the_item.Color)
                    e.Graphics.DrawString(the_item.Text, fnt, brush, x, y)
                End Using
            End If
        End Sub
     
    End Class
    Cet item stylé peut être aussi accepté par un control Listbox avec un code identique à celui du StyledCombox....
    code du class StyledListBox :
    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
    100
     
     
    Public Class StyledListBox
        Inherits ListBox
        ' Create a new strongly typed Items property so
        ' the Properties Window can provide an editor for it.
        ' Delegate it to the inherited Items property.
        Public Shadows Property Items() As StyledItem()
            Get
                Dim itemArray(0 To MyBase.Items.Count - 1) As StyledItem
                For i As Integer = 0 To MyBase.Items.Count - 1
                    itemArray(i) = DirectCast(MyBase.Items(i), StyledItem)
                Next i
                Return itemArray
            End Get
            Set(ByVal value() As StyledItem)
                MyBase.Items.Clear()
                For Each styledItem As StyledItem In value
                    MyBase.Items.Add(styledItem)
                Next styledItem
            End Set
        End Property
        '' Specify OwnerDraw mode.
        Public Sub New()
            MyBase.New()
            InitializeComponent()
            ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
            MyBase.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
        End Sub
        ' Set the size for an item.
        Private Const ITEM_MARGIN As Integer = 2
        Private Sub StyledListBox_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Me.MeasureItem
     
            If e.Index < 0 Or e.Index >= MyBase.Items.Count Then Exit Sub
            Dim gr As Graphics = e.Graphics
            ' Convert the item into a StyledListBoxItem.
            Dim the_item As StyledItem =
                DirectCast(MyBase.Items(e.Index), StyledItem)
     
            ' Debug.WriteLine(“Measure: “ & the_item.Text)
            ' See how much room it will need.
            Dim h As Integer = 0
            Dim w As Integer = 0
            ' Allow room for the image if present.
            If the_item.Image IsNot Nothing Then
                w = the_item.Image.Width + 2 * ITEM_MARGIN
                h = the_item.Image.Height + 2 * ITEM_MARGIN
            End If
     
            ' Measure the text.
            If (the_item.Text IsNot Nothing) AndAlso (the_item.Text.Length > 0) Then
                Dim fnt As Font = the_item.Font
                If fnt Is Nothing Then fnt = Me.Parent.Font
                Dim item_size As SizeF = gr.MeasureString(the_item.Text, fnt)
                Dim txtHeight As Integer = CInt(item_size.Height + 2 * ITEM_MARGIN)
                If txtHeight > h Then h = txtHeight
                w += CInt(item_size.Width + 2 * ITEM_MARGIN)
            End If
            e.ItemWidth = w
            e.ItemHeight = h
        End Sub
        ' Draw the item.
     
        Private Sub StyledListBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
            If e.Index < 0 Or e.Index >= MyBase.Items.Count Then Exit Sub
            Dim gr As Graphics = e.Graphics
     
            ' Convert the item into a StyledListBoxItem.
            Dim the_item As StyledItem =
            DirectCast(MyBase.Items(e.Index), StyledItem)
     
            ' Debug.WriteLine(“Draw: “ & the_item.Text)
            ' Clear the item’s drawing area.
            e.DrawBackground()
            If Me.SelectedIndex = e.Index Then e.DrawFocusRectangle()
     
            ' Draw the image if present.
            Dim x As Integer = ITEM_MARGIN
            If the_item.Image IsNot Nothing Then
                Dim y As Integer =
                e.Bounds.Top + (e.Bounds.Height - the_item.Image.Height) \ 2
                gr.DrawImage(the_item.Image, x, y)
                x = the_item.Image.Width + 2 * ITEM_MARGIN
            End If
     
            ' Draw the text if present.
            If (the_item.Text IsNot Nothing) AndAlso (the_item.Text.Length > 0) Then
                Dim fnt As Font = the_item.Font
                If fnt Is Nothing Then fnt = Me.Parent.Font
                Dim item_size As SizeF =
                gr.MeasureString(the_item.Text, fnt)
                Dim y As Integer =
                e.Bounds.Top + CInt((e.Bounds.Height - item_size.Height) / 2)
                Using brush As New SolidBrush(the_item.Color)
                    e.Graphics.DrawString(the_item.Text, fnt, brush, x, y)
                End Using
            End If
        End Sub
     
    End Class
    Le StyledComboBox ou le StyledListBox apres generation apparaissent dans la boite à outils du Designer VS, il suffit de les dropper et remplir leur collection Items ....
    bon code...

Discussions similaires

  1. Réponses: 3
    Dernier message: 26/05/2010, 17h45
  2. portée d'une variable dans une fonction dans une méthode
    Par laurentg2003 dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 29/06/2009, 19h05
  3. Envoyer une formulaire dans une page dans une Frame
    Par zooffy dans le forum Balisage (X)HTML et validation W3C
    Réponses: 5
    Dernier message: 29/06/2007, 10h13
  4. Comment afficher des images dans crystalreport
    Par rddev dans le forum VB.NET
    Réponses: 1
    Dernier message: 18/05/2007, 02h44
  5. [TP][MULTI-PROBLEME]Comment afficher des images pcx
    Par mikoeur dans le forum Turbo Pascal
    Réponses: 7
    Dernier message: 24/10/2002, 13h57

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