Solved

How to do my own MessageBox in c#

Posted on 2011-09-19
7
307 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
Comment Utility
Use ShowDialog() on an instance of the form.

eg.

x.ShowDialog();
0
 
LVL 13

Accepted Solution

by:
Naman Goel earned 83 total points
Comment Utility
0
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
What are the features that are not in the standard MessageBox and that you want to have in yours?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 167 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks naman_goel !!!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

10 Experts available now in Live!

Get 1:1 Help Now