:::: MENU ::::
Monthly Archives: January 2016

Bunch of Useful extension methods

Introduction

Most favorite things about C# 3.0 compilers  is the extension methods. Extension methods allow us to extend an Existing Type without deriving ,modifying  or recompiling it.If you ever worked with LINQ  then I am sure you are  already  used the extension methods because LINQ holds bunch of extension methods.here is how visual studio indicate the extension methods with an existing type.

linqExt

an arrow with a method indicates an extension method

 

extension methods greatly simplify the methods call.Actually An extension method is static method  existing in a static class.Before diving into the detail let’s  create an extension methods which will reverse the order of words inside a given sentence.

here is how you will use it.
basically it is just a syntactic sugar,at compile time this methods call is turned into an ordinary static method call

Specification of an Extension Methods.

  1. Extension method must be a static method.
  2. It must be located inside a static class.
  3. Extension method’s first parameter specifies the type of the extended object, and it must preceded by the “this” keyword.
  4. Extension method should be in the same namespace if  namespace is different then we need to include the namespace
  5. An extension method couldn’t override the existing instance methods.

Now i would share some helpful extension methods

SmExtensionMethods

This is common practice when we want to convert a string into integer and if there is exception then assign a default value in order to deal with this situation i have written some extension methods which beautifully converts a given string into primitive types like int , long short and gracefully handle above exception see an example here.

in this above extension method we try to convert a string into int?  if there is an exception we will return simply nullable Int so let the caller to handle the NULL Exception and here is how you can use it.

in this above example we are using ?? operator called null-coalescing operator It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand.
See below even more examples

You can fork the complete library on  github

Please follow and like us:

Accordion Image Slider winform control

We have seen a lot of image Slider Control for web built on jquery but I have created a Thumbnail image slider control for winform that can be used like an ordinary winform control with few lines of code,you can see the Output at bottom.Here is how you can utilize this control.Drop ImageAccordion,PictureBox,Label on the form from the tool box  where ImageAccordion is the control i created , you need to wire up these control like this.

and finally we need to subscribe the ThumbnailChanged event of ImageAccordion Control so we can change Full Preview Image

Accordion Image Slider

image accordion slider winform

Now i would explain in detail how this control works it self. This control is actually composed from three different control 1st of them is the left and right buttons. In order make button fancy i extended the button Class and painted the button graphics so we can get fancy effects,here is the code.

here is the output of AccordionButton you can change the HoverColor,DownColor and Face of the button by chaining Hover,Down Color and Face property respectively
buttons

the 2nd control is the thumbnail Box which shows the thumbnails with text in the slider this controls it self composed with a picture box and label.

here is output

thumbnailbox
Now finally we will compose the ImageAccordiong Control from buttons and thumbnail box we created.

image accordion

hope you like it. you can fork the complete solution on  github. Please share your thoughts.

Please follow and like us: