共用方式為


指定 MessageBoxOptions

更新:2007 年 11 月

型別名稱

SpecifyMessageBoxOptions

CheckId

CA1300

分類

Microsoft.Globalization

中斷變更

非中斷

原因

方法會呼叫 MessageBox.Show 方法 (不使用 System.Windows.Forms.MessageBoxOptions 引數) 的多載。

規則描述

若要對使用由右至左讀取順序的文化特性 (Culture) 正確顯示訊息方塊,MessageBoxOptions 列舉型別的 RightAlignRtlReading 成員必須傳遞至 Show 方法。檢視包含控制項的 Control.RightToLeft 屬性,以判斷是否使用由右至左的讀取順序。

如何修正違規

若要修正此規則的違規情形,請呼叫 Show 方法的多載,而這個方法會採用 MessageBoxOptions 引數。

隱藏警告的時機

如果不會針對使用由右至左讀取順序的文化特性當地語系化程式碼程式庫,則您可以放心地隱藏這項規則的警告。

範例

下列範例顯示方法,該方法顯示具有適用於文化特性讀取順序選項的訊息方塊。建置 (Build) 範例時需要資源檔 (未顯示)。遵循範例中的註解以在不使用資源檔的狀況下建置範例,並測試由右至左功能。

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