Subversion Repositories DevTools

Rev

Rev 2151 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace EA_ReqPro
{
        /// <summary>
        /// A simple alternative MessageBox class. This allows us to set TopMost=true for the
        /// message box.
        /// </summary>
        public class MessageBoxEx : System.Windows.Forms.Form
        {
      private System.Windows.Forms.Label label1;
      
                /// <summary>
                /// Required designer variable.
                /// </summary>
                private System.ComponentModel.Container components = null;

                public MessageBoxEx(string msg, MessageBoxButtons msgBoxButtons)
                {
                        //
                        // Required for Windows Form Designer support
                        //
                        InitializeComponent();

         // this is why we need this class - to be able to force the message box to be the
         // top most window so a user of EA is forced to deal with the message that arises
         // out of a background thread managing the ReqPro import.
         this.TopMost = true;

         // assign a minimum width for the message box based on how many buttons it needs
         int minWidth = 160;
         int maxWidth = 800;
         if (msgBoxButtons == MessageBoxButtons.YesNo)
         {
            minWidth = 280;
         }

         this.SuspendLayout();

         label1.Text = msg;

         Size sz = MeasureString(msg, maxWidth, label1.Font);

         // adjust label dimensions
         if (label1.Width > maxWidth)
            label1.Width = maxWidth;
         if (label1.Width < sz.Width)
            label1.Width = sz.Width;

         label1.Height = sz.Height + 50;

         // adjust client dimensions
         this.Width = sz.Width + 50;
         if (this.Width < minWidth)
            this.Width = minWidth;
         this.Height = sz.Height + 150;

         // adjust label position
         label1.Left = (this.Width - sz.Width)/2;
      
         // create buttons
         if (msgBoxButtons == MessageBoxButtons.OK)
         {
            Button button1 = new Button();
            button1.DialogResult = DialogResult.OK;
            button1.Size = new System.Drawing.Size(80, 24);
            button1.Name = "button1";
            button1.TabIndex = 0;
            button1.Text = "OK";
            button1.Visible = true;
            button1.Location = new System.Drawing.Point((this.Width - button1.Width)/2, label1.Top + sz.Height + 50);
            this.Controls.Add(button1);
         }
         else if (msgBoxButtons == MessageBoxButtons.YesNo)
         {
            Button button1 = new Button();
            button1.DialogResult = DialogResult.Yes;
            button1.Size = new System.Drawing.Size(80, 24);
            button1.Name = "button1";
            button1.TabIndex = 0;
            button1.Text = "Yes";
            button1.Location = new Point((this.Width - (button1.Width * 2))/3, label1.Top + sz.Height + 50);
            this.Controls.Add(button1);

            Button button2 = new Button();
            button2.DialogResult = DialogResult.No;
            button2.Size = new System.Drawing.Size(80, 24);
            button2.Name = "button2";
            button2.TabIndex = 1;
            button2.Text = "No";
            button2.Location = new Point(button1.Left + button1.Width + ((this.Width - (button2.Width * 2))/3), label1.Top + sz.Height + 50);
            this.Controls.Add(button2);
         }

         this.ResumeLayout(false);
                }

      public static DialogResult Show(string msg)
      {
         MessageBoxEx msgBox = new MessageBoxEx(msg, MessageBoxButtons.OK);
         msgBox.Text = "";
         return msgBox.ShowDialog();
      }

      public static DialogResult Show(string msg, MessageBoxButtons msgBoxButtons)
      {
         MessageBoxEx msgBox = new MessageBoxEx(msg, msgBoxButtons);
         msgBox.Text = "";
         return msgBox.ShowDialog();
      }

      public static DialogResult Show(string msg, string caption, MessageBoxButtons msgBoxButtons)
      {
         MessageBoxEx msgBox = new MessageBoxEx(msg, msgBoxButtons);
         msgBox.Text = caption;
         return msgBox.ShowDialog();
      }

      public static DialogResult Show(string msg, string caption)
      {
         MessageBoxEx msgBox = new MessageBoxEx(msg, MessageBoxButtons.OK);
         msgBox.Text = caption;
         return msgBox.ShowDialog();
      }

                /// <summary>
                /// Clean up any resources being used.
                /// </summary>
                protected override void Dispose( bool disposing )
                {
                        if( disposing )
                        {
                                if(components != null)
                                {
                                        components.Dispose();
                                }
                        }
                        base.Dispose( disposing );
                }

                #region Windows Form Designer generated code
                /// <summary>
                /// Required method for Designer support - do not modify
                /// the contents of this method with the code editor.
                /// </summary>
                private void InitializeComponent()
                {
         this.label1 = new System.Windows.Forms.Label();
         this.SuspendLayout();
         // 
         // label1
         // 
         this.label1.Location = new System.Drawing.Point(8, 24);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(280, 32);
         this.label1.TabIndex = 1;
         this.label1.Text = "label1";
         // 
         // MessageBoxEx
         // 
         this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
         this.ClientSize = new System.Drawing.Size(292, 168);
         this.Controls.Add(this.label1);
         this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
         this.MaximizeBox = false;
         this.MinimizeBox = false;
         this.Name = "MessageBoxEx";
         this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
         this.Text = "caption";
         this.TopMost = true;
         this.ResumeLayout(false);

      }
                #endregion


      /// <summary>
      /// Measures a string using the Graphics object for this form with
      /// the specified font
      /// </summary>
      /// <param name="str">The string to measure</param>
      /// <param name="maxWidth">The maximum width available to display the string</param>
      /// <param name="font">The font with which to measure the string</param>
      /// <returns></returns>
      private Size MeasureString(string str, int maxWidth, Font font)
      {
         Graphics g = this.CreateGraphics();
         SizeF strRectSizeF = g.MeasureString(str, font, maxWidth);
         g.Dispose();

         return new Size((int)Math.Ceiling(strRectSizeF.Width), (int)Math.Ceiling(strRectSizeF.Height));
      }


      /// <summary>
      /// Measures a string using the Graphics object for this form and the
      /// font of this form
      /// </summary>
      /// <param name="str"></param>
      /// <param name="maxWidth"></param>
      /// <returns></returns>
      private Size MeasureString(string str, int maxWidth)
      {
         return MeasureString(str, maxWidth, this.Font);
      }


        }
}