Wednesday, 20 October 2010

examples with source code to desingn an application with checkbox and adiobutton

//examples with source code to desingn an application with checkbox and adiobutton

public partial class Form6 : Form{
int count = 0;
public Form6()
{
InitializeComponent();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = sender as CheckBox;
int amt = int.Parse(txtAmount.Text);
if (cb.Checked)
{
count += 1;
amt += Convert.ToInt32(cb.Tag);
}
else
{
count -= 1;
amt -= Convert.ToInt32(cb.Tag);
}
txtAmount.Text = amt.ToString();
if (count == 0)
radioButton1.Checked = true;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
int amt = int.Parse(txtAmount.Text);
if (amt > 0)
{
RadioButton rb = sender as RadioButton;
if (rb.Checked)
amt += Convert.ToInt32(rb.Tag);
else
amt -= Convert.ToInt32(rb.Tag);
txtAmount.Text = amt.ToString();
}
else
radioButton1.Checked = true;
}

examples with source code of how to raise an event

//examples with source code of how to raise an event
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
MessageBox.Show("Event Raised");

examples with source-code to display where user has clicked

public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("Welcome to windows applications");
}
private void Form1_Click(object sender, EventArgs e)
{
MessageBox.Show("You have clicked on the form");
}
// example with source code to hidden literals
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack )
HiddenField1.Value = "0";


}
protected void Button2_Click(object sender, EventArgs e)
{

int count = Convert.ToInt32(HiddenField1.Value);
count++;

HiddenField1.Value = count.ToString();



Literal1.Text = "No. of clicks = " + HiddenField1.Value;
}

Sunday, 22 August 2010

source code or examples of how to use listboxes


//source code or examples of how to use listboxes

//try to develop same form
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 WindowsProject
{
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
}
private void Form7_Load(object sender, EventArgs e)
{
//source code or examples of how to use listboxes
listBox1.Items.Add("AP");
listBox1.Items.Add("Tamilnadu");
listBox1.Items.Add("Karnataka");
listBox1.Items.Add("Kerala");
listBox1.Items.Add("Orissa");
listBox1.Items.Add("Maharastra");
string[] cols = { "Red", "Blue", "Green", "White", "Black", "Yellow", "Pink" };
checkedListBox1.Items.AddRange(cols);
}
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(Convert.ToInt32(e.KeyChar) == 13)
if(comboBox1.FindString(comboBox1.Text) == -1)
comboBox1.Items.Add(comboBox1.Text);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(comboBox1.Text);
MessageBox.Show(comboBox1.SelectedIndex.ToString());
MessageBox.Show(comboBox1.SelectedItem.ToString());
}
private void button2_Click(object sender, EventArgs e)
{
foreach(object obj in listBox1.SelectedItems)
MessageBox.Show(obj.ToString());
}
private void button3_Click(object sender, EventArgs e)
{
string str = null;
foreach (object obj in checkedListBox1.CheckedItems)
str += obj.ToString() + ", ";
str = str.Substring(0, str.Length - 2);
int pos = str.LastIndexOf(",");
if (pos != -1)
{
str = str.Remove(pos, 1);
str = str.Insert(pos, " and");
}
MessageBox.Show(str);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}

how to use tags property in c#


//source code how to use tags property in c#
//try to devevlop the sme form it would be better
//it could also be used to develop cash system
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 WindowsProject
{
public partial class Form6 : Form
{
int count = 0;
public Form6()
{
InitializeComponent();
}
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
int amt = int.Parse(txtAmount.Text);
CheckBox cb = sender as CheckBox;
if (cb.Checked)
{
count += 1;
amt += Convert.ToInt32(cb.Tag);
}
else
{
count -= 1;
amt -= Convert.ToInt32(cb.Tag);
}
txtAmount.Text = amt.ToString();
if (count == 0)
radioButton1.Checked = true;
}
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
int amt = int.Parse(txtAmount.Text);
if (amt > 0)
{
RadioButton rb = sender as RadioButton;
if (rb.Checked)
amt += Convert.ToInt32(rb.Tag);
else
amt -= Convert.ToInt32(rb.Tag);
txtAmount.Text = amt.ToString();
}
else
radioButton1.Checked = true;
}
}
}

