Dessiner un rectangle de couleur dans un combo box (VB.NET)
janctil
Messages postés25Date d'inscriptionvendredi 25 avril 2003StatutMembreDernière intervention 2 novembre 2004
-
23 avril 2004 à 19:30
nhervagault
Messages postés6063Date d'inscriptiondimanche 13 avril 2003StatutMembreDernière intervention15 juillet 2011
-
25 avril 2004 à 12:33
Bonjour à tous,
J'ai un petit problème avec une liste déroulante me servant à afficher une liste de couleur, avec un rectangle affichant la couleur. L'affichage de la liste se fait très bien, mais quand je sélectionne un élément, le combo affiche ceci "Color [LimeGreen]" ... j'ai essayé de redessiner le rectangle (sur l'évén. SelectIndexChanged par ex.) après la sélection, mais rien ne marche...
Voici le code sur la liste déroulante :
'Sur le form load, je charge la liste
Private Sub frmSettings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ColorName As String
For Each ColorName In _
System.Enum.GetNames _
(GetType(System.Drawing.KnownColor))
ComboBox1.Items.Add(Color.FromName(ColorName))
Next
End Sub
'Dessine les rectangles et les noms de couleurs dans la liste
Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
If e.Index < 0 Then
e.DrawBackground()
e.DrawFocusRectangle()
Exit Sub
End If
'Obtenir la couleur à partir de l'élément de la liste
Dim aColor As Color = _
CType(ComboBox1.Items(e.Index), Color)
'Obtenir un rectangleheight
Dim rect As Rectangle = New Rectangle _
(2, e.Bounds.Top + 2, e.Bounds.Height, _
e.Bounds.Height - 4)
Dim br As Brush
e.DrawBackground()
e.DrawFocusRectangle()
If e.State = _
Windows.Forms.DrawItemState.Selected Then
br = Brushes.White
Else
br = Brushes.Black
End If
'Dessiner un rectangle et le remplir
e.Graphics.DrawRectangle(New Pen(aColor), rect)
e.Graphics.FillRectangle(New SolidBrush _
(aColor), rect)
'Dessiner une bordure
rect.Inflate(1, 1)
e.Graphics.DrawRectangle(Pens.Black, rect)
'Dessiner le nom de la couleur
e.Graphics.DrawString(aColor.Name, _
cboChatSend.Font, br, e.Bounds.Height + 5, _
((e.Bounds.Height - cboChatSend.Font.Height) _
\ 2) + e.Bounds.Top)
End Sub
Private Sub ComboBox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles ComboBox1.MeasureItem
e.ItemHeight = 16
End Sub
nhervagault
Messages postés6063Date d'inscriptiondimanche 13 avril 2003StatutMembreDernière intervention15 juillet 201137 24 avril 2004 à 00:21
Voila amuses toi bien
Friend WithEvents comboboxcarre1 As New comboboxcarre()
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
comboboxcarre1.Location = New Point(10, 10)
comboboxcarre1.Items.Add("hello")
Me.Controls.Add(Me.comboboxcarre1)
End Sub
End Class
Public Class comboboxcarre
Inherits ComboBox
Public Sub New()
Me.DrawMode = DrawMode.OwnerDrawFixed
'forcement un DropDownList
Me.DropDownStyle = ComboBoxStyle.DropDownList
End Sub
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
Me.DropDownStyle = ComboBoxStyle.DropDownList
MyBase.OnDrawItem(e)
If (e.Index = -1) Then
Return
End If
Dim oB As System.Drawing.SolidBrush = New System.Drawing.SolidBrush(Me.ForeColor)
e.Graphics.DrawString(Me.Items(e.Index).ToString(), Me.Font, oB, 15, 0)
e.Graphics.FillRectangle(New SolidBrush(Color.Blue), New Rectangle(0, 0, 15, 15))
oB.Dispose()
End Sub
End Class
nhervagault
Messages postés6063Date d'inscriptiondimanche 13 avril 2003StatutMembreDernière intervention15 juillet 201137 25 avril 2004 à 12:33
Tiens si tu veux les couleurs windows .net
utilise la reflection
c'est pratique ;-)
code un peu commenté mais non optimisé
Private WithEvents combocouleur As New combocouleur
Private Sub changer_couleur(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles combocouleur.SelectedIndexChanged
Button2.BackColor = Color.FromName(combocouleur.Items(combocouleur.SelectedIndex).ToString)
End Sub
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
combocouleur.Location = New Point(10, 10)
'on charge toutes les couleurs
'en utilisant system.reflection --> besoin de cet import
For Each f As PropertyInfo In GetType(Color).GetProperties()
combocouleur.Items.Add(f.Name)
Next
Dim i As Int32
For i = combocouleur.Items.Count - 1 To combocouleur.Items.Count - 9 Step -1
combocouleur.Items.RemoveAt(i) 'enlever les dernieres property
Next
combocouleur.Items.RemoveAt(0) ' pour enlever transparent
'faut pas oublier de l'ajouter a la form
Me.Controls.Add(combocouleur)
'ajout d'un ecouteur sur le changement d'index
'qui appelle la fonction changer_couleur
AddHandler combocouleur.SelectedIndexChanged, AddressOf changer_couleur
End Sub
End Class
Public Class combocouleur
Inherits System.windows.forms.ComboBox
Public Sub New()
'on dessine le controle
Me.DrawMode = DrawMode.OwnerDrawFixed
Me.DropDownStyle = ComboBoxStyle.DropDownList
End Sub
Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
'Me.DropDownStyle = ComboBoxStyle.DropDownList
MyBase.OnDrawItem(e)
'lorsqu'il y a pas d'items on part ;-)
If e.Index < 0 Then Return
'dessinne la couleur
e.Graphics.FillRectangle(New SolidBrush(Color.FromName(Me.Items(e.Index))), New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width - 90, e.Bounds.Height))
'ecrit le nom de la couleur
e.Graphics.DrawString(Me.Items(e.Index).ToString, Me.Font, New SolidBrush(Color.Black), 10, e.Bounds.Y)
End Sub
End Class