Avance Zone

Technical Experts Zone Blogs

C sharp with Html Example

This program helps in understanding the c sharp with Html

<html>
<form action=http://127.0.0.1/cgi-bin/one.exe>
<input type=submit value=Search>
<form>
</html>
 
 
 
//C # File
class zzz
{
public static void Main()
{
System.Console.WriteLine("Content-Type:text/html\n");
System.Console.WriteLine("hi<b>bye");
}
}

c sharp Gui Form Example

This program helps in understanding the c sharp Gui Form

using System;
using System.Windows.Forms;
 
class myButton{
public Button name;
//constructor of myButton takes coordinates of the button and its integer name as arguments 
    public myButton(int x, int y,int k)
    {
        this.name=new System.Windows.Forms.Button(); //create an instance of Button
        name.Size=new System.Drawing.Size(25,25);//specify the button size
        name.Text=k.ToString();//name of the button
        name.Location=new System.Drawing.Point(x,y);//location of the button
    } //constructor of myButton takes coordinates of the button and its string name as arguments
    public myButton(int x,int y,string k)
    {
        this.name=new System.Windows.Forms.Button(); //create an instance of Button
        name.Size=new System.Drawing.Size(25,25);//specify the button size
        name.Text=k; //name of the button
        name.Location=new System.Drawing.Point(x,y);//location of the button
    }
}
class test:Form
{
    private System.Windows.Forms.TextBox text; //declare textbox
    private long x=0,temp; 
    private char op;
    public test()
    {//create buttons in different places of the form 
        myButton mbutton7=new myButton(25,25,7);
        myButton mbutton8=new myButton(50,25,8);
        myButton mbutton9=new myButton(75,25,9);
        myButton mbutton4=new myButton(25,50,4);
        myButton mbutton5=new myButton(50,50,5);
        myButton mbutton6=new myButton(75,50,6);
        myButton mbutton1=new myButton(25,75,1);
        myButton mbutton2=new myButton(50,75,2);
        myButton mbutton3=new myButton(75,75,3);
        myButton mbutton0=new myButton(100,75,0);
        myButton mbutton_plus=new myButton(25,100,"+");
        myButton mbutton_equal=new myButton(50,100,"=");
        myButton mbutton_clear=new myButton(75,100,"C");
        myButton mbutton_minus=new myButton(100,100,"-");
        myButton mbutton_multiply=new myButton(100,50,"*");
        myButton mbutton_divide=new myButton(100,25,"/");
        this.Text="Calculator."; //specify the name of the form
 
        this.MaximizeBox=false; //do not allow maximizing of the calculator
        this.BorderStyle=FormBorderStyle.FixedDialog; //do not allow resizing
        this.Size=new System.Drawing.Size(150,200); //vertical and
//horizontal size in pixels
//attach buttons to the calculator form one by one.
        this.Controls.Add(mbutton0.name);
        this.Controls.Add(mbutton1.name);
        this.Controls.Add(mbutton2.name);
        this.Controls.Add(mbutton3.name);
        this.Controls.Add(mbutton4.name);
        this.Controls.Add(mbutton5.name);
        this.Controls.Add(mbutton6.name);
        this.Controls.Add(mbutton7.name);
        this.Controls.Add(mbutton8.name);
        this.Controls.Add(mbutton9.name);
        this.Controls.Add(mbutton_plus.name);
        this.Controls.Add(mbutton_minus.name);
        this.Controls.Add(mbutton_equal.name);
        this.Controls.Add(mbutton_clear.name);
        this.Controls.Add(mbutton_minus.name);
        this.Controls.Add(mbutton_multiply.name);
        this.Controls.Add(mbutton_divide.name);
 
        this.text=new System.WinForms.TextBox(); //create TextBox text
        text.Location=new System.Drawing.Point(25,0); //specify location of the text
        this.Controls.Add(text); //Attach TextBox to the Form
        //handle button clicks 
        mbutton0.name.Click+=new EventHandler(this.button_method);
        mbutton1.name.Click+=new EventHandler(this.button_method);
        mbutton2.name.Click+=new EventHandler(this.button_method);
        mbutton3.name.Click+=new EventHandler(this.button_method);
        mbutton4.name.Click+=new EventHandler(this.button_method);
        mbutton5.name.Click+=new EventHandler(this.button_method);
        mbutton6.name.Click+=new EventHandler(this.button_method);
        mbutton7.name.Click+=new EventHandler(this.button_method);
        mbutton8.name.Click+=new EventHandler(this.button_method);
        mbutton9.name.Click+=new EventHandler(this.button_method);
        mbutton_plus.name.Click+=newSystem.EventHandler(this.button_methodoper);
        mbutton_equal.name.Click+=new System.EventHandler(this.button_methodoper);
        mbutton_clear.name.Click+=new System.EventHandler(this.button_methodoper);
        mbutton_minus.name.Click+=new     System.EventHandler(this.button_methodoper);
        mbutton_multiply.name.Click+=new System.EventHandler(this.button_methodoper);
        mbutton_divide.name.Click+=new System.EventHandler(this.button_methodoper); 
    }
    public void button_method(object sender, EventArgs e)
    {
        //find out which button was clicked
        string tem=sender.ToString();
        int k=tem[tem.Length-1]-48; //index is zero based convert integer
        //assuming ASCII incoding 
        temp=k+10*temp;
        text.Text=temp.ToString();
    }
 
    long oper(long x,long y,char sig) //perform an operation depending on the button
    {
    if(sig=='+')
        return x+y;
    else if(sig=='-')
        return x-y;
    else if(sig=='*')
        return x*y;
    else if(sig=='/')
        return x/y;
    else
        return 0;
    }
 
    public void button_methodoper(object sender, EventArgs e)
    {
        string tem=sender.ToString();
        char k=tem[tem.Length-1];//index is zero based convert integer
        //assuming ASCII incoding
        if(k=='=')
        {
            if(x==0)
            {
                    x=temp;
                    temp=0;
            }
            else
            {
                x=oper(x,temp,op);
                temp=0;
                text.Text=x.ToString();
               }
            } 
            else if (k=='C') //clear all stored variables
            {
                x=0;
                temp=0;
                text.Text=x.ToString();
            }
            else
            {
                if(x==0)
                 {
                    x=temp;
                    temp=0;
                    op=k;
                   }
                else
                {
                    if(temp!=0){
                    x=oper(x,temp,op);
                    temp=0;
                    text.Text=x.ToString();
                    }
                op=k;//store operation 
                } 
            }
    }
 
    public void form_method(object sender, EventArgs e) //method to be passed to form
//handler delegate it is activated every time a mouse is clicked on the form's surface.
    {}
    public static void Main()
    {
        test t=new test();
        t.ShowDialog();
    }
}

c sharp Menu With Database Connection

This program helps in understanding the c sharp Menu With Database Connection

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
 
 
    public class Form1 : System.Windows.Forms.Form
    {
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox textBox6;
		private System.Windows.Forms.TextBox textBox5;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.ComboBox comboBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label1;
       private OleDbConnection con;
       private OleDbCommand cmd1;
 
        public Form1()
        {
        try
        {
//        con=new ADOConnection("Data Source=conn");
        con=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\gk.mdb");
        con.Open();
        }
		catch(Exception e)
		{
		MessageBox.Show("Exception........"+e);
		}
        init();
        }
 
       /* public override void Dispose()
        {
        base.Dispose();
        }*/
 
        private void init()
        {
this.textBox1 = new System.Windows.Forms.TextBox ();
this.textBox5 = new System.Windows.Forms.TextBox ();
this.button3 = new System.Windows.Forms.Button ();
this.button2 = new System.Windows.Forms.Button ();
this.label1 = new System.Windows.Forms.Label ();
this.button1 = new System.Windows.Forms.Button ();
this.textBox3 = new System.Windows.Forms.TextBox ();
this.textBox2 = new System.Windows.Forms.TextBox ();
this.textBox4 = new System.Windows.Forms.TextBox ();
this.comboBox1 = new System.Windows.Forms.ComboBox ();
this.textBox6 = new System.Windows.Forms.TextBox ();
 
textBox1.Location = new System.Drawing.Point (224, 104);
textBox1.Text = "";
textBox1.TabIndex = 1;
textBox1.Size = new System.Drawing.Size (144, 20);
textBox5.Location = new System.Drawing.Point (304, 272);
textBox5.Text = "";
textBox5.TabIndex = 6;
textBox5.Size = new System.Drawing.Size (80, 20);
button3.Location = new System.Drawing.Point (352, 328);
button3.Size = new System.Drawing.Size (88, 24);
button3.TabIndex = 10;
button3.Text = "Exit";
button2.Location = new System.Drawing.Point (224, 328);
button2.Size = new System.Drawing.Size (104, 24);
button2.TabIndex = 9;
button2.Text = "clear";
label1.Location = new System.Drawing.Point (155, 40);
label1.Text = "DataBase Transactions through Ado.Net";
label1.Size = new System.Drawing.Size (500, 70);
label1.Font = new System.Drawing.Font ("Impact", 18);
label1.TabIndex = 0;
button1.Location = new System.Drawing.Point (96, 328);
button1.Size = new System.Drawing.Size (88, 24);
button1.TabIndex = 8;
button1.Text = "Save";
 
//hooking the Events to Elements
 
button1.Click += new System.EventHandler(this.button1_Click);
button3.Click += new System.EventHandler(this.b3_click);
button2.Click += new System.EventHandler(this.b2_click);
textBox3.Location = new System.Drawing.Point (112, 272);
textBox3.Text = "";
textBox3.TabIndex = 4;
textBox3.Size = new System.Drawing.Size (64, 20);	
textBox2.Location = new System.Drawing.Point (224, 144);
textBox2.Text = "";
textBox2.TabIndex = 2;
textBox2.Size = new System.Drawing.Size (144, 20);
textBox4.Location = new System.Drawing.Point (208, 272);
textBox4.Text = "";
textBox4.TabIndex = 5;
textBox4.Size = new System.Drawing.Size (64, 20);
comboBox1.Text = "";
comboBox1.Location = new System.Drawing.Point (224, 184);
comboBox1.Size = new System.Drawing.Size (144, 21);
comboBox1.TabIndex = 3;
textBox6.Location = new System.Drawing.Point (408, 272);
textBox6.Text = "";
textBox6.TabIndex = 7;
textBox6.Size = new System.Drawing.Size (64, 20);
this.Text = "Ado.Net";                                   			
this.ClientSize = new System.Drawing.Size (600, 450);
this.Controls.Add (this.button3);
this.Controls.Add (this.button2);
this.Controls.Add (this.button1);
this.Controls.Add (this.textBox6);
this.Controls.Add (this.textBox5);
this.Controls.Add (this.textBox4);
this.Controls.Add (this.textBox3);
this.Controls.Add (this.comboBox1);
this.Controls.Add (this.textBox2);
this.Controls.Add (this.textBox1);
this.Controls.Add (this.label1);
}
 
protected void button1_Click (object sender, System.EventArgs e)
{
int id=Int32.Parse(textBox1.Text);
string name=textBox2.Text;
string branch=comboBox1.Text;
int m1=Int32.Parse(textBox3.Text);
int m2=Int32.Parse(textBox4.Text);
int m3=Int32.Parse(textBox5.Text);
int m4=Int32.Parse(textBox6.Text);
int total=m1+m2+m3+m4;
string result;
if(m1>=50&&m2>=50&&m3>=50&&m4>=50)
{
result="cleared";
} 
else
{
result="not cleared";
}
 
        string query="insert into exam values("+id+",'"+name+"','"+branch+"','"+total+"','"+result+"')";
        cmd1=new OleDbCommand(query,con);
        cmd1.ExecuteNonQuery();        
        MessageBox.Show("Record Updated Successfully");
        textBox1.Text="";
        textBox2.Text="";
        textBox3.Text="";
        textBox4.Text="";
        textBox5.Text="";
        textBox6.Text="";
        comboBox1.Text="";
        }
 
        protected void b3_click(object sender, System.EventArgs e)
        {
        this.Close();
        }
 
 
        protected void b2_click(object sender,System.EventArgs e)
        {
        MessageBox.Show("click Me to Clear the Elements");
        textBox1.Text="";
        textBox2.Text="";
        textBox3.Text="";
        textBox4.Text="";
        textBox5.Text="";
        textBox6.Text="";
        comboBox1.Text="";
 
        }
 
 
 
        public static void Main(string[] args)
        {
        Form1 f=new Form1();
        Application.Run(f);
        }
        }

c sharp Adding a Menu Example

This program helps in understanding the c sharp Adding a Menu

using System;
using System.Windows.Forms;
 
