Lets start dude programmer with a short tutorial, In this tutorial we will see how to create your own simple Custom MessageBox in C#.
Here we are going to create Android Toast like MessageBox which pops up the message and automatically gets close after some time.
Your CustomMessageBox will look like the above picture, the text in center and a semi transparent background. |
First of all Create a new Windows Form Application project in C#
Go to the form properties and change the following properties :
Opacity - 70%
StartPosition - CenterScreen
FormBorderStyle - None
BackColor - Black
Now Resize your form a bit smaller !!
Add a Label from ToolBox and align it in the Center of the form
Select the Label and change the following properties :
BackColor - Transparent
FontSize - 30 or 35
ForeColor - White
Add a Timer and set the following properties :
Enabled - True
Interval - 3000 // This is the time after which your CustomMessageBox will be closed automatically
Double click the Timer and add this code :
this.Close(); //This will close the CustomMessageBox after the specified time/interval
Now go to your constructor i.e :
public CustomMessageBox() //Example of constructor , Here CustomMessageBox is my form name
{
InitializeComponent();
}
Add this code below your constructor :
public CustomMessageBox(String Message) //Code to be added below the constructor
{
InitializeComponent();
label1.Text = Message;
}
Note : CustomMessageBox is my form Name you should replace it with your form Name.
Download Source Code
How to use it?
Well its very simple add a windows form application and set it as startup form
Now add a button to your form & change its text to Show MessageBox
Double click the button and add the following code :
CustomMessageBox Obj= new CustomMessageBox("Your Message Here!"); //Creating an object of CustomMessageBox Class
Obj.Show(); //Displaying the form
Enjoyed the Article, Then don't forgot to Share it :)
0 replies:
Post a Comment