コピー可能なメッセージボックス2

System.Windows.Forms.MessageBoxButtonsを受け取って動的にボタンを作る。

続き。SetButtonsメソッドの実装について。
ボタンを格納しておく領域が必要そうだ。それに、ボタンを配置しておくパネルがあると楽ができそう。
追加しておこう。

Private _Buttons As List(Of System.Windows.Forms.Button)
Private WithEvents ButtonPanel As System.Windows.Forms.Panel

あまりいい方法が思い浮かばないので、Enumの内容を愚直に翻訳することにした。

    ''' <summary>
    ''' MessageDialogにボタンを設定します。
    ''' </summary>
    ''' <param name="buttons">表示するボタンを定義する定数を指定します。</param>
    ''' <remarks></remarks>
    Private Sub SetButtons(ByVal buttons As System.Windows.Forms.MessageBoxButtons)
        Select Case buttons
            Case Windows.Forms.MessageBoxButtons.AbortRetryIgnore
                AddButton(System.Windows.Forms.DialogResult.Abort)
                AddButton(System.Windows.Forms.DialogResult.Retry)
                AddButton(System.Windows.Forms.DialogResult.Ignore)

            Case Windows.Forms.MessageBoxButtons.OK
                AddButton(System.Windows.Forms.DialogResult.OK)

            Case Windows.Forms.MessageBoxButtons.OKCancel
                AddButton(System.Windows.Forms.DialogResult.OK)
                AddButton(System.Windows.Forms.DialogResult.Cancel)

            Case Windows.Forms.MessageBoxButtons.RetryCancel
                AddButton(System.Windows.Forms.DialogResult.Retry)
                AddButton(System.Windows.Forms.DialogResult.Cancel)

            Case Windows.Forms.MessageBoxButtons.YesNo
                AddButton(System.Windows.Forms.DialogResult.Yes)
                AddButton(System.Windows.Forms.DialogResult.No)

            Case Windows.Forms.MessageBoxButtons.YesNoCancel
                AddButton(System.Windows.Forms.DialogResult.Yes)
                AddButton(System.Windows.Forms.DialogResult.No)
                AddButton(System.Windows.Forms.DialogResult.Cancel)

            Case Else
                ' なにもしない

        End Select

    End Sub
    ''' <summary>
    ''' MessageDialogにボタンを追加します。
    ''' 既存のボタンがある場合は、左側にシフトします。
    ''' 新しいボタンは常に画面の一番右に追加されます。
    ''' </summary>
    ''' <param name="dialogResult">
    ''' 追加するボタンに設定する System.Windows.Forms.DialogResult 値のいずれか。
    ''' </param>
    ''' <remarks>
    ''' 引数:<paramref name="dialogResult">dialogResult</paramref>によってボタンの表示文字列が自動設定されます。
    ''' </remarks>
    Private Sub AddButton(ByVal dialogResult As System.Windows.Forms.DialogResult)
        Dim button As New System.Windows.Forms.Button()
        button.Size = New System.Drawing.Size(BUTTON_WIDTH, BUTTON_HEIGHT)
        button.Location = New System.Drawing.Point(Me.ButtonPanel.Size.Width - BUTTON_WIDTH - BUTTONS_MARGIN, 0)
        button.Anchor = Windows.Forms.AnchorStyles.Bottom Or Windows.Forms.AnchorStyles.Right

        ' 既存のボタンを移動
        For Each btn As System.Windows.Forms.Button In _Buttons
            btn.Location = New System.Drawing.Point(btn.Location.X - BUTTON_WIDTH - BUTTONS_INTERVAL, btn.Location.Y)
        Next

        Select Case dialogResult
            Case Windows.Forms.DialogResult.OK
                button.Text = "OK"

            Case Windows.Forms.DialogResult.Cancel
                button.Text = "キャンセル"

            Case Windows.Forms.DialogResult.Abort
                button.Text = "中止"

            Case Windows.Forms.DialogResult.Retry
                button.Text = "再試行"

            Case Windows.Forms.DialogResult.Ignore
                button.Text = "無視"

            Case Windows.Forms.DialogResult.Yes
                button.Text = "はい"

            Case Windows.Forms.DialogResult.No
                button.Text = "いいえ"

            Case Else
                Return
        End Select

        button.DialogResult = dialogResult
        _Buttons.Add(button)
        Me.ButtonPanel.Controls.Add(button)
    End Sub

すっげー力技な気がする。。。
まぁいい考えが浮かんだら差し替えることにして次行ってみよー。

System.Windows.Forms.MessageBoxDefaultButtonを受け取って既定のボタンを設定。

SetDefaultButtonメソッドの実装。
System.Windows.Forms.MessageBoxの動作をみてみると、
左から順にButton1、Button2〜となっているようだ。
仮にボタンが2つしかない状態でButton3を指定するとButton1が既定になる。
これをなぞると、多分こんなコードになる。

    ''' <summary>
    ''' MessageDialogの既定のボタンを設定します。
    ''' </summary>
    ''' <param name="defaultButton">表示する既定のボタンを定義する定数を指定します。</param>
    ''' <remarks></remarks>
    Private Sub SetDefaultButton(ByVal defaultButton As System.Windows.Forms.MessageBoxDefaultButton)
        Me.AcceptButton = Nothing

        Dim max As Integer = 0
        Select Case defaultButton
            Case Windows.Forms.MessageBoxDefaultButton.Button1
                max = 0
            Case Windows.Forms.MessageBoxDefaultButton.Button2
                max = 1
            Case Windows.Forms.MessageBoxDefaultButton.Button3
                max = 2
            Case Else
                Return
        End Select
        If (max >= _Buttons.Count) Then max = _Buttons.Count - 1

        For i As Integer = max To 0 Step -1
            If (_Buttons(i) IsNot Nothing) Then
                Me.AcceptButton = _Buttons(i)
                Exit For
            End If
        Next
    End Sub

またまた長いので続きは別途。