cookieChoices = {};

Friday 25 April 2014

Handling Mouse Right Click Event Example

Main.Xaml



<Grid x:Name="LayoutRoot" Background="Black">
        <Border x:Name="BorderCtrl" Height="400" Width="400" MouseLeftButtonDown="BorderCtrl_MouseLeftButtonDown"
                MouseRightButtonDown="BorderCtrl_MouseRightButtonDown" MouseRightButtonUp="BorderCtrl_MouseRightButtonUp" >
            <Border.BorderBrush>
                <LinearGradientBrush EndPoint="0,0" StartPoint="1,1">
                    <GradientStop Color="#333333" Offset="0"/>
                    <GradientStop Color="#999333" Offset="1"/>
                </LinearGradientBrush>
            </Border.BorderBrush>
            <Border.Background>
                <LinearGradientBrush EndPoint="0.5,0" StartPoint="0.5,1">

                    <GradientStop Color="#1F1F1F" Offset="0"/>
                    <GradientStop Color="#060606" Offset="1"/>
                </LinearGradientBrush>

            </Border.Background>

        </Border>
        <TextBlock Text="Right Click on App" IsHitTestVisible="False" Foreground="White" FontSize="15" VerticalAlignment="Center" HorizontalAlignment="Center" />
        <Canvas>
            <Popup x:Name="ContextMenu" Visibility="Collapsed">
                <Border CornerRadius="5" BorderBrush="White" Background="Black" BorderThickness="1">
                    <StackPanel>
                        <HyperlinkButton BorderBrush="White" Visibility="Visible" Click="Green" Content="Green" Foreground="White" ></HyperlinkButton>
                        <HyperlinkButton BorderBrush="White" Visibility="Visible" Click="Red" Content="Red" Foreground="White" ></HyperlinkButton>
                        <HyperlinkButton BorderBrush="White" Visibility="Visible" Click="Blue" Content="Blue" Foreground="White" ></HyperlinkButton>
                        <HyperlinkButton BorderBrush="White" Visibility="Visible" Click="White" Content="White" Foreground="White" ></HyperlinkButton>


                    </StackPanel>
                </Border>
            </Popup>

        </Canvas>



    </Grid>

Main.Xaml.cs


 private void BorderCtrl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ContextMenu.Visibility = Visibility.Collapsed;
            ContextMenu.IsOpen = true;
        }

        private void BorderCtrl_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        private void BorderCtrl_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            Point p = e.GetPosition(null);
            e.Handled = true;
            ContextMenu.Visibility = Visibility.Visible;
            ContextMenu.IsOpen = true;
            ContextMenu.SetValue(Canvas.LeftProperty, (double)p.X);
            ContextMenu.SetValue(Canvas.TopProperty, (double)p.Y);
        }

        private void Red(object Sender, RoutedEventArgs e)
        {
                   
            Brush br = new SolidColorBrush(Color.FromArgb(100,250,0,0));
            LayoutRoot.Background = br;
        }

        private void Green(object Sender, RoutedEventArgs e)
        {

            Brush br = new SolidColorBrush(Color.FromArgb(100, 0, 250, 0));
            LayoutRoot.Background = br;
        }

        private void Blue(object Sender, RoutedEventArgs e)
        {

            Brush br = new SolidColorBrush(Color.FromArgb(100, 0, 0, 250));
            LayoutRoot.Background = br;
        }

        private void White(object Sender, RoutedEventArgs e)
        {

            Brush br = new SolidColorBrush(Color.FromArgb(100,250, 250, 250));
            LayoutRoot.Background = br;
        }
        

Thursday 24 April 2014

Share Data Between Applications

Main Page.Xaml:

 <Grid x:Name="LayoutRoot" Background="White">

        <Button Content="Set To File" Height="57" HorizontalAlignment="Left" Margin="70,92,0,0" Name="btnSetFile" VerticalAlignment="Top" Width="98" Click="btnSetFile_Click" />
        <Button Content="Get From File" Height="57" HorizontalAlignment="Right" Margin="0,92,90,0" Name="btnGetFile" VerticalAlignment="Top" Width="100" Click="btnGetFile_Click" />

    </Grid>
MainPage.Xaml.cs:

private void btnSetFile_Click(object sender, RoutedEventArgs e)
        {
            IsolatedStorageFile myStorage = IsolatedStorageFile.GetUserStoreForApplication();

            using (StreamWriter sw = new StreamWriter(new IsolatedStorageFileStream("ISData.txt", FileMode.Create, FileAccess.Write, myStorage))) 
            {
                sw.WriteLine("Isolated Storage Demo");
                sw.Close();
            }
        }

        private void btnGetFile_Click(object sender, RoutedEventArgs e)
        {
            IsolatedStorageFile myStorage = IsolatedStorageFile.GetUserStoreForApplication();

            IsolatedStorageFileStream fs = myStorage.OpenFile("ISData.txt", FileMode.Open, FileAccess.Read);

            using (StreamReader sr = new StreamReader(fs))
            {
               MessageBox.Show(sr.ReadLine());
            }


        }

Tuesday 22 April 2014

SilverLight Interact with Html and JS

Interact with Html 


Create new Project of SilverLight Applicatioon with host website



Add Following code to the  .html page 


.htmlContainer
        {
        margin-top:115px;
        margin-left:0px;
        float:left;
        font-size:1.4em;
        }

<div class="htmlContainer">
        <input type="text" name="txtHtml" />
        <input type="button" name="btnHtml" value="Demo" onclick="CallSL()" />
    </div>

Main.Xaml:

 <Grid x:Name="LayoutRoot" Background="White">
        <Canvas>
            <TextBox Canvas.Left="211" Canvas.Top="99" Height="23" Name="txtSV" Width="120" />
            <Button Canvas.Left="211" Canvas.Top="149" Content="InteractHTML" Height="23" Name="btnInteractHtml" Width="75" Click="btnInteractHtml_Click" />
            <Button Canvas.Left="218" Canvas.Top="32" Content="Call JS" Height="26" Name="btnJS" Width="113" Click="btnJS_Click" />
        </Canvas>
    </Grid>

Main.Xaml.cs:

Add namespace--> using System.Windows.Browser;

 public MainPage()
        {
            InitializeComponent();
            HtmlPage.RegisterScriptableObject("Page", this);
        }

        private void btnInteractHtml_Click(object sender, RoutedEventArgs e)
        {
            
            HtmlDocument doc = HtmlPage.Document;
            HtmlElement element = doc.GetElementById("txtHtml");

            if (element != null)
                element.SetProperty("value", txtSV.Text);
        }


Interact with JavaScript

Add Code to Html and Main.xaml page and see

In Html Page

       function ShowMsg(message) {
            alert(message);
        }

        function CallSL() {

            var slcontrol = document.getElementById('silverlightControlHost');
            slcontrol.Content.Page.Msg();

        }


    <div class="htmlContainer">
        <input type="text" name="txtHtml" />
        <input type="button" name="btnHtml" value="Demo" onclick="CallSL()" />
    </div>


Main.Xaml.cs      

private void btnJS_Click(object sender, RoutedEventArgs e)
        {
            HtmlPage.Window.Invoke("ShowMsg", "Hello SLight");
        }

        [ScriptableMember]
        public void Msg()
        {
            MessageBox.Show("From SL");
        }


SilverLight Life Cycle

SilverLight Life Cycle


1. User Request webpage.


2.Html Page <Object> tag loads silverlight plug in.


3.Plugin downloads Xap file and reads Manifest.


4.Create instance of Application Class.


5. Fires Application Start up event.


6. Complete page rendering.



Introduction Program


Select New project --> SliverLight -->

New Silverlight Dialog will open -- Check the Host Silverlight Application in new website

By checking  the check box , The Silverlight Application is new hosted in to the website by default with same silverlight Application Name.

--> Ok

Two projects are Created:
1.silverlight Application(with name u have given)
2. A new website application

In Silverlight Application 

Properties Folder AppManiFest.Xaml is required to generate Application pacakage (.Xap file). This file created .xap file in web application when you build.

In Silverlight : App.xaml works same as Global.asax in web forms

Copy and Paste code below in Main.xaml and Main.Xaml.cs

Build the Application and see the Output.



Main.Xaml:

<Grid x:Name="LayoutRoot" Background="White">
        <Button Content="Hello" Height="58" HorizontalAlignment="Left" Margin="95,130,0,0" Name="btnHello" VerticalAlignment="Top" Width="83" Click="btnHello_Click" />

    </Grid>

