
Introduction
I am new for codeproject and this is my first article. I have written this article to help the developers who want to create own message box. So I think it would be most helpfull for the readers. I have written code in C#. And I will continue to inhancements in it if got any sugession from readers and I will most welcome to resolve query of readers.
How To Use
Basically I have written this article when I have required this in my project and try to find code. I have found a lot of code but I had not come way of coding. So finally decided to write my own code. And try to code for this article and got success in my way.
You can not directly use this article in your application but it will be helpful when
you want to generate your message box. Follow the following steps:
1. Add new form and named it.
2. Design GUI as per given image above.
3. Declare following in partial class
private bool mouse_is_down = false;
private Point mouse_pos;
4. On Load event paste this code:
private void MsgBox_load(object sender, EventArgs e)
{
ResizeForm();
if (!btnCancel.Visible)
btnOK.Left = (this.Width - btnOK.Width) / 2;
else
btnOK.Left = this.Width / 2 - btnOK.Width - 5;
btnCancel.Left = btnOK.Right + 5;
}
//ResizeForm()
# region "Apperiance"
public void ResizeForm()
{
if (lblMessage.Width > 168)
this.Width = lblMessage.Width + 88;
int lblWidth = this.lblMessage.Width;
this.lblMessage.AutoSize = false;
this.lblMessage.Width = lblWidth;
}
#endregion
public void MyMessageBox(string message)
{
MsgBox msgbox = new MsgBox();
lblMessage.Text = message;
}
5. Code it for set button's visibility and Setting message title
public void buttonVisibility(bool OK, bool cancelstatus, bool checkbox,string msgTitle)
{
btnOK.Visible = OK;
btnCancel.Visible = cancelstatus;
chkAskagain.Visible = checkbox;
lblTitle.Text = msgTitle;
}
6. write this code for OK and Cancel action
private void btnCancel_Click(object sender, EventArgs e)
{
this.Hide();
}
private void btnOK_Click(object sender, System.EventArgs e)
{
this.Hide();
this.DialogResult = DialogResult.OK;
}
7. This code is for moving the form
private void pbMessage_MouseDown(object sender, MouseEventArgs e)
{
mouse_pos.X = e.X;
mouse_pos.Y = e.Y;
mouse_is_down = true;
}
private void pbMessage_MouseUp(object sender, MouseEventArgs e)
{
mouse_is_down = false;
}
private void pbMessage_MouseMove(object sender, MouseEventArgs e)
{
if (mouse_is_down)
{
Point current_pos = Control.MousePosition;
current_pos.X = current_pos.X - mouse_pos.X;
current_pos.Y = current_pos.Y - mouse_pos.Y;
this.Location = current_pos;
}
}
8. And last is for closing the msgbox.
private void pictureBox2_Click(object sender, EventArgs e)
{
this.Close();
}
How to call this message box in your application:
Write this code in that partial class where you have to call message box
private MsgBox msgbox = new MsgBox();
Finally write for calling of messagebox
msgbox.MyMessageBox("Hi!This is Demo Message Box");
msgbox.buttonVisibility(true, true, true,"Demo Message Box");
DialogResult msgdresult = msgbox.ShowDialog();