Monday, December 21, 2009

My First C# Program

Summary:
My First program in C#, in Object Oriented Programming Class,
Our task was to create a program similar to the MS Paint Pencil.
So here is the program...

Program :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace finallineproject
{
public partial class Form3 : Form
{
ArrayList colors = new ArrayList();
Graphics g;
int x1, y1, x2, y2;
int counter;
int ctr;
public Form3()
{
InitializeComponent();
g = this.CreateGraphics();
colors.Add(Color.Red);
colors.Add(Color.Blue);
colors.Add(Color.Green);
colors.Add(this.BackColor);
}
private void Form3_Load(object sender, EventArgs e)
{
}
private void Form3_MouseDown(object sender, MouseEventArgs e)
{
if (radioButton2.Checked == true)
{
x1 = e.X;
y1 = e.Y;
ctr = 1;
}
}
private void Form3_MouseMove(object sender, MouseEventArgs e)
{
if (radioButton2.Checked == true)
{
if (ctr == 1)
{
if (counter == 1)
{
x2 = e.X;
y2 = e.Y;
Pen p = new Pen((Color)colors[comboBox1.SelectedIndex]);
g.DrawLine(p, x1, y1, x2, y2);
counter = 0;
}
if (counter == 0)
{
x1 = e.X;
y1 = e.Y;
counter = 1;
}
}
}
}
private void Form3_MouseUp(object sender, MouseEventArgs e)
{
if (radioButton2.Checked == true)
{
ctr = 0;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
Pen p = new Pen(this.BackColor);
g.DrawLine(p, x1, y1, x2, y2);
}
if (radioButton2.Checked == true)
{
Pen p2 = new Pen(this.BackColor);
g.DrawLine(p2, x1, y1, x2, y2);
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
}
private void Form3_MouseClick(object sender, MouseEventArgs e)
{
if (radioButton1.Checked == true)
{
if (counter == 0)
{
x1 = e.X;
y1 = e.Y;
counter++;
}
else
{
counter = 1;
x2 = e.X;
y2 = e.Y;
Pen p = new Pen((Color)colors[comboBox1.SelectedIndex]);
g.DrawLine(p, x1, y1, x2, y2);
counter = 0;
}
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}