Sunday, September 25, 2011

Calculator Example

This is the calculator example.
The first is Form1.cs
=====================
//Author Cheikh Drame
/*
//Author: Cheikh Drame
//To support, please do one of the following
//- Visit irap123.com many times a day
//- Join irap123.com
//- Buy my app irap123 (rhyming dictionary) available for android, and iphone. On windows phone, it is called iLyrics. If you not you can get the desktop version at www.irap123.com/apps/webstore/
//- Include irap123.com in your blogs, forums, facebook walls, email footers, comments
//- send a donation at my paypal address at cbdsigned.vevo@gmail.com
//- Buy a story at http://shop.pparzygnat.com
*/
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 CalculatorSample1
{
public partial class Form1 : Form
{
private Button[] ops;
private char choice = ' ';
private long firstNumber = 0;
private bool fN = false;

public Form1()
{
InitializeComponent();
ops = new Button[] { addButton, minusButton, divButton, MulButton, ModButton};
}

private void highlightOperation(int i)
{
ops[i].BackColor = Color.Red;
for (int j = 0; j < ops.Length; ++j)
{
if (j != i) ops[j].BackColor = Color.White;
}
if (fN == false) { fN = true; firstNumber = getNumber(); textBox1.Text = ""; this.Text = "" + firstNumber + " "+choice+" "; }
else { textBox1.Text = ""; this.Text = "" + firstNumber + " " + choice + " "; }
textBox1.Select();
}

private void uncheckOps()
{
for (int j = 0; j < ops.Length; ++j)
{
ops[j].BackColor = Color.White;
}
}

private void appendText(String c)
{
if (textBox1.Text.Length < 10) { textBox1.AppendText(c); }
textBox1.Select();
}

private long getNumber()
{
long num = 0;
try
{
num = Convert.ToInt64(textBox1.Text);
}
catch (OverflowException)
{
num = 0;
}

return num;
}

private void calculate(long n2)
{
switch (choice)
{
case '+':
firstNumber += n2;
break;
case '-':
firstNumber -= n2;
break;
case '/':
firstNumber /= n2;
break;
case '*':
firstNumber *= n2;
break;
case '%':
firstNumber %= n2;
break;
default:
firstNumber = 0;
break;
}
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void addButton_Click(object sender, EventArgs e)
{
choice = '+';
highlightOperation(0);
}

private void minusButton_Click(object sender, EventArgs e)
{
choice = '-';
highlightOperation(1);
}

private void divButton_Click(object sender, EventArgs e)
{
choice = '/';
highlightOperation(2);
}

private void MulButton_Click(object sender, EventArgs e)
{
choice = '*';
highlightOperation(3);
}

private void ModButton_Click(object sender, EventArgs e)
{
choice = '%';
highlightOperation(4);
}

private void oneButton_Click(object sender, EventArgs e)
{
appendText("1");
}

private void TwoButton_Click(object sender, EventArgs e)
{
appendText("2");
}

private void ThreeButton_Click(object sender, EventArgs e)
{
appendText("3");
}

private void fourButton_Click(object sender, EventArgs e)
{
appendText("4");
}

private void fiveButton_Click(object sender, EventArgs e)
{
appendText("5");
}

private void sixButton_Click(object sender, EventArgs e)
{
appendText("6");
}

private void sevenButton_Click(object sender, EventArgs e)
{
appendText("7");
}

private void eightButton_Click(object sender, EventArgs e)
{
appendText("8");
}

private void nineButton_Click(object sender, EventArgs e)
{
appendText("9");
}

private void zeroButton_Click(object sender, EventArgs e)
{
appendText("0");
}

private void clearButton_Click(object sender, EventArgs e)
{
textBox1.Text = ""; choice = ' '; fN = false;
this.Text = "";
uncheckOps();
}

private void equalButton_Click(object sender, EventArgs e)
{
if (choice != ' ' && textBox1.Text.Length>0)
{
long num2 = getNumber();
calculate(num2);
this.Text += ""+num2+" = " + firstNumber;
textBox1.Text = "" + firstNumber;
}
}

private void Form1_DoubleClick(object sender, EventArgs e)
{

}

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{/*we limit the number of chars, and we only take digits*/
if ( (textBox1.Text.Length < 10 && Char.IsDigit((char)e.KeyValue)) || e.KeyData==Keys.Back) { }
else { e.SuppressKeyPress = true; }
}
}
}

======================
The second one is Form1.designer.cs
====================================
namespace CalculatorSample1
{
partial class Form1
{
///
/// Required designer variable.
///

private System.ComponentModel.IContainer components = null;

///
/// Clean up any resources being used.
///

/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.addButton = new System.Windows.Forms.Button();
this.minusButton = new System.Windows.Forms.Button();
this.divButton = new System.Windows.Forms.Button();
this.MulButton = new System.Windows.Forms.Button();
this.ModButton = new System.Windows.Forms.Button();
this.oneButton = new System.Windows.Forms.Button();
this.TwoButton = new System.Windows.Forms.Button();
this.ThreeButton = new System.Windows.Forms.Button();
this.fourButton = new System.Windows.Forms.Button();
this.fiveButton = new System.Windows.Forms.Button();
this.sixButton = new System.Windows.Forms.Button();
this.sevenButton = new System.Windows.Forms.Button();
this.eightButton = new System.Windows.Forms.Button();
this.nineButton = new System.Windows.Forms.Button();
this.zeroButton = new System.Windows.Forms.Button();
this.clearButton = new System.Windows.Forms.Button();
this.equalButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(1, 2);
this.textBox1.MaxLength = 10;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(339, 20);
this.textBox1.TabIndex = 0;
this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
//
// addButton
//
this.addButton.BackColor = System.Drawing.Color.White;
this.addButton.Location = new System.Drawing.Point(1, 29);
this.addButton.Name = "addButton";
this.addButton.Size = new System.Drawing.Size(33, 23);
this.addButton.TabIndex = 1;
this.addButton.Text = "+";
this.addButton.UseVisualStyleBackColor = false;
this.addButton.Click += new System.EventHandler(this.addButton_Click);
//
// minusButton
//
this.minusButton.BackColor = System.Drawing.Color.White;
this.minusButton.Location = new System.Drawing.Point(72, 29);
this.minusButton.Name = "minusButton";
this.minusButton.Size = new System.Drawing.Size(33, 23);
this.minusButton.TabIndex = 2;
this.minusButton.Text = "-";
this.minusButton.UseVisualStyleBackColor = false;
this.minusButton.Click += new System.EventHandler(this.minusButton_Click);
//
// divButton
//
this.divButton.BackColor = System.Drawing.Color.White;
this.divButton.Location = new System.Drawing.Point(149, 29);
this.divButton.Name = "divButton";
this.divButton.Size = new System.Drawing.Size(33, 23);
this.divButton.TabIndex = 3;
this.divButton.Text = "/";
this.divButton.UseVisualStyleBackColor = false;
this.divButton.Click += new System.EventHandler(this.divButton_Click);
//
// MulButton
//
this.MulButton.BackColor = System.Drawing.Color.White;
this.MulButton.Location = new System.Drawing.Point(224, 29);
this.MulButton.Name = "MulButton";
this.MulButton.Size = new System.Drawing.Size(33, 23);
this.MulButton.TabIndex = 4;
this.MulButton.Text = "*";
this.MulButton.UseVisualStyleBackColor = false;
this.MulButton.Click += new System.EventHandler(this.MulButton_Click);
//
// ModButton
//
this.ModButton.BackColor = System.Drawing.Color.White;
this.ModButton.Location = new System.Drawing.Point(295, 28);
this.ModButton.Name = "ModButton";
this.ModButton.Size = new System.Drawing.Size(33, 23);
this.ModButton.TabIndex = 5;
this.ModButton.Text = "%";
this.ModButton.UseVisualStyleBackColor = false;
this.ModButton.Click += new System.EventHandler(this.ModButton_Click);
//
// oneButton
//
this.oneButton.BackColor = System.Drawing.SystemColors.Highlight;
this.oneButton.Location = new System.Drawing.Point(40, 74);
this.oneButton.Name = "oneButton";
this.oneButton.Size = new System.Drawing.Size(33, 23);
this.oneButton.TabIndex = 6;
this.oneButton.Text = "1";
this.oneButton.UseVisualStyleBackColor = false;
this.oneButton.Click += new System.EventHandler(this.oneButton_Click);
//
// TwoButton
//
this.TwoButton.BackColor = System.Drawing.SystemColors.Highlight;
this.TwoButton.Location = new System.Drawing.Point(90, 74);
this.TwoButton.Name = "TwoButton";
this.TwoButton.Size = new System.Drawing.Size(33, 23);
this.TwoButton.TabIndex = 7;
this.TwoButton.Text = "2";
this.TwoButton.UseVisualStyleBackColor = false;
this.TwoButton.Click += new System.EventHandler(this.TwoButton_Click);
//
// ThreeButton
//
this.ThreeButton.BackColor = System.Drawing.SystemColors.Highlight;
this.ThreeButton.Location = new System.Drawing.Point(139, 74);
this.ThreeButton.Name = "ThreeButton";
this.ThreeButton.Size = new System.Drawing.Size(33, 23);
this.ThreeButton.TabIndex = 8;
this.ThreeButton.Text = "3";
this.ThreeButton.UseVisualStyleBackColor = false;
this.ThreeButton.Click += new System.EventHandler(this.ThreeButton_Click);
//
// fourButton
//
this.fourButton.BackColor = System.Drawing.SystemColors.Highlight;
this.fourButton.Location = new System.Drawing.Point(189, 74);
this.fourButton.Name = "fourButton";
this.fourButton.Size = new System.Drawing.Size(33, 23);
this.fourButton.TabIndex = 9;
this.fourButton.Text = "4";
this.fourButton.UseVisualStyleBackColor = false;
this.fourButton.Click += new System.EventHandler(this.fourButton_Click);
//
// fiveButton
//
this.fiveButton.BackColor = System.Drawing.SystemColors.Highlight;
this.fiveButton.Location = new System.Drawing.Point(238, 74);
this.fiveButton.Name = "fiveButton";
this.fiveButton.Size = new System.Drawing.Size(33, 23);
this.fiveButton.TabIndex = 10;
this.fiveButton.Text = "5";
this.fiveButton.UseVisualStyleBackColor = false;
this.fiveButton.Click += new System.EventHandler(this.fiveButton_Click);
//
// sixButton
//
this.sixButton.BackColor = System.Drawing.SystemColors.Highlight;
this.sixButton.Location = new System.Drawing.Point(40, 125);
this.sixButton.Name = "sixButton";
this.sixButton.Size = new System.Drawing.Size(33, 23);
this.sixButton.TabIndex = 11;
this.sixButton.Text = "6";
this.sixButton.UseVisualStyleBackColor = false;
this.sixButton.Click += new System.EventHandler(this.sixButton_Click);
//
// sevenButton
//
this.sevenButton.BackColor = System.Drawing.SystemColors.Highlight;
this.sevenButton.Location = new System.Drawing.Point(90, 125);
this.sevenButton.Name = "sevenButton";
this.sevenButton.Size = new System.Drawing.Size(33, 23);
this.sevenButton.TabIndex = 12;
this.sevenButton.Text = "7";
this.sevenButton.UseVisualStyleBackColor = false;
this.sevenButton.Click += new System.EventHandler(this.sevenButton_Click);
//
// eightButton
//
this.eightButton.BackColor = System.Drawing.SystemColors.Highlight;
this.eightButton.Location = new System.Drawing.Point(139, 125);
this.eightButton.Name = "eightButton";
this.eightButton.Size = new System.Drawing.Size(33, 23);
this.eightButton.TabIndex = 13;
this.eightButton.Text = "8";
this.eightButton.UseVisualStyleBackColor = false;
this.eightButton.Click += new System.EventHandler(this.eightButton_Click);
//
// nineButton
//
this.nineButton.BackColor = System.Drawing.SystemColors.Highlight;
this.nineButton.Location = new System.Drawing.Point(189, 125);
this.nineButton.Name = "nineButton";
this.nineButton.Size = new System.Drawing.Size(33, 23);
this.nineButton.TabIndex = 14;
this.nineButton.Text = "9";
this.nineButton.UseVisualStyleBackColor = false;
this.nineButton.Click += new System.EventHandler(this.nineButton_Click);
//
// zeroButton
//
this.zeroButton.BackColor = System.Drawing.SystemColors.Highlight;
this.zeroButton.Location = new System.Drawing.Point(238, 125);
this.zeroButton.Name = "zeroButton";
this.zeroButton.Size = new System.Drawing.Size(33, 23);
this.zeroButton.TabIndex = 15;
this.zeroButton.Text = "0";
this.zeroButton.UseVisualStyleBackColor = false;
this.zeroButton.Click += new System.EventHandler(this.zeroButton_Click);
//
// clearButton
//
this.clearButton.BackColor = System.Drawing.Color.Red;
this.clearButton.Location = new System.Drawing.Point(1, 189);
this.clearButton.Name = "clearButton";
this.clearButton.Size = new System.Drawing.Size(339, 23);
this.clearButton.TabIndex = 16;
this.clearButton.Text = "clear";
this.clearButton.UseVisualStyleBackColor = false;
this.clearButton.Click += new System.EventHandler(this.clearButton_Click);
//
// equalButton
//
this.equalButton.BackColor = System.Drawing.Color.LawnGreen;
this.equalButton.Location = new System.Drawing.Point(1, 218);
this.equalButton.Name = "equalButton";
this.equalButton.Size = new System.Drawing.Size(339, 23);
this.equalButton.TabIndex = 17;
this.equalButton.Text = "=";
this.equalButton.UseVisualStyleBackColor = false;
this.equalButton.Click += new System.EventHandler(this.equalButton_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(340, 246);
this.Controls.Add(this.equalButton);
this.Controls.Add(this.clearButton);
this.Controls.Add(this.zeroButton);
this.Controls.Add(this.nineButton);
this.Controls.Add(this.eightButton);
this.Controls.Add(this.sevenButton);
this.Controls.Add(this.sixButton);
this.Controls.Add(this.fiveButton);
this.Controls.Add(this.fourButton);
this.Controls.Add(this.ThreeButton);
this.Controls.Add(this.TwoButton);
this.Controls.Add(this.oneButton);
this.Controls.Add(this.ModButton);
this.Controls.Add(this.MulButton);
this.Controls.Add(this.divButton);
this.Controls.Add(this.minusButton);
this.Controls.Add(this.addButton);
this.Controls.Add(this.textBox1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Text = "Calculator Sample";
this.Load += new System.EventHandler(this.Form1_Load);
this.DoubleClick += new System.EventHandler(this.Form1_DoubleClick);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button addButton;
private System.Windows.Forms.Button minusButton;
private System.Windows.Forms.Button divButton;
private System.Windows.Forms.Button MulButton;
private System.Windows.Forms.Button ModButton;
private System.Windows.Forms.Button oneButton;
private System.Windows.Forms.Button TwoButton;
private System.Windows.Forms.Button ThreeButton;
private System.Windows.Forms.Button fourButton;
private System.Windows.Forms.Button fiveButton;
private System.Windows.Forms.Button sixButton;
private System.Windows.Forms.Button sevenButton;
private System.Windows.Forms.Button eightButton;
private System.Windows.Forms.Button nineButton;
private System.Windows.Forms.Button zeroButton;
private System.Windows.Forms.Button clearButton;
private System.Windows.Forms.Button equalButton;
}
}

=================
This is the youtube video



$40 off Craftsman 5 Drawer Homeowner Tool Center with Riser and Parts Bins $99.99 on sale 10/2-10/8
Sears Deal of the Day: Thule Car Top Carrier, Quest
Wholesale Fleece Blankets
400950_BuddhaGroove.com-Culture Infused Living: Home Accents, Jewelry, Accessories from around the world!
400948_Culture Infused Living: Home Accents, Jewelry, and accessories from around the world. CulturalElemen
504427_Save up to 75% on batteries and accessories at Brooklyn Battery Works!
504080_10% OFF Orders of $75 or More from Journelle.com! Use Promo Code: NOV11 (Valid 11/1 - 11/30/11)
502239_Brand names, bargain prices at shoezoo.com. If you don't love your new shoes, return them for free!

400949_GaneshMall.com-Culture Infused Living: Home Accents, Jewelry, and accessories from around the world!

Thursday, September 15, 2011

ScreenSaver Example

This is the ScreenSaver example partial code. To get the complete program working, you need to watch the youtube video.
There are many things you can change to practice. You can change the interval of the timer to make it faster. For instance: timer1.Interval=100; will make it move very fast. Also, instead of the images changes by clicking you can make it change after each position (top, center, bottom) and so on. The possibilities are endless.



================================
================================
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 ScreenSaver1
{
public partial class Form1 : Form
{
private Image[] imgs = { ScreenSaver1.Properties.Resources.Autumn_Leaves,
ScreenSaver1.Properties.Resources.Creek,
ScreenSaver1.Properties.Resources.Desert_Landscape,
ScreenSaver1.Properties.Resources.Dock,
ScreenSaver1.Properties.Resources.Forest};
private int next = 1;
private int position = 0;//0 is top left, 1 is center, 2 is bottom right

public Form1()
{
InitializeComponent();
}

private void timer1_Tick(object sender, EventArgs e)
{
switch (position)
{
case 0:
this.Location = new Point(1, 1);
break;
case 1:
this.CenterToScreen();
break;
case 2:
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, Screen.PrimaryScreen.Bounds.Height - this.Height);
break;
default:
break;
}
position = (position + 1) % 3;
}

private void Form1_Load(object sender, EventArgs e)
{
this.MinimumSize= this.MaximumSize = this.Size;
this.timer1.Interval = 2000;
this.timer1.Start();
this.Location = new Point(1, 1);
}

private void Form1_Click(object sender, EventArgs e)
{
this.BackgroundImage = this.imgs[++next % imgs.Length];
}
}
}


$40 off Craftsman 5 Drawer Homeowner Tool Center with Riser and Parts Bins $99.99 on sale 10/2-10/8
Sears Deal of the Day: Thule Car Top Carrier, Quest
Wholesale Fleece Blankets
400950_BuddhaGroove.com-Culture Infused Living: Home Accents, Jewelry, Accessories from around the world!
400948_Culture Infused Living: Home Accents, Jewelry, and accessories from around the world. CulturalElemen
504427_Save up to 75% on batteries and accessories at Brooklyn Battery Works!
504080_10% OFF Orders of $75 or More from Journelle.com! Use Promo Code: NOV11 (Valid 11/1 - 11/30/11)
502239_Brand names, bargain prices at shoezoo.com. If you don't love your new shoes, return them for free!

400949_GaneshMall.com-Culture Infused Living: Home Accents, Jewelry, and accessories from around the world!

Thursday, August 18, 2011

Introduction

This blog is to introduce you to the programming language C# through mainly examples.
The learning will be explained moderately directed mainly to the novitiate.
C# is by far the best programming language. All programming languages have strength and weaknesses. Even though Java and C++ are the most predominantly used languages, C# embody the best practices of both. It seems that every language tries to solve problems of earlier languages and then encounter new ones. So far, I have not seen any anomalies with C#, that doesn't mean there are none or will be none in the future.
Nevertheless C# is a great language to start learning, and the development software are easy to use. Writing applications with forms is 1000 times much easier than doing with C++.
You can follow my youtube videos by searching csharpmastery

$40 off Craftsman 5 Drawer Homeowner Tool Center with Riser and Parts Bins $99.99 on sale 10/2-10/8
Sears Deal of the Day: Thule Car Top Carrier, Quest
Wholesale Fleece Blankets
400950_BuddhaGroove.com-Culture Infused Living: Home Accents, Jewelry, Accessories from around the world!
400948_Culture Infused Living: Home Accents, Jewelry, and accessories from around the world. CulturalElemen
504427_Save up to 75% on batteries and accessories at Brooklyn Battery Works!
504080_10% OFF Orders of $75 or More from Journelle.com! Use Promo Code: NOV11 (Valid 11/1 - 11/30/11)
502239_Brand names, bargain prices at shoezoo.com. If you don't love your new shoes, return them for free!

400949_GaneshMall.com-Culture Infused Living: Home Accents, Jewelry, and accessories from around the world!