how to know which checkbox or radiobutton has clicked

//source codehow to know which checkbox or radiobutton has clicked
//try to crate the sme form as shon in fig.

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 WindowsProject
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked)
MessageBox.Show("Radio Button1 is selected");
else if (radioButton2.Checked)
MessageBox.Show("Radio Button2 is selected");
else if (radioButton3.Checked)
MessageBox.Show("Radio Button3 is selected");
}
private void button2_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
MessageBox.Show("Check Box1 is selected");
if (checkBox2.Checked)
MessageBox.Show("Check Box2 is selected");
if (checkBox3.Checked)
MessageBox.Show("Check Box3 is selected");
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
MessageBox.Show("Selected");
else
MessageBox.Show("De-Selected");
}
}
}

how to make validations in a textbox ,source code

how to make validations in a textbox ,source code
//try to make forms like this
//try to name as mentioned

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 WindowsProject
{
public partial class Form4 : Form
{
TextBox tb;
public Form4()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
//Code to save data into database
MessageBox.Show("Data saved");
}
private void btnClear_Click(object sender, EventArgs e)
{
// txtName.Clear(); txtPwd.Clear();
// txtCPwd.Text = txtAddress.Text = txtPhone.Text = "";
foreach (Control ctrl in this.Controls)
{
if (ctrl.GetType().Name == "TextBox")
{
TextBox tb = ctrl as TextBox;
tb.Clear();
}
}
txtName.Focus();
}
private void btnClose_Click(object sender, EventArgs e)
{
tb.CausesValidation = false;
this.Close();
}
private void txtName_Leave(object sender, EventArgs e)
{
tb = sender as TextBox;
}
private void txtName_Validating(object sender, CancelEventArgs e)
{
if (tb.Text.Trim().Length == 0)
{
MessageBox.Show("Cannot leave empty");
e.Cancel = true;
return;
}
if (tb.Name != "txtName")
{
if (tb.Text.Trim().Length < 6)
{
MessageBox.Show("Pwd should be randing between 6 to 12 chars");
e.Cancel = true; return;
}
}
if (tb.Name == "txtCPwd")
{
if (txtPwd.Text.Trim() != txtCPwd.Text.Trim())
{ MessageBox.Show("Confirm pwd should match with pwd");
e.Cancel = true; }
}
}
private void txtPhone_KeyPress(object sender, KeyPressEventArgs e)
{
//MessageBox.Show(Convert.ToInt32(e.KeyChar).ToString());
if (char.IsDigit(e.KeyChar) == false && Convert.ToInt32(e.KeyChar) != 8)
{
MessageBox.Show("Enter numerics only");
e.Handled = true;
}
}
}
}

to identify whic contoll is clicked


//to identify whic contoll is clicked
//create sush a form as shown in fig
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 WindowsProject
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void button_Click(object sender, EventArgs e)
{
//Button b = (Button)sender;
Button b = sender as Button;
if (b.Name == "button1")
MessageBox.Show("Button1 is clicked");
else if (b.Name == "button2")
MessageBox.Show("Button2 is clicked");
else if (b.Name == "button3")
MessageBox.Show("Button3 is clicked");
}
private void Form3_Load(object sender, EventArgs e)
{
}
}
}

how to bind with different controlls

// source code how to bind with different controlls
//go to properties and add to any event
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 WindowsProject
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
MessageBox.Show("Bound with multiple events of a control");
}
}
}

code for partial class

//code for partial class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OOPSProj
{
partial class Partia
{
public void Method1()
{
Console.WriteLine("Method One");
}
public void Method2()
{
Console.WriteLine("Method Two");
}
}
}

source code for different function with return

