About DiamondDrake

Known as DiamondDrake on 25+ forums, Real name: Rickey Ward. Formerly springfield, Tn, now resides in Columbia, SC

Vista/Seven Style Question Box

by DiamondDrake 19. December 2010 23:13

I wanted a managed API free way to easily display a question Vist/Seven style task dialog in C#. So I hacked up a custom control, laid out a form and threw together a static class to make it easy to show like the standard winforms messagebox.

//2010 Rickey Ward Diamonddrake.com
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DiamondDrake.Forms
{

    public class frmQuestionBox : Form
    {
        public frmQuestionBox()
        {
            InitializeComponent();
            this.Icon = SystemIcons.Question;
            pictureBox1.Image = SystemIcons.Question.ToBitmap();
        }

        public frmQuestionBox(string caption, string question)
            : this()
        {
            this.Text = caption;
            this.QuestionText = question;
            this.DescriptionText = string.Empty;
        }

        public frmQuestionBox(string caption, string question, Image Image24x24)
            : this()
        {
            this.Text = caption;
            this.QuestionText = question;
            this.DescriptionText = string.Empty;
            pictureBox1.Image = (Image)Image24x24.Clone();
        }

        public frmQuestionBox(string caption, string question, string description)
            : this()
        {
            this.Text = caption;
            this.QuestionText = question;
            this.DescriptionText = description;
        }

        public frmQuestionBox(string caption, string question, string description, Image Image24x24)
            : this()
        {
            this.Text = caption;
            this.QuestionText = question;
            this.DescriptionText = description;
            pictureBox1.Image = (Image)Image24x24.Clone();
        }

        public string QuestionText
        {
            get { return this.lblbx_question.Text; }
            set { this.lblbx_question.Text = value; }
        }

        public string DescriptionText
        {
            get { return this.lblbx_descripton.Text; }
            set { this.lblbx_descripton.Text = value; }
        }

        #region VSDesigner stuff

                /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// 
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {
            this.pnlButtons = new System.Windows.Forms.Panel();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnOK = new System.Windows.Forms.Button();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.lblbx_question = new DiamondDrake.Controls.LabelBox();
            this.lblbx_descripton = new DiamondDrake.Controls.LabelBox();
            this.pnlButtons.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlButtons
            // 
            this.pnlButtons.BackColor = System.Drawing.SystemColors.Control;
            this.pnlButtons.Controls.Add(this.btnCancel);
            this.pnlButtons.Controls.Add(this.btnOK);
            this.pnlButtons.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.pnlButtons.Location = new System.Drawing.Point(0, 105);
            this.pnlButtons.Name = "pnlButtons";
            this.pnlButtons.Size = new System.Drawing.Size(335, 39);
            this.pnlButtons.TabIndex = 0;
            // 
            // btnCancel
            // 
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.No;
            this.btnCancel.Location = new System.Drawing.Point(171, 8);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 23);
            this.btnCancel.TabIndex = 1;
            this.btnCancel.Text = "No";
            this.btnCancel.UseVisualStyleBackColor = true;
            // 
            // btnOK
            // 
            this.btnOK.DialogResult = System.Windows.Forms.DialogResult.Yes;
            this.btnOK.Location = new System.Drawing.Point(252, 8);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new System.Drawing.Size(75, 23);
            this.btnOK.TabIndex = 0;
            this.btnOK.Text = "Yes";
            this.btnOK.UseVisualStyleBackColor = true;
            // 
            // pictureBox1
            // 
            this.pictureBox1.Location = new System.Drawing.Point(12, 12);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(48, 48);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.pictureBox1.TabIndex = 1;
            this.pictureBox1.TabStop = false;
            // 
            // lblbx_question
            // 
            this.lblbx_question.BackColor = System.Drawing.Color.Transparent;
            this.lblbx_question.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
            this.lblbx_question.ForeColor = System.Drawing.Color.Blue;
            this.lblbx_question.HorizontalAlignment = System.Drawing.StringAlignment.Near;
            this.lblbx_question.Location = new System.Drawing.Point(66, 12);
            this.lblbx_question.Name = "lblbx_question";
            this.lblbx_question.Size = new System.Drawing.Size(257, 48);
            this.lblbx_question.TabIndex = 2;
            this.lblbx_question.Text = "This is a sample question?";
            this.lblbx_question.VerticalAlignment = System.Drawing.StringAlignment.Center;
            // 
            // lblbx_descripton
            // 
            this.lblbx_descripton.BackColor = System.Drawing.Color.Transparent;
            this.lblbx_descripton.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblbx_descripton.HorizontalAlignment = System.Drawing.StringAlignment.Near;
            this.lblbx_descripton.Location = new System.Drawing.Point(12, 66);
            this.lblbx_descripton.Name = "lblbx_descripton";
            this.lblbx_descripton.Size = new System.Drawing.Size(311, 33);
            this.lblbx_descripton.TabIndex = 3;
            this.lblbx_descripton.Text = "This is a sample description";
            this.lblbx_descripton.VerticalAlignment = System.Drawing.StringAlignment.Near;
            // 
            // frmQuestionBox
            // 
            this.AcceptButton = this.btnOK;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.White;
            this.CancelButton = this.btnCancel;
            this.ClientSize = new System.Drawing.Size(335, 144);
            this.Controls.Add(this.lblbx_descripton);
            this.Controls.Add(this.lblbx_question);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.pnlButtons);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmQuestionBox";
            this.ShowInTaskbar = false;
            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "frmQuestionBox";
            this.TopMost = true;
            this.pnlButtons.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel pnlButtons;
        private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.Button btnOK;
        private System.Windows.Forms.PictureBox pictureBox1;
        private DiamondDrake.Controls.LabelBox lblbx_question;
        private DiamondDrake.Controls.LabelBox lblbx_descripton;

        #endregion


    }

    public static class QuestionBox
    {
        public static DialogResult Show(string caption, string question)
        {
            using (frmQuestionBox dialog = new frmQuestionBox(caption, question))
            {
                return dialog.ShowDialog();
            }    
        }

        public static DialogResult Show(string caption, string question, string description)
        {
            using (frmQuestionBox dialog = new frmQuestionBox(caption, question, description))
            {
                return dialog.ShowDialog();
            }
        }

        public static DialogResult Show(string caption, string question, string description, Image Image24x24)
        {
            using (frmQuestionBox dialog = new frmQuestionBox(caption, question, description, Image24x24))
            {
                return dialog.ShowDialog();
            }
        }

        public static DialogResult Show(string caption, string question, Image Image24x24)
        {
            using (frmQuestionBox dialog = new frmQuestionBox(caption, question, Image24x24))
            {
                return dialog.ShowDialog();
            }
        }
    }
}

