Saturday, 5 October 2013

Uses a Combo Box to select different shapes to draw and fill (using Graphics, Pen and Solid Brush object)

uses a Combo Box to select different shapes to draw and fill in Windows Application (using Graphics, Pen and Solid Brush object

First we take one Combo Box and set Collection Property which show in the image given below



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 TestComboBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{

}
//Form uses a Combo Box to select different shapes to draw and fill (using Graphics, Pen and Solid Brush object;

private void ComboBoxForImage_SelectedIndexChanged(object sender, EventArgs e)
{

//create graphics object,Pen,SolidBrush..........................

Graphics myGraphics = base.CreateGraphics();

//create Pen using Color DrakBlue...............................

Pen myPen = new Pen(Color.DarkBlue);

//create SolidBrush using Color DrakRed...............................

SolidBrush mysolidbrush = new SolidBrush(Color.DarkRed);

//clear drawing area,setting it to color white..................

myGraphics.Clear(Color.White);

//get index of selected ComboBox for Draw Shape and Fill Shape

switch (ComboBoxForImage.SelectedIndex)
{
case 0:
//case 0: for draw Line...........(myPen,int x1-axis,int y1-axis,x2-axis,int y2-axis)
myGraphics.DrawLine(myPen, 50, 100, 200, 345);
break;
//case 1: for draw Circle...........(myPen,X-axis,Y-axis,width,height)
case 1:
myGraphics.DrawEllipse(myPen, 100, 100, 150, 150);
break;
//case 2 for draw Rectangle with height and width are different...........
case 2:
myGraphics.DrawRectangle(myPen, 100, 100, 200, 150);
break;
//case 3 for draw Square with height and width are same...........
case 3:
myGraphics.DrawRectangle(myPen, 100, 100, 100, 100);
break;
//case 4 for draw Ellipse with height and width are different...........
case 4:
myGraphics.DrawEllipse(myPen, 100, 100, 150, 85);
break;
//case 5 for draw Pie...........(myPen,X-axis,Y-axis,width,height,start angle,sweep angle)
case 5:
myGraphics.DrawPie(myPen, 50, 50, 150, 150,0,45);
break;
case 6:
myGraphics.FillEllipse(mysolidbrush, 100, 100, 150, 150);
break;
case 7:
myGraphics.FillRectangle(mysolidbrush, 100, 100, 200, 150);
break;
case 8:
myGraphics.FillRectangle(mysolidbrush, 100, 100, 100, 100);
break;
case 9:
myGraphics.FillEllipse(mysolidbrush, 100, 100, 150, 115);
break;
case 10:
myGraphics.FillPie(mysolidbrush, 50, 50, 150, 150, 0, 45);
break;

}
// release the Graphice object...................
myGraphics.Dispose();

}
}
}



1 comments: