Ejemplo n.º 1
0
        /// <summary>Displays a message box in front of the specified object and with the specified text, caption, buttons, and icon.</summary>
        public static DialogResult Show(IWin32Window owner, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            using (ShowText st = new ShowText())
            {
                st.Message = text;
                if (caption != null)
                {
                    st.Text = caption;
                }

                // Set the dialogbox/taskbar icon to the icon of the owning form.
                if (owner != null)
                {
                    Form f = owner as Form;
                    if (f == null)
                    {
                        Control c = owner as Control;
                        if (c != null)
                        {
                            f = c.FindForm();
                        }
                    }
                    if (f != null && f.Icon != null)
                    {
                        st.Icon = (Icon)f.Icon.Clone();
                    }
                }

                // Determine the MessageBoxIcon to use
                Icon iconInstance = null;
                switch (icon)
                {
                case MessageBoxIcon.Error:                      iconInstance = iconError;               break;

                case MessageBoxIcon.Information:        iconInstance = iconInformation; break;

                case MessageBoxIcon.Question:           iconInstance = iconQuestion;    break;

                case MessageBoxIcon.Warning:            iconInstance = iconWarning;             break;
                }
                if (iconInstance != null)
                {
                    System.Windows.Forms.PictureBox pb = new PictureBox();
                    pb.SizeMode = PictureBoxSizeMode.Normal;
                    pb.Size     = iconInstance.Size;
                    pb.Image    = iconInstance.ToBitmap();
                    pb.Location = new Point(st.textBox1.Left + 2, st.textBox1.Top + (st.textBox1.Height / 2) - (pb.Height / 2));
                    pb.Anchor   = AnchorStyles.Left;
                    int shiftAmount = pb.Size.Width + 5;
                    st.textBox1.Left  += shiftAmount;
                    st.textBox1.Width -= shiftAmount;
                    st.Controls.Add(pb);
                }

                // Show the DialogBox.

                DialogResult dr = st.ShowDialog(owner);
                return(dr);
            }
        }