Thursday, January 10, 2008

Date of Birth Calculation in C# .Net

Calculate the Age in Years,Months and Days for Current





Introduction
If we give the Date of Birth Its Calculate the Age in Days,Month and Year


Using the code The user can calculate not only the age, he can calculate the what is the days, month, Year from Given date

Dynamic buttons function call: alternative of BEGIN_MESSAGE_MAP

This article discusses a trick to define/re-define a dynamic button for it's click


Introduction
This article's main purpose is to show another aspect of BEGIN_MESSAGE_MAP(). In MFC, whenever there is a need to make a button control dynamic, we often get hung up on the point of defining a method for that button such that when that button is clicked, our method will be called. This article is going to show you a sample of code where I have created a dynamic button. This button is mapped with a function as per the job selected by the radio buttons.


Background
As an MFC developer, you may know that if you have to do a task in which buttons or controls are going to be created dynamically, then in you need to presume constant(Resource_ID) for controls. You also have to specify ON_COMMAND(RESOURCE_ID, methodName) in BEGIN_MESSAGE_MAP(). Thus, whenever that button is clicked, the corresponding function will invoke.
However, for a more dynamic environment we need to find an alternative way in which we can re-define that method, where the method is already defined for that button-click event.
If you know .NET, then you must be familiar with delegates -- i.e. pointers to functions -- that have a significant role in defining any particular method for any control. I have tried something similar in this article, but for button-click case only. This time, I have covered only the ON_COMMAND() aspect of MFC with my message map. I have defined my own message map and assigned value to the controls, as per need. In the future I'll try to capture all of the events of MFC so that it will become flexible for all events.

Using the code
Here I have used a trick: just like BEGIN_MESSAGE_MAP(), I am collecting information about ID and its corresponding function. I have made my own map to collect all IDs and functions. Here is the map type created, having ID and function pointers:

typedef void (CDynButtonDlg::*fn)(int i);
typedef std::map<> EventMessageMap;

Using this map type, we have created a new variable. Here is the actual instance:
EventMessageMap msgMap;
Whenever we need to create a button dynamically, we'll make a corresponding entry in this map:
// To Create Dynamic Button with #define DYNAMIC_BUTTON_ID 123
m_btnDynamic.Create("Dynamic", WS_VISIBLEWS_CHILDBS_PUSHBUTTON,
CRect(100,150,200,200),this, DYNAMIC_BUTTON_ID);

// Make Entry in Map for this ID
msgMap[DYNAMIC_BUTTON_ID] = &CDynButtonDlg::Job1;
Now our work of changing functions is too simple. Whenever we wish to change the functionality of a function, we just need to do this:
// Make Entry in Map for this ID
msgMap[DYNAMIC_BUTTON_ID] = &CDynButtonDlg::Job2;

The only remaining question to be answered is, "How will this function be called when the button is clicked?" The answer is:
// OnCmdMsg is called when any command is fired
BOOL CDynButtonDlg::OnCmdMsg(UINT nID, int nCode,
void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
// Here we'll iterate our Map & call it's corresponding function.
EventMessageMap::iterator itTrg = msgMap.find( nID );
if(itTrg != msgMap.end())
{
fn btnM = msgMap[nID];
this->*btnM)(5);
}
return CDialog::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}

I am not claiming that this is the only way to do this. If you wish to fulfill your target, then you can even put conditions in one method that you already defined in BEGIN_MESSAGE_MAP(). I Just found a new way to accomplish this task, so I am here to share it with all of you.

Using button controls in an application

How to get a button control wired-in and working


Introduction
This tutorial could well be the simplest windows program you could ever write. All you need to understand this tutorial is the basics of windows messaging. This tutorial is about the CButton class, and how to get a simple button working.

This tutorial simply takes input for two numbers and depending on what button you click, does the math. ie. if you enter 1 and 2, then click on 'Plus' the answer will be 3. Pretty basic.
To create this program first we need to get the framework laid out. Now 99.9% of the time, you will be using buttons via a dialog box so that is where we will start. Open up the AppWizard and create a new project titled ButtonDemo. Just create a basic dialog box without any document / view architecture.



Then click on the ResourceVew tab in the 'Workspace' window. Proceed to edit the dialog box IDD_BUTTONDEMO_DLG. It will already contain the Buttons 'Ok' and 'Cancel'. Delete the 'TODO :' message and the 'Cancel' button so we can get to work.
Just drag n' drop the buttons needed, in this example 'Plus' and 'Minus'. Then select and right click to edit their properties. The MFC keeps track of these buttons by their unique ID, a macro located in the "Resource.h" file. For code clarity, change the name of the ID to ID_BUTTON_ADD and modify the caption of the button.

Next, let's add the Edit boxes to house the values to be added or subtracted, do this in a very similar manner to the way you added the buttons. Just drag and drop them in and then modify their ID. We will need an Edit box for the left and right side of the equation as well as one to house the answer. Just for clarity to the end user, check the 'disable' property of the last Edit box. Since we won't directly be able to specify the answer. We will also add a Static control for the equals sign. Just drop one in and then change the caption.


Now that our buttons and are in place we need to wire them into our application. We do this via the class wizard. (Control + W) Click on the Member Variables tab and proceed to add the variables m_nX, m_nY, and m_nAnswer. All integers; these will hold the values inputted from the Edit controls.



Then click on the Message Maps tab. And add a new function for the object ID ID_BUTTON_ADD and ID_BUTTON_SUBTRACT message BN_CLICK. This create a function is called whenever that button is clicked once. Denoted by OnButtonAdd and OnButtonSubtract.



Now that we have created our buttons this is all the code we need to write to get this program working. It is fairly self explanatory: what we did was create two functions to be called whenever the respective buttons were pressed. The UpdateData (BOOL) functions are used to manage the data in the Edit boxes of the dialog. UpdataData (TRUE), 'updates' the member variables linked to the Edit boxes to whatever is the current value. UpdataData (FALSE), updates the Edit box to whatever our variable is.

void CButtonDemoDlg::OnButtonAdd()
{
UpdateData (TRUE);
m_nAnswer = m_nX + m_nY;
UpdateData (FALSE);
}

void CButtonDemoDlg::OnButtonSubtract()
{
UpdateData (TRUE);
m_nAnswer = m_nX - m_nY;
UpdateData (FALSE);
}


That is it! All we need to do to implement a simple button.

Home >> Desktop Development >> Button Control >> Button Controls - Beginners

Join Mad Engineerss...!

Button Controls - Beginners

Using button controls in an application
How to get a button control wired-in and working
VC6, NT4, Win2K, VS6, MFC, Dev, Beginner

*********************************************************************************

Dynamic buttons function call: alternative of BEGIN_MESSAGE_MAP
This article discusses a trick to define/re-define a dynamic button for it's click
C++, Windows, VS, MFC, Dev, Intermediate

*********************************************************************************

Date of Birth Calculation in C# .Net
Calculate the Age in Years,Months and Days for Current day
.NET 1.0, .NET 1.1, .NET 2.0, Win2K, WinXP, Win2003, Mono, DotGNU, C# 1.0, C# 2.0, .NET 3.0, C# 3.0, .NET 3.5, C#, Windows, .NET, Beginner, Intermediate

*********************************************************************************

**********************************
Links
**********************************
Home
Button Control
Desktop Development

Join :- Mad Engineerss...!
**********************************

Button Controls

A button (often simply "button" or "pushbutton") is a simple switch mechanism for controlling some aspect of a process.


Contents
Button Controls - Beginners
Button Controls - General
Button Controls - Non-rectangular Buttons
Button Controls - Owner-draw buttons


***********************
Links
***********************
Home
Back
Maddy's Profile
Mad Engineerss...!
***********************

Wednesday, January 9, 2008

Development Lifecycle

All Development Lifecycle Topics


!~ Debug Tips
!~ Application design and architecture
!~ Installation
!~ Work Issues


***********************
Links
***********************
Home
Comments
Maddy's Profile
Mad Engineerss...!
***********************

Graphics / Design

All Graphics / Design Topics


!~ Microsoft Expression
!~ Usability and Accessibility


***********************
Links
***********************
Home
Comments
Maddy's Profile
Mad Engineerss...!
***********************

General Programming

All General Programming Topics

!~ Algorithms and Recipes
!~ Bugs and Workarounds
!~ Collections
!~ Cryptography and Security
!~ Date and Time
!~ DLLs, Assemblies and Manifests
!~ Exception Handling
!~ Localisation
!~ Macros and Add-ins
!~ Programming Tips
!~ String handling
!~ Threads, Processes and IPC
!~ WinHelp / HTMLHelp


***********************
Links
***********************
Home
Comments
Maddy's Profile
Mad Engineerss...!
***********************

Languages

All Languages Topics

!~ C / C++ Language
!~ C++ / CLI
!~ C#
!~ MSIL
!~ VBScript
!~ VB.NET
!~ VB6 Interop
!~ .NET Languages
!~ XML


***********************
Links
***********************
Home
Comments
Maddy's Profile
Mad Engineerss...!
***********************

Platforms & Frameworks

All Platforms, Frameworks & Libraries Topics

!~ ATL
!~ Microsoft Foundation Classes
!~ STL
!~ WTL
!~ COM / COM+
!~ .NET Framework
!~ Win32/64 SDK & OS
!~ Vista API
!~ Articles relating to the Vista Security APIs
!~ Cross Platform
!~ Game Development
!~ Mobile Development
!~ Windows CardSpace
!~ Windows Communication Foundation
!~ Windows Presentation Foundation
!~ Windows Workflow Foundation
!~ General Programming Libraries


***********************
Links
***********************
Home
Comments
Maddy's Profile
Mad Engineerss...!
***********************

Database

All Database Topics

!~ Database
!~ SQL Reporting Services

***********************
Links
***********************
Home
Comments
Maddy's Profile
Mad Engineerss...!
***********************

Multimedia

All Multimedia Topics


!~ Audio and Video
!~ DirectX
!~ GDI
!~ GDI+
!~ Graphics
!~ OpenGL


***********************
Links
***********************
Home
Comments
Maddy's Profile
Mad Engineerss...!
***********************

Enterprise Systems

All Enterprise Systems Topics

!~ Content Management Server
!~ Microsoft BizTalk Server
!~ Microsoft Exchange
!~ Office Development
!~ SharePoint Server


***********************
Links
***********************
Home
Comments
Maddy's Profile
Mad Engineerss...!
***********************

Web Development

All Web Development Topics

!~ AJAX and Atlas
!~ Applications and Free Tools
!~ ASP
!~ ASP.NET
!~ ASP.NET Intrinsic Controls
!~ ATL Server
!~ Caching
!~ Charts, Graphs and Images
!~ Client side scripting
!~ Custom Controls
!~ HTML / CSS
!~ Internet / Network
!~ ISAPI
!~ Server Management
!~ Session State
!~ Microsoft Silverlight
!~ Trace and Logs
!~ User Controls
!~ Validation
!~ View State
!~ WAP / WML
!~ Web Security
!~ Web Services


***********************
Links
***********************
Home
Comments
Maddy's Profile
Mad Engineerss...!
***********************

Desktop Development

All Desktop Development Topics

!~ Button Controls
!~ Clipboard
!~ Combo and List Boxes
!~ Dialogs and Windows
!~ Desktop Gadgets
!~ Document / View Framworks
!~ Edit Controls
!~ Files and Folders
!~ Grid and Data Controls
!~ Hardware and System
!~ List Controls
!~ Menus
!~ Miscellaneous
!~ Printing
!~ Progress Controls
!~ Selection Controls
!~ Shell and IE programming
!~ Smart Client
!~ Splitter Windows
!~ Static and Panel Controls
!~ Status Bar
!~ Tabs, Property Pages and Wizards
!~ Toolbars and Docking windows
!~ Tree Controls


************************
Links
************************
Home
Comments
Maddy's Profile
Mad Engineerss...!
************************