Popular Posts

There was an error in this gadget

Oct 18, 2012

Agile Thinking



- Analyse root causes of problems
- Plot the course to a better future
- Plan quickly and thoroughly
- Resolve conflicts without compromise

http://flyinglogic.com

Oct 6, 2012

Using Anonymous Method in C#




Implementing an Anonymous Method:

-Open windows form application

public Form1()
        {
            InitializeComponent();

            Button btnHello = new Button();
            btnHello.Text = "Hello";

            btnHello.Click +=
                delegate
                {
                    MessageBox.Show("Hello");
                };

            Controls.Add(btnHello);
        }



Using Parameters with Anonymous Methods

Button btnHello = new Button();
            //Button btnGoodBye = new Button();
            btnHello.Text = "Hello";

            btnHello.Click +=
                delegate(object sender, EventArgs e)
                {
                    string message = (sender as Button).Text;
                    MessageBox.Show(message);
                };

            Controls.Add(btnHello);


Why should we use Anonymous method?

We can reduce the code by preventing delegate instantiation and registering it with methods. It increases the
readability and maintainability of our code by keeping the caller of the method and the method itself as close to one another as possible

Call Anonymous Method using event programming In C#


Anonymous Method

- An anonymous method is a method without a name - which is why it is called anonymous. You
don't declare anonymous methods like regular methods. Instead they get hooked up directly to
events. You'll see a code example shortly.
-  To see the benefit of anonymous methods, you need to look at how they improve your
development experience over using delegates. Think about all of the moving pieces there are with
using delegates: you declare the delegate, write a method with a signature defined by the delegate
interface, delcare the event based on that delegate, and then write code to hook the handler method
up to the delegate. With all this work to do, no wonder programmers, who are new to C#
delegates, have to do a double-take to understand how they work. Because you can hook an
anonymous method up to an event directly, a couple of the steps of working with delegates can be
removed.
-  The code in Listing below is a Windows Forms application. It instantiates a Button control and
sets its Text to "Hello". Notice the  combine,  +=, syntax being used to hook up the anonymous
method. You can tell that it is an anonymous  method because it uses the delegate keyword,
followed by the method body in curly braces. Remember to terminate anonymous methods with a
semi-colon.


Implementing an Anonymous Method:

-Open windows form application

public Form1()
        {
            InitializeComponent();

            Button btnHello = new Button();
            btnHello.Text = "Hello";

            btnHello.Click +=
                delegate
                {
                    MessageBox.Show("Hello");
                };

            Controls.Add(btnHello);
        }

Events Programming in C#


Events
- An event is an automatic notification that some action  has occurred.
-  An event might be a button push, a menu selection, and so forth. In short, something
happens and you must respond to it with an event handler. 
-  Event is a member of a class and are declared using the event keyword which has the
form-


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

namespace Final_events
{
    // Declare a delegate for an event.  
    delegate void MyEventHandler();

    // Declare an event class. 
    class MyEvent
    {
        public event MyEventHandler SomeEvent; 
 
          // This is called to fire the event. 
          public void OnSomeEvent() { 
            if(SomeEvent != null) 
              SomeEvent(); 
          } 
        }
   

    

    class Program
    {
        // An event handler. 
        static void handler()
        {
            Console.WriteLine("Event occurred");
        }

        static void Main(string[] args)
        {
            MyEvent evt = new MyEvent(); 
 
            // Add handler() to the event list. 
            evt.SomeEvent += handler; 
         
            // Fire the event. 
            evt.OnSomeEvent();

            Console.ReadKey();

        }
    }
}

Purpose of Delegate And Captured variable in C#.




Demonstrate a captured variable.


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

namespace DemoCapture
{
    // This delegate returns int and takes an int argument. 
    delegate int CountIt(int end);
    class VarCapture
    {
        static CountIt counter()
        {
            int sum = 0;

            // Here, a summation of the count is stored 
            // in the captured variable sum. 
            CountIt ctObj = delegate(int end)
            {
                for (int i = 0; i <= end; i++)
                {
                    Console.WriteLine(i);
                    sum += i;
                }
                return sum;
            };
            return ctObj;
        }

        class Program
        {
            static void Main(string[] args)
            {
                // Get a counter 
                CountIt count = counter();

                int result;

                result = count(3);
                Console.WriteLine("Summation of 3 is " + result);
                Console.WriteLine();

                result = count(5);
                Console.WriteLine("Summation of 5 is " + result);
                Console.ReadKey();
            }
        }
    }
}



Purpose of Delegate

- It supports event programming
- It gives the program a way to execute a method at runtime without having to know
precisely what that method is at compile time.





An anonymous method that returns a value(C#)



Demonstrate an anonymous method that returns a value



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

namespace AnoynRetnValue
{

    // This delegate returns a value. 
    delegate int CountIt(int end);


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

            // Here, the ending value for the count 
            // is passed to the anonymous method.   
            // A summation of the count is returned. 
            CountIt count = delegate(int end)
            {
                int sum = 0;

                for (int i = 0; i <= end; i++)
                {
                    Console.WriteLine(i);
                    sum += i;
                }
                return sum; // return a value from an anonymous method 
            };

            result = count(3);
            Console.WriteLine("Summation of 3 is " + result);
            Console.WriteLine();

            result = count(5);
            Console.WriteLine("Summation of 5 is " + result);

            Console.ReadKey();
        }
    }
}

Anonymous method that takes an argument(C#)



An anonymous method that takes an argument



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

namespace DemonsAnoynmous
{
    // Notice that CountIt now has a parameter. 
    delegate void CountIt(int end); 

    class Program
    {
        static void Main(string[] args)
        {
            // Here, the ending value for the count 
            // is passed to the anonymous method. 
            CountIt count = delegate(int end)
            {
                for (int i = 0; i <= end; i++)
                    Console.WriteLine(i);
            };

            count(3);
            Console.WriteLine();
            count(5);
            Console.ReadKey();
        }
    }
}

Introduce Anonymous Methods In C#




An anonymous method is, essentially, a block of code that is passed to delegate.


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

namespace AnoynmousMethos
{

    // Declare a delegate.   
    delegate void CountIt(); 

    class Program
    {
        static void Main(string[] args)
        {
            // Here, the code for counting is passed 
            // as an anonymous method.     
            CountIt count = delegate
            {
                // This is the block of code passed to the delegate.  
                for (int i = 0; i <= 5; i++)
                    Console.WriteLine(i);
            }; // notice the semicolon 

            count();

            Console.ReadKey();
        }
    }
}

Oct 3, 2012

AutoComplete TextBox in C#


         


   We are familiar with the auto completion text feature available in browsers, search controls and other controls. The auto completion feature is when you start typing some characters in a control, the matching data is loaded automatically to make easier to type. 


 In formload method type the code below:

            comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

            // AutoCompleteStringCollection
            AutoCompleteStringCollection data = new AutoCompleteStringCollection();
            data.Add("Khalid");
            data.Add("Sabus");
            data.Add("Sagar");
            data.Add("Jubayer");
            data.Add("Jamil");
            data.Add("Jaki");
            data.Add("Jibon");
            data.Add("Tomal");
            data.Add("Jamal");

            comboBox1.AutoCompleteCustomSource = data;


            comboBox2.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox2.AutoCompleteMode = AutoCompleteMode.SuggestAppend;


            comboBox2.AutoCompleteCustomSource = data;