Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
 Specify MessageBoxOptions
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:

Want more? Here are some additional resources on this topic:

Visual Studio Team System
Specify MessageBoxOptions

TypeName

SpecifyMessageBoxOptions

CheckId

CA1300

Category

Microsoft.Globalization

Breaking Change

NonBreaking

A method calls an overload of the System.Windows.Forms.MessageBox.Show method that does not take a System.Windows.Forms.MessageBoxOptions argument.

To display a message box correctly for cultures that use a right to left reading order, the RightAlign and RtlReading members of the MessageBoxOptions enumeration must be passed to the Show method. Examine the System.Windows.Forms.Control.RightToLeft property of the containing control to determine whether to use a right to left reading order.

To fix a violation of this rule, call an overload of the Show method that takes a MessageBoxOptions argument.

It is safe to exclude a warning from this rule if the code library will not be localized for a culture that uses a right to left reading order.

The following example shows a method that displays a message box with options appropriate for the reading order of the culture. A resource file, which is not shown, is required to build the example. Follow the comments in the example to build the example without a resource file and test the right to left feature.

C#
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;
        }
    }
}
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Showing a localized MessageBox without an owner window      Eric Bickle - BlueRealm ... David M. Kean - MSFT   |   Edit   |   Show History

The help topic example does not explain how to show a "right to left friendly" message box without an owner Window. The following code should work in that situation:

   MessageBoxOptions localizedOptions;
   if (CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft)
       localizedOptions = MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign;
   else
       localizedOptions = (MessageBoxOptions)0; // Unfortunately, the MessageBoxOptions enum doesn't have a predefined ".None" value.
    
   MessageBox.Show(text, message, caption, buttons, icon, defaultButton, localizedOptions);
  


[edit - David M. Kean - MSFT] Passing null (Nothing in Visual Basic) to the RtlAwareMessageBox.Show method for the owner parameter in the sample above, achieves this.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker