Translate

Home > 2011

2011

Extented Datagrid, Enter press move next cell

Wednesday, November 23, 2011 Category : 0


public partial class DatagridControl : DataGridView
    {
       // ContextMenu m_contextmenu = null;
        public DatagridControl() :base()
           
        {
            InitializeComponent();
            this.BackColor = System.Drawing.Color.NavajoWhite;
            this.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.PeachPuff;
            this.AlternatingRowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.SandyBrown;
            this.DefaultCellStyle.BackColor = System.Drawing.Color.Honeydew;
            this.DefaultCellStyle.SelectionBackColor = System.Drawing.Color.LightGreen;
           
            this.BackgroundColor = System.Drawing.SystemColors.InactiveCaptionText;
            this.EditMode = DataGridViewEditMode.EditOnEnter;
           
           
           
            //this.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Custom;
            //this.RowHeadersWidth = 30;
        }
       
        //[System.Security.Permissions.UIPermission(
        //        System.Security.Permissions.SecurityAction.LinkDemand,
        //        Window = System.Security.Permissions.UIPermissionWindow.AllWindows)]
        //protected override bool ProcessDialogKey(Keys keyData)
        //{
        //    // Extract the key code from the key value.
        //    Keys key = (keyData & Keys.KeyCode);

        //    // Handle the ENTER key as if it were a RIGHT ARROW key.
        //    if (key == Keys.Enter)
        //    {
        //        return this.ProcessRightKey(keyData);
        //    }
        //    return base.ProcessDialogKey(keyData);
        //}

        //[System.Security.Permissions.SecurityPermission(
        //    System.Security.Permissions.SecurityAction.LinkDemand, Flags =
        //    System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)]
        //protected override bool ProcessDataGridViewKey(KeyEventArgs e)
        //{
        //    // Handle the ENTER key as if it were a RIGHT ARROW key.
        //    if (e.KeyCode == Keys.Enter)
        //    {
        //        return this.ProcessRightKey(e.KeyData);
        //    }
        //    return base.ProcessDataGridViewKey(e);
        //}
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                MoveToNextCell();
                return true;
            }
            else
                return base.ProcessDialogKey(keyData);

        }


        public void MoveToNextCell()
        {
            int CurrentColumn, CurrentRow;
            CurrentColumn = this.CurrentCell.ColumnIndex;
            CurrentRow = this.CurrentCell.RowIndex;
            if (CurrentColumn == this.Columns.Count - 1 && CurrentRow != this.Rows.Count - 1)
            {
               
                base.ProcessDataGridViewKey(new KeyEventArgs(Keys.Home));
                base.ProcessDataGridViewKey(new KeyEventArgs(Keys.Down));
            }
            else
                base.ProcessDataGridViewKey(new KeyEventArgs(Keys.Right));

        }
    }

In which scenario we use Abstract Classes and Interfaces

Tuesday, November 22, 2011 Category : 0

Interface:
–> If your child classes should all implement a certain group of methods/functionalities but each of the child classes is free to provide its own implementation then use interfaces.
For e.g. if you are implementing a class hierarchy for vehicles implement an interface called Vehicle which has properties like Colour MaxSpeed etc. and methods like Drive(). All child classes like Car Scooter AirPlane SolarCar etc. should derive from this base interface but provide a seperate implementation of the methods and properties exposed by Vehicle.
–> If you want your child classes to implement multiple unrelated functionalities in short multiple inheritance use interfaces.
For e.g. if you are implementing a class called SpaceShip that has to have functionalities from a Vehicle as well as that from a UFO then make both Vehicle and UFO as interfaces and then create a class SpaceShip that implements both Vehicle and UFO .

Abstract Classes
–> When you have a requirement where your base class should provide default implementation of certain methods whereas other methods should be open to being overridden by child classes use abstract classes.
For e.g. again take the example of the Vehicle class above. If we want all classes deriving from Vehicle to implement the Drive() method in a fixed way whereas the other methods can be overridden by child classes. In such a scenario we implement the Vehicle class as an abstract class with an implementation of Drive while leave the other methods / properties as abstract so they could be overridden by child classes.
–> The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.
For example a class library may define an abstract class that is used as a parameter to many of its functions and require programmers using that library to provide their own implementation of the class by creating a derived class.

Use an abstract class

  • When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning. This is the practice used by the Microsoft team which developed the Base Class Library. ( COM was designed around interfaces.)
  • Use an abstract class to define a common base class for a family of types.
  • Use an abstract class to provide default behavior.
  • Subclass only a base class in a hierarchy to which the class logically belongs.

Use an interface

  • When creating a standalone project which can be changed at will, use an interface in preference to an abstract class; because, it offers more design flexibility.
  • Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance—allowing a specific type to support numerous behaviors.
  • Use an interface to design a polymorphic hierarchy for value types.
  • Use an interface when an immutable contract is really intended.
  • A well-designed interface defines a very specific range of functionality. Split up interfaces that contain unrelated functionality.

Concat all column values in sql

Category : 0


Table Name WHArchieveOut
GoodReceiveID
CartonNo
1
1,2,3
2
1,5,7
1
4,5
1
6,7
2
2,3

SELECT     Main.GoodReceiveID,
                                                LEFT(Main.WHArchieveOut, Len(Main.WHArchieveOut) - 1) AS 'CartonList'
FROM         (SELECT DISTINCT ST2.GoodReceiveID,
                                                  (SELECT  ST1.CartonNo + ',' AS [text()]
                                                    FROM dbo.WHArchieveOut ST1
                                                    WHERE      ST1.GoodReceiveID = ST2.GoodReceiveID
                                                    ORDER BY ST1.GoodReceiveID
                                                                                                                                                                                                                FOR XML PATH('')) [WHArchieveOut]
                       FROM          dbo.WHArchieveOut ST2)[Main] where Main.GoodReceiveID = '1'


OutPut
GoodReceiveID
'CartonList'
1
1,2,3,4,5,6,7

Create your own Event using delegate

Monday, November 21, 2011 Category : 0

public delegate void afterButtonClick();

public event afterButtonClick onButtonClick;

private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (onButtonClick != null)
            {
                onButtonClick();
            }
        }


Use this code on Class , like custom control , when use this control you will find your event on event list of this control.

XML Parsing and Saving Using GDataXML

Tuesday, October 25, 2011 Category : 0

My XML Have Following pattern

<Party>
  <Player>
    <Name>Butch</Name>
    <Level>1</Level>
    <Class>Fighter</Class>
  </Player>
  <Player>
    <Name>Shadow</Name>
    <Level>2</Level>
    <Class>Rogue</Class>
  </Player>
  <Player>
    <Name>Crak</Name>
    <Level>3</Level>
    <Class>Wizard</Class>
  </Player>
</Party>

So i have root node Party and a Child Node Player
After parsing XML i will be used two class for store data in memory. here is the two class


//Party.h
#import <Foundation/Foundation.h>

@interface Party : NSObject {
    NSMutableArray *_players;
}

@property (nonatomic, retain) NSMutableArray *players;

@end


//
//  Party.m
//  XMLTest
//
//  Created by Ray Wenderlich on 3/17/10.
//  Copyright 2010 Ray Wenderlich. All rights reserved.
//

#import "Party.h"

@implementation Party
@synthesize players = _players;

- (id)init {

    if ((self = [super init])) {
        self.players = [[[NSMutableArray alloc] init] autorelease];
    }
    return self;
   
}

- (void) dealloc {
    self.players = nil;   
    [super dealloc];
}

@end


//
//  Player.h
//  XMLTest
//
//  Created by Ray Wenderlich on 3/17/10.
//  Copyright 2010 Ray Wenderlich. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef enum {
    RPGClassFighter,
    RPGClassRogue,
    RPGClassWizard
} RPGClass;
   
@interface Player : NSObject {
    NSString *_name;
    int _level;
    RPGClass _rpgClass;
}

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int level;
@property (nonatomic, assign) RPGClass rpgClass;

- (id)initWithName:(NSString *)name level:(int)level rpgClass:(RPGClass)rpgClass;

@end


#import "Player.h"

@implementation Player
@synthesize name = _name;
@synthesize level = _level;
@synthesize rpgClass = _rpgClass;

- (id)initWithName:(NSString *)name level:(int)level rpgClass:(RPGClass)rpgClass {

    if ((self = [super init])) {
        self.name = name;
        self.level = level;
        self.rpgClass = rpgClass;
    }   
    return self;
   
}

- (void) dealloc {
    self.name = nil;   
    [super dealloc];
}

@end

use following function in any class where you want to build your parse method

#import "Party.h"
#import "Player.h"
#import "GDataXMLNode.h"

+ (Party *)loadParty {

    NSString *filePath = [self dataFilePath:FALSE];
    NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:filePath];
    NSError *error;
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
    if (doc == nil) { return nil; }
   
    Party *party = [[[Party alloc] init] autorelease];
    //NSArray *partyMembers = [doc.rootElement elementsForName:@"Player"];
    NSArray *partyMembers = [doc nodesForXPath:@"//Party/Player" error:nil];
    for (GDataXMLElement *partyMember in partyMembers) {
               
        // Let's fill these in!
        NSString *name;
        int level;
        RPGClass rpgClass;

        // Name
        NSArray *names = [partyMember elementsForName:@"Name"];
        if (names.count > 0) {
            GDataXMLElement *firstName = (GDataXMLElement *) [names objectAtIndex:0];
            name = firstName.stringValue;
        } else continue;
               
        // Level
        NSArray *levels = [partyMember elementsForName:@"Level"];
        if (levels.count > 0) {
            GDataXMLElement *firstLevel = (GDataXMLElement *) [levels objectAtIndex:0];
            level = firstLevel.stringValue.intValue;
        } else continue;
       
        // Class
        NSArray *classes = [partyMember elementsForName:@"Class"];
        if (classes.count > 0) {
            GDataXMLElement *firstClass = (GDataXMLElement *) [classes objectAtIndex:0];
            if ([firstClass.stringValue caseInsensitiveCompare:@"Fighter"] == NSOrderedSame) {
                rpgClass = RPGClassFighter;
            } else if ([firstClass.stringValue caseInsensitiveCompare:@"Rogue"] == NSOrderedSame) {
                rpgClass = RPGClassRogue;
            } else if ([firstClass.stringValue caseInsensitiveCompare:@"Wizard"] == NSOrderedSame) {
                rpgClass = RPGClassWizard;
            } else {
                continue;
            }           
        } else continue;
       
        Player *player = [[[Player alloc] initWithName:name level:level rpgClass:rpgClass] autorelease];
        [party.players addObject:player];
       
    }
           
    [doc release];
    [xmlData release];
    return party;
   
}

+ (void)saveParty:(Party *)party {

    GDataXMLElement * partyElement = [GDataXMLNode elementWithName:@"Party"];
   
    for(Player *player in party.players) {
    
        GDataXMLElement * playerElement = [GDataXMLNode elementWithName:@"Player"];
        GDataXMLElement * nameElement = [GDataXMLNode elementWithName:@"Name" stringValue:player.name];
        GDataXMLElement * levelElement = [GDataXMLNode elementWithName:@"Level" stringValue:[NSString stringWithFormat:@"%d", player.level]];
        NSString *classString;
        if (player.rpgClass == RPGClassFighter) {
            classString = @"Fighter";
        } else if (player.rpgClass == RPGClassRogue) {
            classString = @"Rogue";
        } else if (player.rpgClass == RPGClassWizard) {
            classString = @"Wizard";
        }       
        GDataXMLElement * classElement = [GDataXMLNode elementWithName:@"Class" stringValue:classString];
       
        [playerElement addChild:nameElement];
        [playerElement addChild:levelElement];
        [playerElement addChild:classElement];
        [partyElement addChild:playerElement];
    }
   
    GDataXMLDocument *document = [[[GDataXMLDocument alloc] initWithRootElement:partyElement] autorelease];
    NSData *xmlData = document.XMLData;
   
    NSString *filePath = [self dataFilePath:TRUE];
    NSLog(@"Saving xml data to %@...", filePath);
    [xmlData writeToFile:filePath atomically:YES];
       
}

Download GDataXML class from here

Polymorphism

Monday, October 24, 2011 Category : 0

Polymorphism is the ability of an object of one class to appear and be used as an object 
of another class. This is usually done by creating methods and attributes that are similar to 
those of another class. 
// file: Animal.h
#import <Foundation/Foundation.h>
 
@interface Animal : NSObject
{
        NSString *name;
}
 
@property(copy) NSString *name;
 
-(id) initWithName: (NSString *) aName;
 
-(void) talk;
 
@end
 
// =============================
 
// file: Animal.m
#import "Animal.h"
 
@implementation Animal
 
@synthesize name;
 
-(id) initWithName:(NSString *)aName {
        self = [super init];
 
        if ( self ) 
                self.name = aName;
 
        return self;
}
 
-(id) init {
        return [self initWithName:@""];
}
 
-(void) talk {
        NSLog(@"%@: Animals cannot talk!", name);
}
 
-(void) dealloc {
        if ( name )
                [name release];
        [super dealloc];
}
 
@end
 
// =============================
 
// file: Cat.h
#import "Animal.h"
 
@interface Cat : Animal
 
-(void) talk;
 
@end
 
// =============================
 
// file: Cat.m
#import "Cat.h"
 
@implementation Cat
 
-(void) talk {
        NSLog(@"%@: Meow!", name);
}
 
@end
 
// =============================
 
// file: Dog.h
#import "Animal.h"
 
@interface Dog : Animal
 
-(void) talk;
 
@end
 
// =============================
 
// file: Dog.m
#import "Dog.h"
 
@implementation Dog
 
-(void) talk {
        NSLog(@"%@: Woof! Woof!", name);
}
 
@end
 
// =============================
 
// file: main.m
#import <Foundation/Foundation.h>
#import "Animal.h"
#import "Cat.h"
#import "Dog.h"
 
int main (int argc, const char * argv[])
{
        // all instances are behind a superclass type (Animal)
        Animal *animal = [[Animal alloc] initWithName:@"Animal"];
        Animal *missy = [[Cat alloc] initWithName:@"Missy"];
        Animal *mr = [[Cat alloc] initWithName:@"Mr. Mistophelees"];
        Animal *lassie = [[Dog alloc] initWithName:@"Lassie"];
 
        // polymorphic behavior
        [animal talk];
        [missy talk];
        [mr talk];
        [lassie talk];
 
        // releasing memory
        [animal release];
        [missy release];
        [mr release];
        [lassie release];
 
    return 0;
}
 
// =============================
 
// --> Console output:
// Animal: Animals cannot talk!
// Missy: Meow!
// Mr. Mistophelees: Meow!
// Lassie: Woof! Woof!

Powered by Blogger.