'Cette classe simple permet d'afficher une fenêtre de style msgbox proposant un choix multiple à l'utilisateur (voir capture)
'On a le choix du titre, de l'icône et de la liste. Le code est commenté, et un exemple est fourni.
'Ce code fonctionne en vb.net sur le framework 3.5 (visual studio 2008)
'****************************************
'SYNTAXE:
'METHODE1:
Dim resultatDuChoix As String
resultatDuChoix = MsgListBox.Show("Ceci est un titre", , "baba", "au rhum", "aux fraises", "et aux poires")
'(cette méthode est limitée à 16 éléments de choix, mais il est facile d'en rajouter)
'METHODE2:
Dim resultatDuChoix As String
'définition de la liste de choix:
Dim maListe As New Collections.Generic.List(Of String)
maListe.Add("charlotte aux framboises")
maListe.Add("charlotte aux pommes")
'appel de la fonction:
resultatDuChoix = MsgListBox.Show(maListe, "Veuillez choisir la charlotte:", SystemIcons.Question)
Source / Exemple :
Public Class MsgListBox
'Développé par mafieulemouton
'---------------------------------------------------------------------------
Inherits System.Windows.Forms.Form
Private reponseChoix As String
Private fermetureValide As Boolean
'methode d'instanciation de la classe
Private Sub New(ByVal listeDeChoix As Collections.Generic.List(Of String), Optional ByVal nomDeLaFenetre As String = "", Optional ByVal monIcone As System.Drawing.Icon = Nothing, Optional ByVal SelectionParDefaut As Integer = 0, Optional ByVal CancelButton As Boolean = False)
'si le boutton d'annulation est présent on peut fermer avec la croix meme sans rien selectionner
If CancelButton = True Then
If SelectionParDefaut = 0 Then reponseChoix = ""
fermetureValide = True
End If
'définition du formulaire
Me.Width = 600
Me.Height = 160
Me.Icon = monIcone
If monIcone Is Nothing Then Me.Icon = SystemIcons.Question
Me.CenterToScreen()
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
Me.ShowInTaskbar = True
Me.MinimizeBox = False
Me.MaximizeBox = False
Me.Text = nomDeLaFenetre
'empecher la fermeture de la fenetre
AddHandler Me.FormClosing, AddressOf empecherLafermeture
'ajouter la listbox
Dim _maListBox As New ListBox
_maListBox.Width = 560
_maListBox.Height = 52
_maListBox.Top = 20
_maListBox.Left = 10
_maListBox.SelectionMode = SelectionMode.One
For Each compteur As String In listeDeChoix
_maListBox.Items.Add(compteur)
Next
If SelectionParDefaut > 0 And SelectionParDefaut <= listeDeChoix.Count Then 'gérer la selection d'un element par defaut
_maListBox.SelectedIndex = SelectionParDefaut - 1
fermetureValide = True
End If
Me.Controls.Add(_maListBox)
'ajouter le boutton OK
Dim _monBoutonOK As New Button
With _monBoutonOK
.Top = 80
.Left = 470
.Height = 28
.Text = "OK"
End With
Me.Controls.Add(_monBoutonOK)
Me.AcceptButton = _monBoutonOK
AddHandler _monBoutonOK.Click, AddressOf BoutonOk 'relier le code du bouton OK
'ajouter le boutton cancel
If CancelButton = True Then
Dim _monBoutonCancel As New Button
With _monBoutonCancel
.Top = 80
.Left = 370
.Height = 28
.Text = "CANCEL"
End With
Me.Controls.Add(_monBoutonCancel)
Me.CancelButton = _monBoutonCancel
AddHandler _monBoutonCancel.Click, AddressOf BoutonCancel 'relier le code du bouton CANCEL
End If
'afficher le formulaire
Me.ShowDialog()
End Sub
'valide le formulaire et rend possible sa fermeture
Private Sub BoutonOk()
reponseChoix = CType(Me.Controls.Item(0), ListBox).SelectedItem
If reponseChoix <> "" Then fermetureValide = True
If CType(Me.Controls.Item(0), ListBox).SelectedItems.Count <> 0 Then Me.Close()
End Sub
'valide le formulaire et rend possible sa fermeture
Private Sub BoutonCancel()
fermetureValide = True
reponseChoix = ""
Me.Close()
End Sub
'empeche la fermeture si aucun choix n'a été fait
Private Sub empecherLafermeture(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
If fermetureValide = False Then e.Cancel = True
End Sub
'MODIFIER LA MANIERE DONT LE FORMULAIRE EST DESSINE
'Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
' MyBase.OnPaint(e) 'Appel à la méthode de la classe de base, ce qui dessine le bouton
' Dim myPen As New Pen(Color.Red, 5)
' e.Graphics.DrawRectangle(myPen, 3, 3, Me.Width - 25, Me.Height - 55) 'Ajoute un cadre sur le dessin du bouton
'End Sub
'METHODES D'AFFICHAGE
Public Overloads Shared Function Show(ByVal listeDeChoix As Collections.Generic.List(Of String), Optional ByVal nomDeLaFenetre As String = "", Optional ByVal monIcone As System.Drawing.Icon = Nothing, Optional ByVal SelectionParDefaut As Integer = 0, Optional ByVal CancelButton As Boolean = False) As String
Dim toto As New MsgListBox(listeDeChoix, nomDeLaFenetre, monIcone, SelectionParDefaut, CancelButton)
Return toto.reponseChoix
End Function
Public Overloads Shared Function Show(ByVal nomDeLaFenetre As String, ByVal monIcone As System.Drawing.Icon, ByVal SelectionParDefaut As Integer, ByVal CancelButton As Boolean, ByVal ParamArray mesOptions() As String) As String
Dim tempList As New Collections.Generic.List(Of String)
For Each choix As String In mesOptions
tempList.Add(choix)
Next
'gérer la preselection:
If SelectionParDefaut < 0 Or SelectionParDefaut > tempList.Count Then
SelectionParDefaut = 0
End If
Dim toto As New MsgListBox(tempList, nomDeLaFenetre, monIcone, SelectionParDefaut, CancelButton)
Return toto.reponseChoix
End Function
End Class
Conclusion :
--------------------------------------------------------
Toute remarque constructive est bienvenue, c'est ma première source et je serais heureux de progresser
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.