Main.Xaml.cs:

 private void btnHello_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Hello SilverLight");
        }


Thursday 3 April 2014

Example on Create and Use of DLL in Csharp

Dynamic Linking Library:

  A DLL is an application which can have everything what an EXE can have.

 The difference between them is, an EXE can execute independently but DLL cannot.

 The code in the DLL can be reused in many other applications but the code in EXE cannot be reused.

Types of DLL in Windows OS:

 1. Win32 DLL: Here the code is available in the form of simple "C" functions.

 2. COM DLL: This DLL have code in the form of reusable COM Components. These are also referred as ActiveX DLL.

  3. .NET DLL: These DLL’s (Portable Executables) are used for distributing the reusable classes to various types of .NET applications.

About Namespaces

 1. A namespace is a logical collection of classes and other types with unique names. In a given namespace all the types have unique name and thus it is used to resolve the ambiguity in case of name conflict.

 2. Any type when used outside the namespace, it must be qualified by its namespace, but when used by another type with in the same namespace it need not be qualified by the namespace.

 3. We can use using Namespace on top of the file so that we don’t have to qualify all the types of that namespace in that file.

=========================================================================================

How to Create DLL in CSharp:


Open New Project à Select ClassLibrary à Name as “CACreateDllExampleApp”à

Rename Class1.cs to Math.cs

Copy below Code to Math.cs


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

namespace CACreateDllExampleApp
{
    public class Math
    {
        public int sum(int a, int b)
        {
            return a + b;
        }

        public int diff(int a, int b)
        {
            return a - b;
        }

        public int product(int a, int b)
        {
            return a * b;
        }
    }
}

Build the Project.

A dll of name CACreateDllExampleApp.dll is generated in binàRelease folder.


How to Use DLL File in another Project

Create New Console Project

è  In Solution explorer Right Click on the Project à Add Reference à
è  Go to CACreateDllExampleApp Project Folder
è  In binàReleaseà you will find CACreateDllExampleApp.dll Add this reference to your current project

Copy the below code to Program.cs

Run the project.



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

namespace CAUseDLLApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 10;
            int y = 5;

            CACreateDllExampleApp.Math obj = new CACreateDllExampleApp.Math();
            string sum = obj.sum(x, y).ToString();
            string diff = obj.diff(x, y).ToString();
            string product = obj.product(x, y).ToString();

            Console.WriteLine(sum);
            Console.WriteLine(diff);
            Console.WriteLine(product);
            Console.ReadLine();
        }
    }
}



Wednesday 2 April 2014

Abstract Class Example

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

namespace CAInheritance_AbstractApp
{
    abstract  class Figure
    {
        public int dimension;
        public abstract double Area();
        public abstract double Perimeter();
    }

    class Square : Figure
    {
        public override double Area()
        {
            return dimension * dimension;
        }

        public override double Perimeter()
        {
            return 4 * dimension;
        }
    }

    class Circle : Figure
    {
        public override double Area()
        {
            return Math.PI * dimension * dimension;
        }

        public override double Perimeter()
        {
            return 2 *Math.PI* dimension;
        }
    }

    class AbstractApp
    {
        static void Main(string[] args)
        {
            Figure fi = new Square();
            fi.dimension = 10;
            Console.WriteLine(fi.Area());
            Console.WriteLine(fi.Perimeter());
            Console.ReadLine();
        }
     
    }
}

Inheritance Example

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

namespace CAInheritance_AbstractApp
{

    class cParent // Parent Class
    {
        public int pubA;
        private int priA;
        protected int proA;

        public cParent() //Default Constructor
        { }

        public cParent(int a, int b, int c) :this() // Parameterized Constructor
        {
            this.pubA = a;
            this.priA = b;
            this.proA = c;
        }
    }

    class cChild : cParent
    {
        public int pubB;

        public cChild()
        { }

        public cChild(int a, int b, int c,int d)
            : base(a, b, c)
        {
            this.pubA = a;
            this.proA = b;
            //this.priA = c; private A connot accesble here because of its access level
            this.pubB = d;
        }

