Friday, 6 January 2012

How To Print Form Control


Using System.Runtime.InteropServices;
this utility is used to draw any control on form or whole form ..
this is used in Thermal printing card or barcode and I -card generation .




[DllImport("gdi32.dll")]
public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap Imagedata;
private void CaptureScreen()
{
   Graphics graphicpanel = Panel1.CreateGraphics();
   Size pansize = this.Size;
   Imagedata = new Bitmap(pansize .Width, pansize .Height, graphicpanel );
   Graphics memoryGraphics = Graphics.FromImage(Imagedata);
   IntPtr dc1 = graphicpanel .GetHdc();
   IntPtr dc2 = Imagedata .GetHdc();
   BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
   graphicpanel .ReleaseHdc(dc1);
   Imagedata .ReleaseHdc(dc2);
}
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
   e.Graphics.DrawImage(Imagedata, 0, 0);
}
private void printButton_Click(System.Object sender, System.EventArgs e)
{
   CaptureScreen();
   printDocument1.Print();
}

Wednesday, 4 January 2012

Type Casting in C#

Type casting :
1) cast double data type into int :

           int ans ;
           double a=10.5;
          double b =20.3;


          ans = (int)( a / b)  ;  //cast from double  to int .


2) cast int data type into byte :
    
            byte b ;
            int i=100;
  
            b =(byte) i ;   // cast from int to byte .
            Console.WriteLine(b);


        if int value is less thn 255 then there is no data lost .
         but if integer value is greater than 255 there will be data lost .



               byte b ;
               int i=257 ;
  
              b =(byte) i ;   // cast from int to byte .

              Console.WriteLine(b);




3) Cast uint data type into short :
                uint uval ;
                short sval ;
                uval =23000;
                 sval =(short) uval ;    // cast from Uint to short with no data lost  
                 Console.WriteLine(s);


        if  uval is greater than 32767 then data will be lost .


4) cast long into uint :


             uint uval ;
               long lval ;


              long =64000;


              uval =(uint) lval ;         //cast from long to uint with no data lost .
           if long value is negative then there will be data loss .


5) cast byte into char


                   byte bval ;
                   char ch ;
                   bval =88;


                 ch =(char)bval ;           // output x without data loss.
  

Enum example in C#

its a collection of similar data types .

we can declare enum as 


enum Enum_name
{
value1 ,     // by default its index is zero 
value2,
value3
}


How can we define start index for enum



enum Enum_name
{
value1 =100,     // by default its index is 100 
value2,
value3
}


How to get enum value 
void main()
{
Enum_name getvalue =Enum_name.value1;
Console.writeLine(getvalue);
}


Loop through Enum Datatype :



using System; 

class MainClass { 
  enum Month { january, feb, mar, apr, may, june,july,aug ,sept,nov,dec }; 

  public static void Main() { 
    Month i;

    for(i = Month .january; i <= Month .dec; i++)  
      Console.WriteLine(i + " has value of " + (int)i); 

  } 
}


Boxing and UnBoxing in C#

Boxing is to pass any data type to object

eg :

   int v=25;
  object o=v;        //this is called boxing

and get data type from object is called UnBoxing
eg:

  int val =(int)o ;                //this is called unboxing

Tuesday, 3 January 2012

Exception In C#

Exception - All exceptions are derived from System.Exception Class .
                  its a one  type of run time error. 


There are two types of exception 
1) System exception 
2) Application Exception.


Example ;




 public void Add()
{


try
{
    int[] num =new int[4];


   for(int i=0 ; i<10;i++)
{
     num[i] =i ;
}


}catch( IndexOutOfRangeException  ex)
{

Console.WriteLine("Standard message is: "); 
      Console.WriteLine(exc); // calls ToString()           
      Console.WriteLine("Stack trace: " + exc.StackTrace);   //   stack of call that leads to exception .
      Console.WriteLine("Message: " + exc.Message);               //string describe nature of error .
      Console.WriteLine("TargetSite: " + exc.TargetSite);    //method who generate excetoin .
      Console.WriteLine("Class defining member: {0}", exc.TargetSite.DeclaringType);
      Console.WriteLine("Member type: {0}", exc.TargetSite.MemberType);
      Console.WriteLine("Source: {0}", exc.Source);
      Console.WriteLine("Help Link: {0}", exc.HelpLink);
}
}

                 

How to use PARAMS in C#

PARAMS - is used to pass number of parameter of same data type to method .


such as
public static int Addition( params int[] nums)
{
int ans ;
  for(int i=0;i<nums.length;i++)
{
 ans +=nums[i] ;
}
return ans ;
}


void main()
{
   int result = Additon( 17,45,565,34,45)   ;  // pass any number of int parameter .
        Consol.WriteLine(result);


}

Use of ref and out parameter


1) Use of Ref and Out parameter :


Ref -is used for pass by reference rather than pass by value ..
        pass by reference require initialization of variable .
Class Addittion
{
        public int Add(ref int a , ref int b)
{
        return a+b;
}

}


Class Mainclass
{
public static void Main()
{
    int a =10;
    int b =20 ;
Addittion additem =new Addition();


 int result =additem.Add(ref a ,ref b);  //it will just pass reference not value ..


//     result =30


}
}
------------------------------------------------------------------------------------------


Out - initialization is not required for out parameter .
but assignment is necessary before return call. 
it is use to pass value out of method  .





Class Addittion
{
        public void Add( int a ,  int b out int ans)
{
        ans = a+b;
}

}


Class Mainclass
{
public static void Main()
{
     int a =10;
    int b =20 ;
int result ;
Addittion additem =new Addition();


  additem.Add( a , b ,out result  );  //it will just pass reference not value ..


//     result =30


}
}