Solved

How to do my own MessageBox in c#

Posted on 2011-09-19
7
312 Views
Last Modified: 2013-12-17
hello,
I try to do my own and display MessageBox when called, I have already created the form.
Someone has an idea
thank you in advance
0
Comment
Question by:abgconsulting
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 167 total points
ID: 36560822
Use ShowDialog() on an instance of the form.

eg.

x.ShowDialog();
0
 
LVL 13

Accepted Solution

by:
Naman Goel earned 83 total points
ID: 36560912
0
 
LVL 40
ID: 36560946
What are the features that are not in the standard MessageBox and that you want to have in yours?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 167 total points
ID: 36560949
From the question.
I have already created the form.

All is required is then a ShowDialog so it shows and blocks the form displaying it from working until the custom message box is closed.
0
 

Author Comment

by:abgconsulting
ID: 36560972
Thks
i have found the solution
my solution is in below
public static bool Show(string content, string title, MessageBoxButtons button, MessageBoxIcon icon)
        {
            CustomMessageBox customMessageBox = new CustomMessageBox();
            customMessageBox.lblTitle.Text = title;
            customMessageBox.lbMessage.Text = content;
            customMessageBox.ShowDialog();
      
            return false;
        }

Open in new window

CustomMessageBox.Show("print PDF fails: \n" + ex.Message, "Print PFD",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);

Open in new window

0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 36561071
Here is sample code for you :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class MyMessageBox : Form
    {
        private Panel panel1;
        private Button btnOK;
        private Label lblText;
        private PictureBox picBoxIcon;
        private FlowLayoutPanel flowLayoutPanel1;
        private Panel panelText;
        private TableLayoutPanel tableLayoutPanel1;
    
        public MyMessageBox()
        {
            InitializeComponent();
        }

        public static void ShowMessageBox(Image icon, string message, string title)
        {
            MyMessageBox myMessage = new MyMessageBox();
            if (icon != null)
            {
                myMessage.picBoxIcon.Image = icon;
            }
            myMessage.lblText.Text = message;
            myMessage.Text = title;
            myMessage.ShowDialog();
        }

        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.btnOK = new System.Windows.Forms.Button();
            this.lblText = new System.Windows.Forms.Label();
            this.picBoxIcon = new System.Windows.Forms.PictureBox();
            this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
            this.panelText = new System.Windows.Forms.Panel();
            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.picBoxIcon)).BeginInit();
            this.flowLayoutPanel1.SuspendLayout();
            this.panelText.SuspendLayout();
            this.tableLayoutPanel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.btnOK);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(3, 124);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(482, 37);
            this.panel1.TabIndex = 2;
            // 
            // btnOK
            // 
            this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.btnOK.Location = new System.Drawing.Point(216, 8);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new System.Drawing.Size(75, 23);
            this.btnOK.TabIndex = 0;
            this.btnOK.Text = "OK";
            this.btnOK.UseVisualStyleBackColor = true;
            // 
            // lblText
            // 
            this.lblText.AutoSize = true;
            this.lblText.Location = new System.Drawing.Point(12, 24);
            this.lblText.Name = "lblText";
            this.lblText.Size = new System.Drawing.Size(0, 13);
            this.lblText.TabIndex = 0;
            // 
            // picBoxIcon
            // 
            this.picBoxIcon.Location = new System.Drawing.Point(3, 3);
            this.picBoxIcon.Name = "picBoxIcon";
            this.picBoxIcon.Size = new System.Drawing.Size(100, 109);
            this.picBoxIcon.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.picBoxIcon.TabIndex = 0;
            this.picBoxIcon.TabStop = false;
            // 
            // flowLayoutPanel1
            // 
            this.flowLayoutPanel1.Controls.Add(this.picBoxIcon);
            this.flowLayoutPanel1.Controls.Add(this.panelText);
            this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.flowLayoutPanel1.Location = new System.Drawing.Point(3, 3);
            this.flowLayoutPanel1.Name = "flowLayoutPanel1";
            this.flowLayoutPanel1.Size = new System.Drawing.Size(482, 115);
            this.flowLayoutPanel1.TabIndex = 1;
            // 
            // panelText
            // 
            this.panelText.Controls.Add(this.lblText);
            this.panelText.Location = new System.Drawing.Point(109, 3);
            this.panelText.Name = "panelText";
            this.panelText.Size = new System.Drawing.Size(364, 109);
            this.panelText.TabIndex = 1;
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.ColumnCount = 1;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tableLayoutPanel1.Controls.Add(this.flowLayoutPanel1, 0, 0);
            this.tableLayoutPanel1.Controls.Add(this.panel1, 0, 1);
            this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 2;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 73.86364F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 26.13636F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(488, 164);
            this.tableLayoutPanel1.TabIndex = 1;
            // 
            // MyMessageBox
            // 
            this.ClientSize = new System.Drawing.Size(488, 164);
            this.Controls.Add(this.tableLayoutPanel1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "MyMessageBox";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.panel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.picBoxIcon)).EndInit();
            this.flowLayoutPanel1.ResumeLayout(false);
            this.flowLayoutPanel1.PerformLayout();
            this.panelText.ResumeLayout(false);
            this.panelText.PerformLayout();
            this.tableLayoutPanel1.ResumeLayout(false);
            this.ResumeLayout(false);

        }
    }
}

Open in new window



you can call your messagebox like this :

MyMessageBox.ShowMessageBox(null, "Hello dear", "Message");
0
 

Author Comment

by:abgconsulting
ID: 36561275
thanks naman_goel !!!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question