Popular Posts

Monday, February 21, 2011

Good feature of C#:Partial classes

Partial classes give you the ability to split a class into more than one source code .cs file.
If a class becomes particularly long and intricate you might decide to break it into pieces as shown below


//This file is stored in the Product.cs

public partial class Product
{
private string name;
private decimal price1;
public string Name
{

}

public decimal Price
{

}

public string ImageUrl
{

}

}

public partial class Product
{
//continued code here
}



these two  partial classes comprise one class Product and are divided to simplify coding.

Handling events in VB.NET C#


Copy this code as it is to the code part of your form.
Debug the code step by step to understand how events are called.
Delegates are used for calling events to implement event based system elegantly.


How to debug:
Click on the left side pane of your coding window to add a debug point.
press Ctrl+F11 to debug  the code step wise.


CODE


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace trialprograms3
{
    public partial class Form1 : Form
    {
    
      

//declaring the event handler delegate
delegate void ButtonEventHandler(object source,int clickCount);

class Button
{
//declaring the event
public event ButtonEventHandler ButtonClick;

//Function to trigger the event
public void clicked(int count)
{
MessageBox.Show("\nInside Clicked !!!");
//Invoking all the event handlers
if (ButtonClick != null)
    ButtonClick(this,count);      //the parameters sent to the event should match those of the above declared delegate
                                 //delegate is like a function pointer
                                //onButtonAction event function is called from here.
}
}
public class Dialog
{
public Dialog()
{
Button b = new Button();

//Adding an event handler
b. ButtonClick += new ButtonEventHandler(onButtonAction);
//Triggering the event
b.clicked(1);   //public void click is called here  and a count of 1 is passes as parameter

b.ButtonClick += new ButtonEventHandler(onButtonAction);
b.clicked(1);

//Removing an event handler
b.ButtonClick -= new ButtonEventHandler(onButtonAction);
b.clicked(1);

b.ButtonClick -= new ButtonEventHandler(onButtonAction);
b.clicked(1);
}






//Event Handler function
public void onButtonAction(object source,int clickCount)
{
MessageBox.Show("Inside Event Handler !!!");
}
}
        public Form1()
        {
            InitializeComponent();
            new Dialog();
        }
    }
}

Sunday, February 20, 2011

Code Snippet to paint a panel border with a specific color.

e.Graphics.DrawRectangle(Pens.Green, 0, 0, ((Panel)sender).Width - 1, ((Panel)sender).Height - 1); base.OnPaint(e);

Saturday, February 19, 2011

Difference between Client Side and Server side programming

1.
Client Side Scripting
In client side scripting the client requests a web page and the server returns an HTML document and an embedded applet and the application is run on the  client side.

Server Side Scripting
In server side scripting the client requests a web page an a server side application is run on the server and the server returns an HTML document to the client.

2.
Client Side Scripting
In client side scripting client side code can't access server side resources.

Server side scripting
Client side code can access server side resources.

SERVER SIDE WEB APPLICATION

CLIENT SIDE WEB APPLICATION

Wednesday, February 16, 2011

Make Life Easy: To display a form as a MDI child

Make Life Easy: To display a form as a MDI child: "HERE :FormName is the name of the Form formobject is the object of the form CODE SNIPPET: &..."

To display a form as a MDI child


HERE :FormName is the name of the Form
            formobject is the object of the form

CODE SNIPPET:
            FormName formobject = new FormName();
             MasterPage MDI = (MasterPage)this.ParentForm;
             formobject.MdiParent = this.ParentForm;
             formobject.Location = new Point(0, 0);
             formobject.StartPosition = FormStartPosition.Manual;
             formobject.Show();
             formobject.TopLevel = false;
             formobject.Parent = MDI.panel1;
             formobject.Dock = DockStyle.Fill;
             formobject.AutoScroll = true;
             this.Close();

Tuesday, February 15, 2011

Make Life Easy: code to View any file through vb.net

Make Life Easy: code to View any file through vb.net: "//path is the full path of the file System.Diagnostics.Process.Start(path); 1.U may also start .exe files with this line of code."

Sunday, February 13, 2011

Delegates Made Easy


using ....   //include all the libraries you want to include

namespace trialprograms
{
    public partial class Form1 : Form
    { 
        //declare a delegate
        public delegate void Del(string message); 
        //Del is the name of the delegate
        //x,y are the arguments to the delegate
        //int is the return type of the delegate

        private void DelegateMethod(string message)
        {

            MessageBox.Show(message);
        }

        public void func_checkdelegate(int x,int y,Del callback)
        {
            //invoke the delegate and send the parameters
            callback("The sum of the two numbers is "+ (x + y).ToString());
        }

public Form1()
        {
            InitializeComponent();
            //instantiate the delegate
            //Here it points to the function Delegate Method
            Del handler = DelegateMethod;  //the return type the signature of the delegate and the function it       points to must match
            //sending the  object of the delegate with the function
            func_checkdelegate(2,3,handler);
            
           }
 }

Friday, February 11, 2011

C# VB.NET


To obtain the path of where the vb.net windows form is loaded:
            string path = System.Windows.Forms.Application.StartupPath;
            path = path.Replace(@"\bin\Debug", "");