Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube
Showing posts with label .Net. Show all posts
Showing posts with label .Net. Show all posts

Saturday 22 March 2014

How To Create Message Box In .Net

Message Box is used to inform something to the user. Message box interrupts user and user must have to answer message box to process further. Most of time this type of message box you find in error message, warning message, etc.

In this tutorial, I am explaining about C#.Net message box.

To create message box in C#.Net, you have to include 'System.Windows.Forms' namespace.


How To Create C#.Net Message Box - Dialog Box
C#.Net Message Box

Example:

using System.Windows.Forms;

The name of class used to create message box is 'MessageBox' and contains static method named 'Show()'.

Static method does not need to call using object, it is called directly using class name.

Message box sometimes called dialog box or popup box.

Following are the examples of message box and it is written in Form1_Load event of window form.

1. Simple Message Box

MessageBox.Show("Simple Message Box");

This message box contains just message and no title text displayed.

Simple message box looks like:


Simple Message Box In C sharp dot net







 2. Message Box With Message and Title Text

MessageBox.Show("All Code Tips ~ Programming Blog", "Title: All Code Tips");

This message box contains text message and title text.

This message box looks like:

Message Box With Title Text ~ C sharp dot net







3. Message Box With Different Buttons

DialogResult res = MessageBox.Show("Click on button", "All Code Tips", MessageBoxButtons.YesNo);

This message box contains text message, title text, Yes and No buttons.

This message box looks like:


Message Box With Buttons ~ C sharp Dot Net







MessageBoxButtons enumeration contains following options:
  • MessageBoxButtons.AbortRetryIgnore
  • MessageBoxButtons.OK
  • MessageBoxButtons.OKCancel
  • MessageBoxButtons.RetryCancel
  • MessageBoxButtons.YesNo
  • MessageBoxButtons.YesNoCancel
If you want to get result of what button user clicked then you have to use following code:

DialogResult res = MessageBox.Show("Click on button", "All Code Tips", MessageBoxButtons.YesNo);
if(res == DialogResult.Yes)
{
   MessageBox.Show("You click on Yes button");
}
else
{
   MessageBox.Show("You click on No button");
}


4. Message Box With Icons

DialogResult res = MessageBox.Show("Click on button", "All Code Tips", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

Using MessageBoxIcon enumeration, you can change icon of message box.

This message box looks like:

Message Box with Icons ~ C Sharp Dot Net ~ dialog box with icon








MessageBoxIcon enumeration contains following options:
  • MessageBoxIcon.Asterisk
  • MessageBoxIcon.Error
  • MessageBoxIcon.Exclamation
  • MessageBoxIcon.Hand
  • MessageBoxIcon.Information
  • MessageBoxIcon.None
  • MessageBoxIcon.Question
  • MessageBoxIcon.Stop
  • MessageBoxIcon.Warning
As your requirement, you can use any above options with your message box.

5. Default Button Settings

DialogResult res = MessageBox.Show("Click on button", "All Code Tips", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);

Using MessageBoxDefaultButton enumeration, you can set default button. If you set default Button2 then button - 2 is selected.

This message box looks like:

Dialog Box - Message Box with default button settings ~ C sharp Dot Net








MessageBoxDefaultButton enumeration cotains following options:
  • MessageBoxDefaultButton.Button1
  • MessageBoxDefaultButton.Button2
  • MessageBoxDefaultButton.Button3

6. Setting Message Box Options

DialogResult res = MessageBox.Show("Click on button", "All Code Tips", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2, MessageBoxOptions.RightAlign);

Using MessageBoxOptions enumeration, you can set following options:
  • MessageBoxOptions.DefaultDesktopOnly
  • MessageBoxOptions.RightAlign
  • MessageBoxOptions.RtlReading
  • MessageBoxOptions.ServiceNotification
In above code, we use RightAlign option and that message box looks like:

Message Box - Dialog Box with other options ~ C sharp Dot Net









If you find any problem then comment below.

Tuesday 10 December 2013

Draw Rounded Rectangle Using GDI+ in C#

In the below code, I created one method called: DrawRoundedRectangle() that makes rectangle with rounded corners. Call this method from form's Paint event. I used FillPath() method to fill rectangle, you can use DrawPath() method to just draw rectangle. FillPath() contains parameter like first object of Brush and second object of GraphicsPath. DrawPath() contains parameter like first object of Pen and second object of GraphicsPath.



Rounded Corner of Rectangle shape using GDI+ in C#

.cs file code:
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;
using System.Drawing.Drawing2D;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.BackColor = Color.DarkGray;
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            
            Brush myBrush = new SolidBrush(Color.Blue);
            // Blue Color Rounded Rectangle
            DrawRoundedRectangle(g, new Rectangle(50, 50, 100, 100),
                                                       5, myBrush);

            // Red Color Rounded Rectangle
            DrawRoundedRectangle(g, new Rectangle(160, 50, 100, 100), 
                                      30, new SolidBrush(Color.Red));

            // Yellow Color Rounded Rectangle
            DrawRoundedRectangle(g, new Rectangle(270, 50, 100, 100),
                                   50, new SolidBrush(Color.Yellow));

            // Green Color Rounded Rectangle
            DrawRoundedRectangle(g, new Rectangle(160, 160, 100, 100),
                                 80,new SolidBrush(Color.Green));
        }
        public static void DrawRoundedRectangle(Graphics g, 
                                   Rectangle r, int d, Brush myBrush)
        {
            GraphicsPath gp = new GraphicsPath();

            gp.AddArc(r.X, r.Y, d, d, 180, 90);
            gp.AddArc(r.X + r.Width - d, r.Y, d, d, 270, 90);
            gp.AddArc(r.X + r.Width - d, r.Y + r.Height - d, d, d,
                                                             0, 90);
            gp.AddArc(r.X, r.Y + r.Height - d, d, d, 90, 90);
            gp.AddLine(r.X, r.Y + r.Height - d, r.X, r.Y + d / 2);

            g.FillPath(myBrush, gp);
        }
    }
}

Monday 9 December 2013

Ellipse Line and Rectangle Drawing using GDI+ in C#

Below Code Snippet Illustrates basic GDI+ shape like Line, Ellipse and Rectangle. Code Generates Following  Output.

Output of basic shape using gdi+ library
Output of below code


.cs File Code:

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;
using System.Drawing.Drawing2D;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
            this.BackColor = Color.Cornsilk;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics dc = e.Graphics;

            //200,10 is top,left, 50=width, 50=height
            // 3 pixels wide
            Pen bluePen = new Pen(Color.Blue, 3); 
            dc.DrawEllipse(bluePen, 200, 10, 50, 50);  
            
            Pen greenPen = new Pen(Color.Green,4);
            dc.DrawRectangle(greenPen, 200, 60, 50, 50);

            Pen redPen1 = new Pen(Color.Red, 2);
            dc.DrawLine(redPen1, 210, 110, 210, 150);

            Pen redPen2 = new Pen(Color.Red, 2);
            dc.DrawLine(redPen2, 240, 110, 240, 150);

            Pen redPen3 = new Pen(Color.Red, 2);
            dc.DrawLine(redPen3, 200, 60, 150, 80);

            Pen redPen4 = new Pen(Color.Red, 2);
            dc.DrawLine(redPen4, 250, 60, 300, 40);

        }
    }
}