namespace DiamondDrake.Controls
{
    public class LabelBox : Control
    {
        public LabelBox()
        {
            //Set up double buffering and a little extra.
            this.SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer |
            ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor,
            true);

            //set up some default values
            this.BackColor = Color.Transparent;
            _stringformat = new StringFormat();
            _stringformat.LineAlignment = StringAlignment.Near;
            _stringformat.Alignment = StringAlignment.Center;
            this.Font = new Font("Microsoft Sans Serif", 12F);
        }

        private StringFormat _stringformat;

        public StringAlignment VerticalAlignment
        {
            get { return _stringformat.LineAlignment; }
            set { _stringformat.LineAlignment = value; this.Invalidate(); }
        }
        public StringAlignment HorizontalAlignment
        {
            get { return _stringformat.Alignment; }
            set { _stringformat.Alignment = value; this.Invalidate(); }
        }
        

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), this.ClientRectangle, _stringformat);
        }


    }
    
}

Tags:

Programming

Comments

9/16/2011 6:04:22 PM #

throttle body spacer

Hi. I needed to drop you a quick note to specific my thanks. Ive been following your weblog for a month or so and have picked up a ton of fine info and loved the tactic youve structured your site. I am trying to run my very personal blog nonetheless I think its too normal and I need to give attention to numerous smaller topics. Being all things to all of us is not all that its cracked up to be.

throttle body spacer United States | Reply

9/23/2011 10:26:27 AM #

Welded wire mesh

It is the best time to make some plans for the future and it’s time to be happy. I have read this post and if I could I want to suggest you some interesting things or tips. Maybe you could write next articles referring to this article. I wish to read more things about it!

Welded wire mesh United States | Reply

11/16/2011 4:05:38 AM #

pasatiempo luxury homes

Pretty complex but the codes are all there anyway.  Great job! Let me try this one out. Thanks for sharing!

Gregg

pasatiempo luxury homes United States | Reply

1/12/2012 7:51:18 AM #

oakleys

12HLKKFJBEAlike it very much!

oakleys People's Republic of China | Reply

1/17/2012 8:54:18 AM #

cleaning services epping

I was looking at some of your articles on this site and I think this internet site is really informative ! Continue putting up.

cleaning services epping United States | Reply

2/4/2012 10:40:46 AM #

oakleys sunglasses

XSNLKNYTYXWM
Oakley Sunglasses are very well.

oakleys sunglasses United States | Reply

2/22/2012 8:55:16 AM #

SJR builder

Youre so cool! I dont suppose Ive read anything like this before. So nice to find somebody with some original thoughts on this subject. realy thank you for starting this up. this website is something that is needed on the web, someone with a little originality. useful job for bringing something new to the internet!

SJR builder United States | Reply

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading




Copyright © 2010 DiamondDrake Design - Rickey Ward Powered by BlogEngine.NET 1.6.1.0 - Log in