public class formmenu1:Form
{
static void Main()
{
formmenu1 m=new formmenu1();
Application.Run(m);
}
 
 
public formmenu1()
{
this.Text="A Menu Program";
MainMenu mainmenu1=new MainMenu();
MenuItem menuitem1=new MenuItem();
MenuItem menuitem2=new MenuItem();
MenuItem subm1=new MenuItem("Open");
MenuItem subm2=new MenuItem("Save");
MenuItem subm3=new MenuItem("Exit",new System.EventHandler(Exit_click));
 
 
MenuItem s1sub1=new MenuItem("Txt",new System.EventHandler(mnu_click));
MenuItem s1sub2=new MenuItem("Doc");
 
menuitem1.Text="&file";
menuitem2.Text="&Edit";
 
//Add Two Menuitem objects to the main menu
 
mainmenu1.MenuItems.Add(menuitem1);
mainmenu1.MenuItems.Add(menuitem2);
 
menuitem1.MenuItems.Add(subm1);
menuitem1.MenuItems.Add(subm2);
menuitem1.MenuItems.Add(subm3);
 
subm1.MenuItems.Add(s1sub1);
subm1.MenuItems.Add(s1sub2);
 
 
//Bind the Mainmenu for form
 
this.Menu=mainmenu1;
}
 
private void mnu_click(object sender,System.EventArgs e)
{
MessageBox.Show("Hai");
}
 
 
private void Exit_click(object sender,System.EventArgs e)
{
this.Close();
}
}

Writing unsafe code – pointers in C#

This program helps in understanding the c sharp Writing unsafe code – pointers

using System;
class zzz
{
public static void Main() 
{
yyy a = new yyy();
a.abc();
}
}
 
struct aaa
{
public int i,j;
}
 
class yyy
{
unsafe public void abc()
{
aaa *a;
aaa b;
b = new aaa();
a = &b;
int *x;
x = &(a->i);
int *y;
y = &(a->j);
Console.WriteLine((int) a + " " + (int)x + " " + (int) y);
}
}

Writing unsafe code – pointers in C#

This program helps in understanding the c sharp Writing unsafe code – pointers

using System;
class zzz
{
public static void Main() 
{
yyy a = new yyy();
a.abc();
}
}
 
class yyy
{
unsafe public void abc()
{
int  *i;
int j=1, 
k = 1;
i = &j; //passing j's address to i
Console.WriteLine((int)i);
*i = 10; //now j=10
Console.WriteLine(j);
i = &k; //passing address of k to i
Console.WriteLine((int)i);
*i = 100;//now k=100
Console.WriteLine(k + " " + j);
}
}

Writing unsafe code – pointers in C#

This program helps in understanding the Writing unsafe code – pointers in C#

 
using System;
class zzz
{
public static void Main() 
{
yyy a = new yyy();
a.abc();
}
}
 
public class yyy
{
 
unsafe public void pqr( int *a)
{
Console.WriteLine("{0} {1} {2}",a[0],a[1],a[2]);
}
 
unsafe public void abc()
{
int [] a = new int[3];
a[0] = 10; 
a[1] = 2; 
a[2] = 30;
pqr(a);
}
 
}

c sharp Thread Example

This program helps in understanding the c sharp Thread

using System;
using System.Threading;
public class thread3
{
       public static void Main()
        {
                Thread t=Thread.CurrentThread;
                t.Name="Karthik";
		System.Console.WriteLine("Current thread : "+t);
                System.Console.WriteLine("Name of the current Thread : "+t.Name.ToString());
                System.Console.WriteLine("Priority : "+t.Priority.ToString());
                t.Priority=ThreadPriority.AboveNormal;
                System.Console.WriteLine("After Priority Change : "+t.Priority.ToString());
             	System.Console.WriteLine("Thread Alive : "+t.IsAlive);
		System.Console.WriteLine("Thread State : "+t.ThreadState);
 
 
        }
}

c sharp Thread Example

This program helps in understanding the c sharp Thread

using System;
using System.Threading;
 
public class threads
{
static void Main()
{
for(int i=1;i<=10;i++)
{
Console.WriteLine("I am "+i);
Thread.Sleep(1000);
}
}
}

C sharp Thread Example

This program helps in understanding the c sharp Thread

using System;
using System.Threading;
public class threaddemo
{
 threaddemo()
{
Thread thread=new Thread(new ThreadStart(Writedata));
thread.Start();
}
protected static void Writedata()
{
string str;
for (int i=1;i<=10;i++)
{
str="Secondary Thread"+i.ToString();
Console.WriteLine(str);
Thread.Sleep(1000);
}
}

public static void Main()
{
threaddemo td=new threaddemo();
for(int j=1;j<=10;j++)
{
Console.WriteLine(“Main Thread “+j.ToString());
Thread.Sleep(1000);
}
}
}