Thursday, August 27, 2009

Windows Programming - Working with MouseMove

using System;
using System.Windows.Forms;
using System.Drawing;
class myfrm : Form
{
public myfrm()
{
this.MouseMove +=new MouseEventHandler(fun);
}
void fun(object o, MouseEventArgs e)
{
Graphics g = this.CreateGraphics();
g.DrawEllipse(Pens.Red, e.X - 50, e.Y - 50, 100, 100);
}
}
class Program
{
static void Main(string[] args)
{
myfrm f = new myfrm();
f.ShowDialog();
}
}

Windows Programming - Working with ContextMenu OR right click menu

using System;
using System.Windows.Forms;
using System.Drawing;
class myfrm : Form
{
ContextMenu cm;
MenuItem m1, m2, m3;
public myfrm()
{
cm = new ContextMenu();
m1 = new MenuItem("aaa");
m2 = new MenuItem("bbb");
m3 = new MenuItem("ccc");

cm.MenuItems.Add(m1);
cm.MenuItems.Add(m2);
cm.MenuItems.Add(m3);
this.ContextMenu = cm;

m1.Click +=new EventHandler(fun);
}
void fun(object o, EventArgs e)
{
MessageBox.Show("m1 clicked");

}

}
class Program
{
static void Main(string[] args)
{
myfrm f = new myfrm();
f.ShowDialog();
}
}

Windows Programming - Working wih Menu

using System;
using System.Windows.Forms;
using System.Drawing;
class myfrm : Form
{
MainMenu m;
MenuItem file, edit;
MenuItem exit, close;
MenuItem cut, copy, paste;
public myfrm()
{
m = new MainMenu();
file = new MenuItem("File");
edit = new MenuItem("Edit");

exit = new MenuItem("Exit");
close = new MenuItem("Close");

cut = new MenuItem("cut");
copy = new MenuItem("copy");
paste = new MenuItem("paste");

file.MenuItems.Add(exit);
file.MenuItems.Add(close);

edit.MenuItems.Add(cut);
edit.MenuItems.Add(copy);
edit.MenuItems.Add(paste);

m.MenuItems.Add(file);
m.MenuItems.Add(edit);

this.Menu = m;
cut.Click +=new EventHandler(fun);
}
void fun(object o, EventArgs e)
{
MessageBox.Show("cut clicked");
}

}
class Program
{
static void Main(string[] args)
{
myfrm f = new myfrm();
f.ShowDialog();
}
}

Windows Programming - Working with ComboBox

using System;
using System.Windows.Forms;
using System.Drawing;
class myfrm : Form
{
ListBox c;
Button b;
public myfrm()
{
c = new ListBox();
c.SetBounds(100, 100, 100, 50);
c.Items.Add("aaa");
c.Items.Add("bbb");
c.Items.Add("ccc");

c.SelectionMode = SelectionMode.MultiSimple;

/* other Selection Modes
SelectionMode.MultiExtended
SelectionMode.MultiSimple
SelectionMode.None
SelectionMode.One
*/
this.Controls.Add(c);

b = new Button();
b.Text = "Ok";
b.SetBounds(100, 300, 100, 50);
this.Controls.Add(b);
b.Click +=new EventHandler(fun);

}
void fun(object o, EventArgs e)
{
//MessageBox.Show(c.Text);
//c.Items.Clear();
//c.Items.Count

//c.Items.Remove("aaa");
//c.Items.RemoveAt(0);

foreach (string x in c.SelectedItems)
{
MessageBox.Show(x);
}
}

}
class Program
{
static void Main(string[] args)
{
myfrm f = new myfrm();
f.ShowDialog();
}
}

Windows Programming - Example for Tab Index

using System;
using System.Windows.Forms;
using System.Drawing;
class myfrm : Form
{
Button b1, b2, b3;
public myfrm()
{
b1 = new Button();
b1.Text = "Ok";
b1.TabIndex = 1;
b1.SetBounds(100, 100, 100, 50);
this.Controls.Add(b1);

b2 = new Button();
b2.Text = "Cancel";
b2.TabIndex = 0;
b2.SetBounds(100, 200, 100, 50);
this.Controls.Add(b2);

b3 = new Button();
b3.Text = "Abort";
b3.SetBounds(100, 300, 100, 50);
b3.TabIndex = 2;
this.Controls.Add(b3);
}
}
class Program
{
static void Main(string[] args)
{
myfrm f = new myfrm();
f.ShowDialog();
}
}

Windows Programming - Working with RadioButton

using System;
using System.Windows.Forms;
using System.Drawing;
class myfrm : Form
{
RadioButton b1, b2;
Button b;
public myfrm()
{

b1 = new RadioButton();
b1.Text = "Male";
b1.SetBounds(100, 100, 150, 50);
this.Controls.Add(b1);
b2 = new RadioButton();
b2.Text = "Female";
b2.SetBounds(100, 200, 150, 50);
this.Controls.Add(b2);
b = new Button();
b.Text = "Ok";
b.SetBounds(100, 300, 150, 50);
this.Controls.Add(b);
b.Click +=new EventHandler(fun);
}
void fun(object o, EventArgs e)
{
if (b1.Checked == true)
{
MessageBox.Show("Male Selected");
}
if (b2.Checked == true)
{
MessageBox.Show("Female Selected");
}
}
}
class Program
{
static void Main(string[] args)
{
myfrm f = new myfrm();
f.ShowDialog();
}
}

Windows Programming - Working with CheckBox

using System;
using System.Windows.Forms;
using System.Drawing;
class myfrm : Form
{
CheckBox c;
Button b;
public myfrm()
{
c=new CheckBox();
c.Text="Subject";
c.SetBounds(100,100,150,50);
this.Controls.Add(c);

b = new Button();
b.Text = "Ok";
b.SetBounds(100, 300, 150, 50);
this.Controls.Add(b);
b.Click +=new EventHandler(fun);
}
void fun(object o, EventArgs e)
{
if (c.CheckState == CheckState.Checked)
{
MessageBox.Show("subject selected");
}
if (c.CheckState == CheckState.Unchecked)
{
MessageBox.Show("subject not selected");
}
}
}
class Program
{
static void Main(string[] args)
{
myfrm f = new myfrm();
f.ShowDialog();
}
}

Windows Programming - Simple Addition or Subtraction of Two Numbers

using System;
using System.Windows.Forms;
using System.Drawing;
class myfrm : Form
{
Button b1, b2;
public myfrm()
{

t1 = new TextBox();
t1.SetBounds(100, 100, 150, 50);
t2 = new TextBox();
t2.SetBounds(100, 200, 150, 50);
this.Controls.Add(t2);
t3 = new TextBox();
t3.SetBounds(100, 300, 150, 50);


b1 = new Button();
b1.Text = "Add";
b1.SetBounds(100, 100, 150, 50);

b2 = new Button();
b2.Text = "Sub";
b2.SetBounds(100, 300, 150, 50);

this.Controls.Add(t1);
this.Controls.Add(t2);
this.Controls.Add(t3);
this.Controls.Add(b1);
this.Controls.Add(b2);

b1.Click +=new EventHandler(fun);
b2.Click +=new EventHandler(fun);
}

void fun(object o, EventArgs e)
{
double a,b,c;
a = Convert.ToDouble(t1.Text);
b = Convert.ToDouble(t2.Text);


if (o.Equals(b1))
{
c = a + b;
}
if (o.Equals(b2))
{
c = a - b;
}

t3.Text = c.ToString();

}

}

Windows Programming - Working with Message Box

using System;
using System.Windows.Forms;
using System.Drawing;
class myfrm : Form
{
Button b;
public myfrm()
{
b = new Button();
b.SetBounds(100, 100, 150, 50);
b.Text = "Ok";
this.Controls.Add(b);
b.Click +=new EventHandler(fun);
}


void fun(object o,EventArgs e)
{
DialogResult y=MessageBox.Show("text", "caption", MessageBoxButtons.AbortRetryIgnore,MessageBoxIcon.Asterisk);
/* The other message box button styles
MessageBoxButtons.OK
MessageBoxButtons.OKCancel
MessageBoxButtons.RetryCancel
MessageBoxButtons.YesNo
MessageBoxButtons.YesNoCancel
*/
/* The other message Box Button Icons
MessageBoxIcon.Asterisk
MessageBoxIcon.Error
MessageBoxIcon.Exclamation
MessageBoxIcon.Hand
MessageBoxIcon.Information
MessageBoxIcon.None
MessageBoxIcon.Question
MessageBoxIcon.Stop
MessageBoxIcon.Warning
*
*/
if (y == DialogResult.Abort)
{
MessageBox.Show("Abort Clicked");
}
}
}
class Program
{
static void Main(string[] args)
{
myfrm f = new myfrm();
f.WindowState = FormWindowState.Maximized;
/* Window State Styles
FormWindowState.Maximized
FormWindowState.Minimized
FormWindowState.Normal
*/
f.ShowDialog();
}
}

Windows Programming - Add Message Box

using System;
using System.Windows.Forms;
using System.Drawing;
class myfrm : Form
{
Button b;
public myfrm()
{
b = new Button();
b.SetBounds(100, 100, 150, 50);
b.Text = "Ok";
this.Controls.Add(b);
b.Click +=new EventHandler(fun);
}
void fun(object o,EventArgs e)
{
MessageBox.Show("text", "caption");
}
}
class Program
{
static void Main(string[] args)
{
myfrm f = new myfrm();
f.ShowDialog();
}
}

Windows Programming - Add Buttons Click Event

using System;
using System.Windows.Forms;
using System.Drawing;
class myfrm : Form
{
Button b;
public myfrm()
{
b = new Button();
b.SetBounds(100, 100, 150, 50);
b.Text = "Ok";
this.Controls.Add(b);
b.Click +=new EventHandler(fun);
}
void fun(object o,EventArgs e)
{
MessageBox.Show("aaa");
}
}
class Program
{
static void Main(string[] args)
{
myfrm f = new myfrm();
f.ShowDialog();
}
}

Windows Programming - Add font property to Button

using System;
using System.Windows.Forms;
using System.Drawing;
class myfrm : Form
{
Button b;
public myfrm()
{
b = new Button();
b.SetBounds(100, 100, 150, 50);
b.Text = "Ok";
Font x = new Font("Arial", 20, FontStyle.Bold);
b.Font = x;
this.Controls.Add(b);
}
}
class Program
{
static void Main(string[] args)
{
myfrm f = new myfrm();
f.ShowDialog();
}
}

Windows Programming - Add Buttons

using System;
using System.Windows.Forms;
using System.Drawing;
class myfrm : Form
{
Button b;
public myfrm()
{
b = new Button();
b.SetBounds(100, 100, 150, 50);
b.Text = "Ok";
this.Controls.Add(b);
}
}

class Program
{
static void Main(string[] args)
{
myfrm f = new myfrm();
f.ShowDialog();
}
}

Windows Programming - change back color 2

using System;
using System.Windows.Forms;
using System.Drawing;
class myfrm : Form
{
}
class Program
{
static void Main(string[] args)
{
myfrm f = new myfrm();
f.BackColor = Color.FromArgb(255, 0, 0);
f.ShowDialog();
}
}

Windows Programming - change back color 1

using System;
using System.Windows.Forms;
using System.Drawing;
class myfrm : Form
{
}
class Program
{
static void Main(string[] args)
{
myfrm f = new myfrm();
f.BackColor = Color.Blue;
f.ShowDialog();
}
}

Windows Programming - Position form

using System;
using System.Windows.Forms;
using System.Drawing;
class myfrm : Form
{
}
class Program
{
static void Main(string[] args)
{
myfrm f = new myfrm();
f.SetBounds(100, 100, 500, 500);
f.ShowDialog();
}
}

Windows Programming - Set form Location

using System;
using System.Windows.Forms;
using System.Drawing;
class myfrm : Form
{
}
class Program
{
static void Main(string[] args)
{
myfrm f = new myfrm();
Size s = new Size(100, 100);
f.Size = s;

Point p = new Point();
p.X = 500;
p.Y = 500;
f.Location = p;
f.ShowDialog();
}
}

Windows Programming - Create Form

using System;

using System.Windows.Forms;

using System.Drawing;

class myfrm : Form

{

}

class Program

{

static void Main(string[] args)

{

myfrm f = new myfrm();

f.ShowDialog();

}

}

Tuesday, August 25, 2009

Program to find HCF and LCM



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static int hcf(int min, int max)
{
int rem;
if (min < max)
{
int temp = min;
min = max;
max = temp;
}
while (true)
{
rem = max % min;
if (rem == 0)
return min;
else
{
max = min;
min = rem;
}
}
}
static int lcm(int one, int two)
{
int lcm = one * two / hcf(one, two);
return lcm;
}

static void Main(string[] args)
{
int element, value, choice;
int[] array;

do
{
Console.WriteLine("1.HCF\n2.LCM\n3.exit\nchoice:");
choice = Convert.ToInt16(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine("No. of elements:");
element = Convert.ToInt32(Console.ReadLine());
array = new int[element];
Console.WriteLine("Elements:\n");
for (int i = 0; i < element; i++)
{
array[i] = Convert.ToInt32(Console.ReadLine());
}
value = array[0];
for (int i = 1; i < element; i++)
{
value = hcf(value , array[i]);
}
Console.WriteLine("HCF:" + value);
break;
case 2:
Console.WriteLine("No. of elements:");
element = Convert.ToInt32(Console.ReadLine());
array = new int[element];
Console.WriteLine("Elements:\n");
for (int i = 0; i < element; i++)
{
array[i] = Convert.ToInt32(Console.ReadLine());
}
value = array[0];
for (int i = 1; i < element; i++)
{
value = lcm(value, array[i]);
}
Console.WriteLine("LCM:" + value);
break;
case 3:
break;
default:
Console.WriteLine("\nwrong choice");
break;

}
} while (choice != 3) ;
}
}
}

Highest Prime Number



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
public class PrimeNumber {

/*
* Given a number, finds the highest prime number.
*/
public static int highestPrime(int n) {
int value = 1;

for (int i = 2; i <= n; i++)
{
System.Console.WriteLine("n: " + n + " i: " + i + " mod: " + n % i);
if (i == n) { value = n; break;
}
if (n % i == 0)
{
n--;
}
}
return value;
}
static void Main(string[] args)
{
int value = highestPrime(100);
System.Console.WriteLine("Prime:" + value.ToString());
System.Console.Read();
}
}
}
}

Thursday, August 20, 2009

Find prime Numbers between numbers

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int num ,count;
bool stat=false;
Console.WriteLine("Enter the number");
num = Convert.ToInt32(Console.ReadLine());

for (int i = 1; i <= num; i++)
{
count = 0;
for (int j = 1; j <= i; j++)
{
if ((i % j) == 0)
stat = true;

if (stat)
{
count++;
stat = false;
}
}
if(count==2)
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}