Subversion Repositories DevTools

Rev

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

Rev Author Line No. Line
2151 ghuddy 1
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Windows.Forms;
6
 
7
namespace EA_ReqPro
8
{
9
	/// <summary>
10
	/// A simple alternative MessageBox class. This allows us to set TopMost=true for the
11
	/// message box.
12
	/// </summary>
13
	public class MessageBoxEx : System.Windows.Forms.Form
14
	{
15
      private System.Windows.Forms.Label label1;
16
 
17
		/// <summary>
18
		/// Required designer variable.
19
		/// </summary>
20
		private System.ComponentModel.Container components = null;
21
 
22
		public MessageBoxEx(string msg, MessageBoxButtons msgBoxButtons)
23
		{
24
			//
25
			// Required for Windows Form Designer support
26
			//
27
			InitializeComponent();
28
 
29
         // this is why we need this class - to be able to force the message box to be the
30
         // top most window so a user of EA is forced to deal with the message that arises
31
         // out of a background thread managing the ReqPro import.
32
         this.TopMost = true;
33
 
34
         // assign a minimum width for the message box based on how many buttons it needs
35
         int minWidth = 160;
2153 ghuddy 36
         int maxWidth = 800;
2151 ghuddy 37
         if (msgBoxButtons == MessageBoxButtons.YesNo)
38
         {
39
            minWidth = 280;
40
         }
41
 
42
         this.SuspendLayout();
43
 
44
         label1.Text = msg;
45
 
46
         Size sz = MeasureString(msg, maxWidth, label1.Font);
47
 
48
         // adjust label dimensions
49
         if (label1.Width > maxWidth)
50
            label1.Width = maxWidth;
2153 ghuddy 51
         if (label1.Width < sz.Width)
52
            label1.Width = sz.Width;
2151 ghuddy 53
 
2153 ghuddy 54
         label1.Height = sz.Height + 50;
55
 
2151 ghuddy 56
         // adjust client dimensions
57
         this.Width = sz.Width + 50;
58
         if (this.Width < minWidth)
59
            this.Width = minWidth;
2153 ghuddy 60
         this.Height = sz.Height + 150;
2151 ghuddy 61
 
62
         // adjust label position
63
         label1.Left = (this.Width - sz.Width)/2;
64
 
65
         // create buttons
66
         if (msgBoxButtons == MessageBoxButtons.OK)
67
         {
68
            Button button1 = new Button();
69
            button1.DialogResult = DialogResult.OK;
70
            button1.Size = new System.Drawing.Size(80, 24);
71
            button1.Name = "button1";
72
            button1.TabIndex = 0;
73
            button1.Text = "OK";
74
            button1.Visible = true;
2153 ghuddy 75
            button1.Location = new System.Drawing.Point((this.Width - button1.Width)/2, label1.Top + sz.Height + 50);
2151 ghuddy 76
            this.Controls.Add(button1);
77
         }
78
         else if (msgBoxButtons == MessageBoxButtons.YesNo)
79
         {
80
            Button button1 = new Button();
81
            button1.DialogResult = DialogResult.Yes;
82
            button1.Size = new System.Drawing.Size(80, 24);
83
            button1.Name = "button1";
84
            button1.TabIndex = 0;
85
            button1.Text = "Yes";
2153 ghuddy 86
            button1.Location = new Point((this.Width - (button1.Width * 2))/3, label1.Top + sz.Height + 50);
2151 ghuddy 87
            this.Controls.Add(button1);
88
 
89
            Button button2 = new Button();
90
            button2.DialogResult = DialogResult.No;
91
            button2.Size = new System.Drawing.Size(80, 24);
92
            button2.Name = "button2";
93
            button2.TabIndex = 1;
94
            button2.Text = "No";
2153 ghuddy 95
            button2.Location = new Point(button1.Left + button1.Width + ((this.Width - (button2.Width * 2))/3), label1.Top + sz.Height + 50);
2151 ghuddy 96
            this.Controls.Add(button2);
97
         }
98
 
99
         this.ResumeLayout(false);
100
		}
101
 
102
      public static DialogResult Show(string msg)
103
      {
104
         MessageBoxEx msgBox = new MessageBoxEx(msg, MessageBoxButtons.OK);
105
         msgBox.Text = "";
106
         return msgBox.ShowDialog();
107
      }
108
 
109
      public static DialogResult Show(string msg, MessageBoxButtons msgBoxButtons)
110
      {
111
         MessageBoxEx msgBox = new MessageBoxEx(msg, msgBoxButtons);
112
         msgBox.Text = "";
113
         return msgBox.ShowDialog();
114
      }
115
 
116
      public static DialogResult Show(string msg, string caption, MessageBoxButtons msgBoxButtons)
117
      {
118
         MessageBoxEx msgBox = new MessageBoxEx(msg, msgBoxButtons);
119
         msgBox.Text = caption;
120
         return msgBox.ShowDialog();
121
      }
122
 
123
      public static DialogResult Show(string msg, string caption)
124
      {
125
         MessageBoxEx msgBox = new MessageBoxEx(msg, MessageBoxButtons.OK);
126
         msgBox.Text = caption;
127
         return msgBox.ShowDialog();
128
      }
129
 
130
		/// <summary>
131
		/// Clean up any resources being used.
132
		/// </summary>
133
		protected override void Dispose( bool disposing )
134
		{
135
			if( disposing )
136
			{
137
				if(components != null)
138
				{
139
					components.Dispose();
140
				}
141
			}
142
			base.Dispose( disposing );
143
		}
144
 
145
		#region Windows Form Designer generated code
146
		/// <summary>
147
		/// Required method for Designer support - do not modify
148
		/// the contents of this method with the code editor.
149
		/// </summary>
150
		private void InitializeComponent()
151
		{
152
         this.label1 = new System.Windows.Forms.Label();
153
         this.SuspendLayout();
154
         // 
155
         // label1
156
         // 
2153 ghuddy 157
         this.label1.Location = new System.Drawing.Point(8, 24);
2151 ghuddy 158
         this.label1.Name = "label1";
159
         this.label1.Size = new System.Drawing.Size(280, 32);
160
         this.label1.TabIndex = 1;
161
         this.label1.Text = "label1";
162
         // 
163
         // MessageBoxEx
164
         // 
165
         this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
166
         this.ClientSize = new System.Drawing.Size(292, 168);
167
         this.Controls.Add(this.label1);
168
         this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
169
         this.MaximizeBox = false;
170
         this.MinimizeBox = false;
171
         this.Name = "MessageBoxEx";
172
         this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
173
         this.Text = "caption";
174
         this.TopMost = true;
175
         this.ResumeLayout(false);
176
 
177
      }
178
		#endregion
179
 
180
 
181
      /// <summary>
182
      /// Measures a string using the Graphics object for this form with
183
      /// the specified font
184
      /// </summary>
185
      /// <param name="str">The string to measure</param>
186
      /// <param name="maxWidth">The maximum width available to display the string</param>
187
      /// <param name="font">The font with which to measure the string</param>
188
      /// <returns></returns>
189
      private Size MeasureString(string str, int maxWidth, Font font)
190
      {
191
         Graphics g = this.CreateGraphics();
192
         SizeF strRectSizeF = g.MeasureString(str, font, maxWidth);
193
         g.Dispose();
194
 
195
         return new Size((int)Math.Ceiling(strRectSizeF.Width), (int)Math.Ceiling(strRectSizeF.Height));
196
      }
197
 
198
 
199
      /// <summary>
200
      /// Measures a string using the Graphics object for this form and the
201
      /// font of this form
202
      /// </summary>
203
      /// <param name="str"></param>
204
      /// <param name="maxWidth"></param>
205
      /// <returns></returns>
206
      private Size MeasureString(string str, int maxWidth)
207
      {
208
         return MeasureString(str, maxWidth, this.Font);
209
      }
210
 
211
 
212
	}
213
}