次の方法で共有


CA1300: MessageBoxOption を指定します

TypeName

SpecifyMessageBoxOptions

CheckId

CA1300

[カテゴリ]

Microsoft.Globalization

互換性に影響する変更点

なし

原因

メソッドで、System.Windows.Forms.MessageBoxOptions 引数を使用しない MessageBox.Show メソッドのオーバーロードが呼び出されています。

規則の説明

右から左への読み取り順序を使用するカルチャでメッセージ ボックスを正しく表示するには、MessageBoxOptions 列挙体の RightAlign メンバーと RtlReading メンバーを、Show メソッドに渡す必要があります。格納しているコントロールの Control.RightToLeft プロパティを検証し、右から左への読み取り順序を使用するかどうかを判断します。

違反の修正方法

この規則違反を修正するには、MessageBoxOptions 引数を使用する Show メソッドのオーバーロードを呼び出します。

警告を抑制する状況

右から左への読み取り順序を使用するカルチャ向けにコード ライブラリをローカライズしない場合は、この規則による警告を抑制しても安全です。

使用例

カルチャの読み取り順序に合わせたオプションが含まれるメッセージ ボックスを表示するメソッドを次の例に示します。ここにリソース ファイルは示しませんが、この例をビルドする場合には必要です。リソース ファイルなしでこの例をビルドして、右から左への読み取り機能をテストするには、コードのコメントを参照してください。

Imports System
Imports System.Globalization
Imports System.Resources
Imports System.Windows.Forms

Namespace GlobalizationLibrary
    Class Program

        <STAThread()> _
        Shared Sub Main()
            Dim myForm As New SomeForm()

            ' Uncomment the following line to test the right to left feature.
            ' myForm.RightToLeft = RightToLeft.Yes
            Application.Run(myForm)
        End Sub
    End Class

    Public Class SomeForm : Inherits Form
        Private _Resources As ResourceManager
        Private WithEvents _Button As Button

        Public Sub New()
            _Resources = New ResourceManager(GetType(SomeForm))
            _Button = New Button()
            Controls.Add(_Button)
        End Sub

        Private Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs) Handles _Button.Click
            ' Switch the commenting on the following 4 lines to test the form.
            'Dim text As String = "Text"
            'Dim caption As String = "Caption"
            Dim text As String = _Resources.GetString("messageBox.Text")
            Dim caption As String = _Resources.GetString("messageBox.Caption")

            RtlAwareMessageBox.Show(CType(sender, Control), text, caption, _
            MessageBoxButtons.OK, MessageBoxIcon.Information, _
            MessageBoxDefaultButton.Button1, CType(0, MessageBoxOptions))
        End Sub
    End Class

    Public Module RtlAwareMessageBox

        Public Function Show(ByVal owner As IWin32Window, ByVal text As String, ByVal caption As String, ByVal buttons As MessageBoxButtons, ByVal icon As MessageBoxIcon, ByVal defaultButton As MessageBoxDefaultButton, ByVal options As MessageBoxOptions) As DialogResult
            If (IsRightToLeft(owner)) Then
                options = options Or MessageBoxOptions.RtlReading Or _
                MessageBoxOptions.RightAlign
            End If

            Return MessageBox.Show(owner, text, caption, _
            buttons, icon, defaultButton, options)
        End Function

        Private Function IsRightToLeft(ByVal owner As IWin32Window) As Boolean
            Dim control As Control = TryCast(owner, Control)

            If (control IsNot Nothing) Then
                Return control.RightToLeft = RightToLeft.Yes
            End If

            ' If no parent control is available, ask the CurrentUICulture
            ' if we are running under right-to-left.
            Return CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft
        End Function
    End Module
End Namespace
using System;
using System.Globalization;
using System.Resources;
using System.Windows.Forms;

namespace GlobalizationLibrary
{
    class Program
    {
        [STAThread]
        static void Main()
        {
            SomeForm myForm = new SomeForm();
            // Uncomment the following line to test the right-to-left feature.
            //myForm.RightToLeft = RightToLeft.Yes;
            Application.Run(myForm);
        }
    }

    public class SomeForm : Form
    {
        private ResourceManager _Resources;
        private Button _Button;
        public SomeForm()
        {
            _Resources = new ResourceManager(typeof(SomeForm));
            _Button = new Button();
            _Button.Click += new EventHandler(Button_Click);
            Controls.Add(_Button);
        }

        private void Button_Click(object sender, EventArgs e)
        {
            // Switch the commenting on the following 4 lines to test the form.
            // string text = "Text";
            // string caption = "Caption";
            string text = _Resources.GetString("messageBox.Text");
            string caption = _Resources.GetString("messageBox.Caption");
            RtlAwareMessageBox.Show((Control)sender, text, caption,
            MessageBoxButtons.OK, MessageBoxIcon.Information,
            MessageBoxDefaultButton.Button1, (MessageBoxOptions)0);
        }
    }

    public static class RtlAwareMessageBox
    {
        public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton, MessageBoxOptions options)
        {
            if (IsRightToLeft(owner))
            {
                options |= MessageBoxOptions.RtlReading |
                MessageBoxOptions.RightAlign;
            }

            return MessageBox.Show(owner, text, caption,
            buttons, icon, defaultButton, options);
        }

        private static bool IsRightToLeft(IWin32Window owner)
        {
            Control control = owner as Control;

            if (control != null)
            {
                return control.RightToLeft == RightToLeft.Yes;
            }

            // If no parent control is available, ask the CurrentUICulture
            // if we are running under right-to-left.
            return CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft;
        }
    }
}

参照

関連項目

System.Resources.ResourceManager

概念

デスクトップ アプリケーションのリソース