CA1300: Especifique MessageBoxOptionsCA1300: Specify MessageBoxOptions

TypeNameTypeName SpecifyMessageBoxOptionsSpecifyMessageBoxOptions
Identificador de comprobaciónCheckId CA1300CA1300
CategoríaCategory Microsoft.GlobalizationMicrosoft.Globalization
Cambio problemáticoBreaking Change Poco problemáticoNon-breaking

MotivoCause

Un método llama a una sobrecarga de la System.Windows.Forms.MessageBox.Show método que no tome un System.Windows.Forms.MessageBoxOptions argumento.A method calls an overload of the System.Windows.Forms.MessageBox.Show method that does not take a System.Windows.Forms.MessageBoxOptions argument.

Descripción de la reglaRule Description

Para mostrar un cuadro de mensaje correctamente para las referencias culturales que usan un orden de lectura de derecha a izquierda, el MessageBoxOptions y MessageBoxOptions los miembros de la MessageBoxOptions enumeración debe pasarse a la Show método.To display a message box correctly for cultures that use a right-to-left reading order, the MessageBoxOptions and MessageBoxOptions members of the MessageBoxOptions enumeration must be passed to the Show method. Examine el System.Windows.Forms.Control.RightToLeft propiedad del control que contiene para determinar si se usa un orden de lectura de derecha a izquierda.Examine the System.Windows.Forms.Control.RightToLeft property of the containing control to determine whether to use a right-to-left reading order.

Cómo corregir infraccionesHow to Fix Violations

Para corregir una infracción de esta regla, llame a una sobrecarga de la Show método que toma un MessageBoxOptions argumento.To fix a violation of this rule, call an overload of the Show method that takes a MessageBoxOptions argument.

Cuándo suprimir advertenciasWhen to Suppress Warnings

Es seguro suprimir una advertencia de esta regla cuando la biblioteca de código no se puede adaptar para una referencia cultural que utiliza un orden de lectura de derecha a izquierda.It is safe to suppress a warning from this rule when the code library will not be localized for a culture that uses a right-to-left reading order.

EjemploExample

En el ejemplo siguiente se muestra un método que muestra un cuadro de mensaje que tiene opciones que son adecuadas para el orden de lectura de la referencia cultural.The following example shows a method that displays a message box that has options that are appropriate for the reading order of the culture. Se requiere un archivo de recursos, que no se muestra, para generar el ejemplo.A resource file, which is not shown, is required to build the example. Siga los comentarios en el ejemplo para compilar el ejemplo sin un archivo de recursos y probar la característica de escritura de derecha a izquierda.Follow the comments in the example to build the example without a resource file and to test the right-to-left feature.

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;
        }
    }
}

Vea tambiénSee Also

System.Resources.ResourceManager Recursos en aplicaciones de escritorioSystem.Resources.ResourceManager Resources in Desktop Apps