Solved

How to do my own MessageBox in c#

Posted on 2011-09-19
7
310 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

943 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now