        public void Foo()
        {
            cParent p = new cParent();
            cChild c = new cChild();

            proA = 10;
            c.proA = 10;

            //p.proA = 10; NotAllowed because it is accessble only through child
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            cChild b = new cChild(1, 2, 3, 4);
            b.Foo();
        }
    }
}

Genric Class Example

First we create a normal integer stack class .

Example of   integer stack class below:

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

namespace CAGenericExamples
{
    //Stack of Integer
    public class Stack
    {
        int[] data;
        int top = -1;

        public Stack(int size)
        {
            data = new int[size];
        }

        public void push(int value)
        {
            top++;
            data[top] = value;
        }

        public int pop()
        {
            int value = data[top];
            top--;
            return value;
        }

        public int GetTopElement()
        {
            return data[top];
        }

        public void print()
        {
            for (int i = 0; i <= top; i++)
            {
                Console.WriteLine(data[i]);
            }
        }

    }


    class Program
    {
        static void Main(string[] args)
        {
            Stack s = new Stack(5);
            s.push(5);
            s.push(12);
            s.print();
            int n = s.pop();
            Console.WriteLine(n);
            s.print();
            Console.ReadLine();
        }
    }
}

==============================================================

Next we create a normal object stack class .

Example of   object stack class below: it is Non Generic Stack Class

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

namespace CAGenericExamples
{
    class NonGenericStackClass
    {
        object[] data;
        int top = -1;

        public NonGenericStackClass(int size)
        {
            data = new object[size];
        }

        public void push(object value)
        {
            top++;
            data[top] = value;
        }

        public object pop()
        {
            object value = data[top];
            top--;
            return value;
        }

        public object GetTopElement()
        {
            return data[top];
        }

        public void print()
        {
            for (int i = 0; i <= top; i++)
            {
                Console.WriteLine(data[i]);
            }
        }
    }

    class NonGenericProgram
    {
        static void Main(string[] args)
        {
            NonGenericStackClass s = new NonGenericStackClass(5);
            s.push(5);
            s.push("CSharp");
            s.push(12);
            s.push(new Stack(5));
            s.print();
            Console.WriteLine(s.pop());
            s.print();
            Console.ReadLine();
        }
    }

}

=========================================================

From Above Two Example we know the difference between a specific datatype class and an object class.

Now we Create a generic Class of same above stack class for clear understanding

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

namespace CAGenericExamples
{
    class GenericClass<T>
    {
        T[] data;
        int top = -1;

        public GenericClass(int size)
        {
            data = new T[size];
        }

        public void push(T value)
        {
            top++;
            data[top] = value;
        }

        public T pop()
        {
            T value = data[top];
            top--;
            return value;
        }

        public T GetTopElement()
        {
            return data[top];
        }

        public void print()
        {
            for (int i = 0; i <= top; i++)
            {
                Console.WriteLine(data[i]);
            }
        }

    }

    class GenericProgram
    {
        static void Main(string[] args)
        {
            GenericClass<int> s = new GenericClass<int>(5);
            s.push(5);
            //s.push("CSharp"); ERROR Comes here if un comment
            s.push(12);
            s.print();
            Console.WriteLine(s.pop());
            s.print();

            GenericClass<string> ss = new GenericClass<string>(2);
            ss.push("CSharp");
            ss.push("Demo");
            ss.print();

            Console.ReadLine();
        }
    }

}

=========================================================

From the Above generic Class as example ,, we create a generic Collection class example below 

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

namespace CAGenericExamples
{

    class student
    {
        public int id;
        public string name;

        public student(int id, string name)
        {
            this.id = id;
            this.name = name;
        }
    }

    class GenericCollectionClass
    {
        static void Main(string[] args)
        {
            List<student> s = new List<student>();
            s.Add(new student(1, "s1"));
            s.Add(new student(2, "s2"));
            s.Add(new student(3, "s3"));
            s.Add(new student(4, "s4"));
            foreach (student ss  in s)
            {
                Console.WriteLine(ss.id +" "+ ss.name);
            }
            Console.WriteLine();

            /// Other way of printing student details collection

            IEnumerator<student> en = s.GetEnumerator();
            while (en.MoveNext())
            {
                student st = en.Current;
                Console.WriteLine(st.id +" "+st.name);
            }

            Console.ReadLine();
        }
    }
}