Utilisation d'un type enum pour créer les choix d'une fenêtre de dialogue

Contenu du snippet

Il suffit d'instancier frmMsgBoxEnum en passant en paramétre la question et le type Enum qui contient toutes les réponses possibles.
Les "_" dans le type Enum sont remplacés par des " " dans le label du checkbox.

Source / Exemple :


Namespace myForms

    Public Class frmMsgBoxEnum
        Inherits System.Windows.Forms.Form

#Region " Code généré par le Concepteur Windows Form "

        Private Const CtWidth = 210
        Private Const CtEnumSize = 30

        Public Enum test
            Toto
            tata
            titi
            tutu
            tyty
        End Enum

        Private myEnumType As Type
        Private myValue As String

        Public Sub New(ByVal enumType As Type, ByVal question As String)
            '    Public Sub New()
            MyBase.New()

            'Cet appel est requis par le Concepteur Windows Form.
            InitializeComponent()

            'Ajoutez une initialisation quelconque après l'appel InitializeComponent()
            Me.myEnumType = enumType
            CreateCkb(Me.myEnumType)
            Me.lblInfo.Text = question

        End Sub

        'La méthode substituée Dispose du formulaire pour nettoyer la liste des composants.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Requis par le Concepteur Windows Form
        Private components As System.ComponentModel.IContainer

        'REMARQUE : la procédure suivante est requise par le Concepteur Windows Form
        'Elle peut être modifiée en utilisant le Concepteur Windows Form.  
        'Ne la modifiez pas en utilisant l'éditeur de code.
        Friend WithEvents btnOK As System.Windows.Forms.Button
        Friend WithEvents lblInfo As System.Windows.Forms.Label
        Friend WithEvents gbxEnum As System.Windows.Forms.GroupBox
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.btnOK = New System.Windows.Forms.Button
            Me.lblInfo = New System.Windows.Forms.Label
            Me.gbxEnum = New System.Windows.Forms.GroupBox
            Me.SuspendLayout()
            '
            'btnOK
            '
            Me.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.System
            Me.btnOK.Location = New System.Drawing.Point(78, 232)
            Me.btnOK.Name = "btnOK"
            Me.btnOK.TabIndex = 0
            Me.btnOK.Text = "OK"
            '
            'lblInfo
            '
            Me.lblInfo.Location = New System.Drawing.Point(8, 8)
            Me.lblInfo.Name = "lblInfo"
            Me.lblInfo.Size = New System.Drawing.Size(210, 50)
            Me.lblInfo.TabIndex = 1
            Me.lblInfo.Text = "..."
            Me.lblInfo.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
            '
            'gbxEnum
            '
            Me.gbxEnum.Location = New System.Drawing.Point(10, 60)
            Me.gbxEnum.Name = "gbxEnum"
            Me.gbxEnum.Size = New System.Drawing.Size(210, 150)
            Me.gbxEnum.TabIndex = 2
            Me.gbxEnum.TabStop = False
            '
            'frmMsgBoxEnum
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(230, 262)
            Me.ControlBox = False
            Me.Controls.Add(Me.gbxEnum)
            Me.Controls.Add(Me.lblInfo)
            Me.Controls.Add(Me.btnOK)
            Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
            Me.MaximizeBox = False
            Me.MinimizeBox = False
            Me.Name = "frmMsgBoxEnum"
            Me.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide
            Me.Text = "Question ?"
            Me.ResumeLayout(False)

        End Sub

#End Region

'Création des choix
        Private Sub CreateCkb(ByVal anEnumType As Type)
            Me.SuspendLayout()

            Dim ckbArray As System.Array = System.Enum.GetValues(anEnumType)
            Dim x As Int16 = 0
            Dim first As Boolean = True

            Me.gbxEnum.Size = New System.Drawing.Size(CtWidth, CtEnumSize * ckbArray.Length + 10)
            Me.btnOK.Location = New System.Drawing.Point(Me.gbxEnum.Size.Width / 2 - Me.btnOK.Size.Width / 2, Me.gbxEnum.Size.Height + Me.lblInfo.Size.Height + 20)
            Me.Size = New System.Drawing.Size(Me.gbxEnum.Size.Width + 30, Me.lblInfo.Size.Height + Me.gbxEnum.Size.Height + Me.btnOK.Size.Height + 75)
            Me.gbxEnum.Location = New System.Drawing.Point((Me.Size.Width - Me.gbxEnum.Size.Width) / 2, Me.lblInfo.Size.Height + 10)

            For Each ckb As Object In ckbArray
                Dim checkBox As New Windows.Forms.RadioButton
                If first Then
                    checkBox.Checked = True
                    first = False
                End If
                checkBox.Name = ckb.ToString.Replace("_", " ")
                checkBox.FlatStyle = FlatStyle.System
                checkBox.Text = ckb.ToString.Replace("_", " ")
                checkBox.Size = New System.Drawing.Size(Me.gbxEnum.Size.Width - 20, 20)
                checkBox.Location = New System.Drawing.Point(10, CtEnumSize * x + 10)
                x = x + 1
                Me.gbxEnum.Controls.Add(checkBox)
            Next

            Me.ResumeLayout(False)

        End Sub

        Private Function GetRadioButtonValue() As String
            Dim rb As New Windows.Forms.RadioButton

            For Each rb In Me.gbxEnum.Controls
                If rb.Checked Then
                    Return rb.Text.Replace(" ", "_")
                End If
            Next
            Return String.Empty
        End Function

'Récupération du choix
        Public Function GetMsgBoxValue() As Object
            Me.ShowDialog()
            Return System.Enum.Parse(Me.myEnumType, GetRadioButtonValue)
        End Function

        Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
            Me.Close()
        End Sub
    End Class
End Namespace

A voir également

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.