Bonjour,
Je souhaite centrer les items d'une ListBox mais je ne trouve pas la méthode pour y parvenir...
J'utilise visual basic.net 2010 et je ne souhaite pas utiliser WPF.
Encore merci pour votre aide,
wChris79.
Bonjour,
Je souhaite centrer les items d'une ListBox mais je ne trouve pas la méthode pour y parvenir...
J'utilise visual basic.net 2010 et je ne souhaite pas utiliser WPF.
Encore merci pour votre aide,
wChris79.
J'ai trouvé ça qui pourrait être un début de piste...
Bonjour wChris79
Pour agrementer l'apparence de ton listbox (and menus, some of the list controls, the tab page control, and status bar panel control) utilise la prop DrawMode
- soit à OwnerDrawFixed ' Fixed-size custom drawing of each item
- soit à OwnerDrawVariable ' Variable-size custom drawing of each item.
Dans l'event ListBox1_DrawItem tu peux personnaliser l'apparence du text grace à FormatString:
-alignement centre,rapproche ,eloigne ,couleur du text ,le font ,le retour à la ligne,hauteur de ligne et j'en passe......
code exemple:
bon 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 'Ajouter : '-un listbox '-un bouton Public Class Form1 Public Sub New() ' Cet appel est requis par le Concepteur Windows Form. InitializeComponent() ' Ajoutez une initialisation quelconque après l'appel InitializeComponent(). 'Mode de presentation ou longueur est variable Me.ListBox1.DrawMode = DrawMode.OwnerDrawVariable Me.ListBox1.ScrollAlwaysVisible = True Me.ListBox1.HorizontalScrollbar = True End Sub Private Sub btnFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFill.Click Dim x As Integer ' Add items that are wide to the ListBox. For x = 0 To 10 ListBox1.Items.Add("Item " + x.ToString() + " is a very large value that requires scroll bars") Next x End Sub 'Personnalise le dessin et mets l'alignement centre du text 'avec FormatString Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem ' Draw the background e.DrawBackground() ' Get the default font Dim drawDefautFont As Font = e.Font Dim ourFont As Boolean = False ' Draw in italics if selected If (e.State And DrawItemState.Selected) = _ DrawItemState.Selected Then ourFont = True drawDefautFont = New Font(drawDefautFont, FontStyle.Italic) End If ' Turn on custom formatting Dim myFormat As StringFormat = New StringFormat() myFormat.Alignment = StringAlignment.Center If e.Index <> -1 Then 'si ListBox n'est pas vide ' Draw the listbox item e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), _ drawDefautFont, New SolidBrush(e.ForeColor), e.Bounds, myFormat) End If If ourFont Then drawDefautFont.Dispose() ' Draw the focus rectangle e.DrawFocusRectangle() End Sub Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged DisplayHScroll() End Sub 'DisplayHScroll est appele lors de ListBox1_SelectedIndexChanged ' Activer la barre horizontale : '-HorizontalScrollbar=True '-HorizontalExtent=largueur du plus grand element 'Car la barre ne sera visible que 'si HorizontalExtent=largueur du plus grand element. 'ceci est specifique au ListBox Private Sub DisplayHScroll() ' Make no partial items are displayed vertically. ListBox1.IntegralHeight = True ' Display a horizontal scroll bar. ListBox1.HorizontalScrollbar = True ' Create a Graphics object to use when determining the size of the largest item in the ListBox. ' ce -g- sert juste à mesurer la longueur du dernier item Dim g As System.Drawing.Graphics = ListBox1.CreateGraphics() ' Determine the size for HorizontalExtent using the MeasureString method using the last item in the list. Dim hzSize As Integer = g.MeasureString(ListBox1.Items(ListBox1.Items.Count - 1).ToString(), ListBox1.Font).Width ' Set the HorizontalExtent property. ListBox1.HorizontalExtent = hzSize End Sub End Class
OK. Merci beaucoup pour votre aide rapide et efficace.
J'ai testé le code il fonctionne parfaitement. Reste à l'adapter à mon application...
Je vais à présent étudier votre code, code que je n'aurai jamais trouvé seul...
Et je doute fort être capable de produire du code semblable un jour...
Encore merci pour votre aide,
wChris79
Partager