//source code for different function with return
using System;
namespace OOPSProj
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.Test11();
p.Test22(2);
Console.WriteLine(p.Test33());
Console.WriteLine(p.Test44("leejo"));
int b = 0;
int a = p.Test55(100, 50, ref b);
Console.WriteLine(a + " " + b);
Console.ReadLine();
}
//No Input No Output
void Test11()
{
Console.WriteLine("First Method");
}
//No Output Has Input
void Test22(int x)
{
Console.WriteLine("Second Method: " + x);
}
//No Input Has Output
string Test33()
{
return "Third Method";
}
//Has Input & Output
string Test44(string name)
{
return "Hello " + name;
}
//Has Input & Multiple Outputs
int Test55(int x, int y, ref int z)
{
z = x * y;
return x + y;
}
}
}

how to consume interface fom diff project

//how to consume interface fom diff project
using System;
using OOPSProj;
namespace SecondProject
{
class Class1
{
static void Main()
{
Rectangle r = new Rectangle(134, 56);
Console.WriteLine(r.GetArea());
Console.WriteLine("Second Project Second Class");
Console.ReadLine();
}
}
}

source code for using abstact class

//source code for using abstact class
using System;
namespace OOPSProj
{
//code for inheritance
public class Rectangle : Figure
{
//code constuctor
public Rectangle(double width, double height)
{
base.width = width;
base.height = height;
}
public override double GetArea()
{
return width * height;
}
static void Main()
{
Rectangle r = new Rectangle(190, 45);
Console.WriteLine(r.GetArea());
Console.ReadLine();
}
}
}

source code for this pointer

using System;
namespace OOPSProject
{
class Params
{
int x, y; // Class Variables
Params(int x, int y) //Block Variables
{
this.x = x;
this.y = y;
}
void Add()
{
Console.WriteLine(x + y);
}
void Sub()
{
Console.WriteLine(x - y);
}
void Mul()
{
Console.WriteLine(x * y);
}
void Div()
{
Console.WriteLine(x / y);
}
static void Main()
{
Params obj = new Params(100, 50);
obj.Add(); obj.Sub(); obj.Mul(); obj.Div();
Console.ReadLine();
}
}
}

to declare object

//source code to declare object
using System;
namespace OOPSProj
{
class First
{
int x = 100;
static void Main()
{
First f11 = new First();
First f22 = f11;
f11 = null;
Console.WriteLine(f22.x); //Valid
Console.WriteLine(f12.x); //InValid
Console.ReadLine();
}
}
}

using c# code to use abstract and inheritance

//using c# code to use abstract and inheritance
sing System;
namespace OOPSProj
{
//code for inheritance
class Circle : Figure
{
public Circle(double radius)
{
base.radius = radius;
}
public override double GetArea()
{
return 3.14 * radius * radius;
}
static void Main()
{
Circle c = new Circle(45);
Console.WriteLine(c.GetArea());
Console.ReadLine();
}
}
}

how to declare abstract class

// source code how to declare abstract class
using System;
namespace OOPSProj
{
public abstract class Fig
{
public double width, height, radius;
public abstract double GetArea();
}
}

how to declare abstract class

// source code how to declare abstract class
using System;
namespace OOPSProj
{
public abstract class Fig
{
public double width, height, radius;
public abstract double GetArea();
}
}

code for using abstract class and in heritance

//code for using abstract class and in heritance
using System;
namespace OOPSProj
{//source code for inheritance
class AbstractChild : AbstractParent
{
public override void Mul1(int x, int y)
{
Console.WriteLine(x * y);
}
public override void Div1(int x, int y)
{
Console.WriteLine(x / y);
}
public void show9()
{
Console.WriteLine("hello");
}
static void Main()
{
AbsChild obj = new AbsChild();
AbsParent p = obj;
p.Add(100, 50); p.Sub(350, 45);
p.Mul(245, 35); p.Div(180, 30);
Console.ReadLine();
}
}
}