Discussion:
Nullable CDateTimeCtrl?
(too old to reply)
rockdale
2008-03-24 14:58:55 UTC
Permalink
Hi, all:

I have a datetime fiels visit_date on my dialog which currently using
CDateTimeCtrl for use to pick the date that they visit, but the visit
date should allow null which the CdateTimeCtrl does not support, what
is the best way to implement this?

I am thinking using a masked CEdit ctrl for user to enter date /delete
date, then put a button besidethe textbox so user can click this
button to shoiw the calendar if they want choose date from the
CDateTimeCtrl?

Or are any better implementations out there already, please point me
to the hyperlinks.

thanks
-rockdale
Doug Harrison [MVP]
2008-03-24 15:50:05 UTC
Permalink
On Mon, 24 Mar 2008 07:58:55 -0700 (PDT), rockdale
Post by rockdale
I have a datetime fiels visit_date on my dialog which currently using
CDateTimeCtrl for use to pick the date that they visit, but the visit
date should allow null which the CdateTimeCtrl does not support, what
is the best way to implement this?
I am thinking using a masked CEdit ctrl for user to enter date /delete
date, then put a button besidethe textbox so user can click this
button to shoiw the calendar if they want choose date from the
CDateTimeCtrl?
Or are any better implementations out there already, please point me
to the hyperlinks.
You can hide the current value with SetFormat(" "). To restore the default
format, SetFormat(0). This works great for showing a blank control, when
the control is disabled, but if you want to show a blank, enabled control,
you'll have to figure out which messages to handle in order to restore the
date/time display at the right moment.
--
Doug Harrison
Visual C++ MVP
Alfred Furer
2011-12-07 16:43:06 UTC
Permalink
Make a new class as below

Hope it helps you

.h
#pragma once

void AFXAPI DDX_DateTimeCtrl(CDataExchange* pDX, int nIDC, COleDateTime& value);


// CDateTimeEx

class CDateTimeEx : public CDateTimeCtrl
{
DECLARE_DYNAMIC(CDateTimeEx)

public:
CDateTimeEx();
virtual ~CDateTimeEx();

protected:
virtual BOOL PreTranslateMessage(MSG* pMsg);
afx_msg void OnPopupDelete();
afx_msg void OnDtnDatetimechange(NMHDR *pNMHDR, LRESULT *pResult);
DECLARE_MESSAGE_MAP()

private:
void ShowMenu(void);

public:
unsigned int m_nIdCtrl;
CWnd* m_pWnd;
};


.cpp
// DateTimeEx.cpp : implementation file
//

#include "stdafx.h"
#include "DateTimeEx.h"

void AFXAPI DDX_DateTimeCtrl(CDataExchange* pDX, int nIDC, COleDateTime& value)
{
CDateTimeEx dt;
CString strTemp;
HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
CDateTimeCtrl* pWnd = (CDateTimeCtrl*) CWnd::FromHandle(hWndCtrl);


ENSURE(pWnd);
if (pDX->m_bSaveAndValidate)
{
int nLen = ::GetWindowTextLength(hWndCtrl);
::GetWindowText(hWndCtrl, strTemp.GetBufferSetLength(nLen), nLen+1);
strTemp.ReleaseBuffer();

if (strTemp.IsEmpty() || strTemp == _T(" "))
{
value.SetStatus(COleDateTime::null);
}
else
{
if (!value.ParseDateTime(strTemp)) // throws exception
{
value.SetStatus(COleDateTime::null);
// Can't convert string to datetime
AfxMessageBox(AFX_IDP_PARSE_DATETIME);
pDX->Fail(); // throws exception
}
}
}
else
{
if (value.GetStatus() == COleDateTime::valid)
{
pWnd->SetFormat(NULL);
pWnd->SetTime(value);
strTemp = value.Format();
}
else
{
// Sets the time to the calendar
pWnd->SetTime(COleDateTime::GetCurrentTime());
// Clears the DateTime control
pWnd->SetFormat(" ");
strTemp = _T("");
AfxSetWindowText(hWndCtrl, strTemp);
}
}
}


// CDateTimeEx

IMPLEMENT_DYNAMIC(CDateTimeEx, CDateTimeCtrl)

CDateTimeEx::CDateTimeEx()
: m_nIdCtrl(0)
, m_pWnd(NULL)
{

}

CDateTimeEx::~CDateTimeEx()
{
}


BEGIN_MESSAGE_MAP(CDateTimeEx, CDateTimeCtrl)
ON_COMMAND(IDM_POPUP_DELETE, &CDateTimeEx::OnPopupDelete)
ON_NOTIFY_REFLECT(DTN_DATETIMECHANGE, &CDateTimeEx::OnDtnDatetimechange)
END_MESSAGE_MAP()



// CDateTimeEx message handlers




BOOL CDateTimeEx::PreTranslateMessage(MSG* pMsg)
{
BOOL bCtrlFound = FALSE;

// Retreiving Control ID
m_pWnd = FromHandle(pMsg->hwnd);
if (m_pWnd != NULL)
{
m_nIdCtrl = m_pWnd->GetDlgCtrlID();
bCtrlFound = TRUE;
}

switch (pMsg->message)
{
case WM_MOUSEMOVE:
break;

case WM_CHAR:
break;

case WM_LBUTTONDBLCLK:
break;

case WM_RBUTTONUP:
break;

case WM_RBUTTONDOWN:
ShowMenu();
break;

case WM_LBUTTONDOWN:
break;

case WM_KEYDOWN:
// No ESCAPE or RETURN Key
if( pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE )
{
::TranslateMessage(pMsg);
::DispatchMessage(pMsg);
return FALSE; // DO NOT process further
}
break;

// NO ALT+ key
case WM_SYSCOMMAND:
return TRUE;
break;
}

return CDateTimeCtrl::PreTranslateMessage(pMsg);
}

// Shows the delete popup menu item
void CDateTimeEx::ShowMenu(void)
{
int n = 0;

// Popup menu
CMenu popUpMenu;
popUpMenu.CreatePopupMenu();
popUpMenu.InsertMenu(n, MF_BYPOSITION, IDM_POPUP_DELETE, _T("Supprimer")); n++;

CPoint p;
GetCursorPos(&p);
popUpMenu.TrackPopupMenu(TPM_CENTERALIGN | TPM_LEFTBUTTON,
p.x, p.y, (CWnd *) this, NULL);
}


// Clears window content
void CDateTimeEx::OnPopupDelete()
{
SetFormat(" ");
m_pWnd->SetWindowTextA(_T(""));
}


void CDateTimeEx::OnDtnDatetimechange(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
// Sets format to default sytem date format
SetFormat(NULL);
*pResult = 0;
}
Post by Doug Harrison [MVP]
On Mon, 24 Mar 2008 07:58:55 -0700 (PDT), rockdale
You can hide the current value with SetFormat(" "). To restore the default
format, SetFormat(0). This works great for showing a blank control, when
the control is disabled, but if you want to show a blank, enabled control,
you'll have to figure out which messages to handle in order to restore the
date/time display at the right moment.
--
Doug Harrison
Visual C++ MVP
Post by Sheng Jiang[MVP]
use DTS_SHOWNONE
--
Sheng Jiang
Microsoft MVP in VC++
Post by rockdale
I have a datetime fiels visit_date on my dialog which currently using
CDateTimeCtrl for use to pick the date that they visit, but the visit
date should allow null which the CdateTimeCtrl does not support, what
is the best way to implement this?
I am thinking using a masked CEdit ctrl for user to enter date /delete
date, then put a button besidethe textbox so user can click this
button to shoiw the calendar if they want choose date from the
CDateTimeCtrl?
Or are any better implementations out there already, please point me
to the hyperlinks.
thanks
-rockdale
Post by rockdale
The first time I can use SetFormat("") to hide Today's date when I
initDialog, Then the user can
pick a date from the CDateTimeCtrl, but what if they try to delete/
empty the date they picked before? This part I do not know how. add a
button say "clear"
also, How could I know the DateTime is picked by the user or is the
underline datetime for SetFormat(""), or should I set a special date
so that I know the user did not pick a date?
thanks again
On Mar 24, 1:16=A0pm, "Sheng Jiang[MVP]"
e
Post by Sheng Jiang[MVP]
I am not suggesting changing the formating. I am suggesting the DTS_SHOWNONE
style.
Use SetTime/GetTime to get/set the time.A null time has a COleDateTime::null
status.
--
Sheng Jiang
Microsoft MVP in VC++
The first time I can use SetFormat("") to hide Today's date when I
initDialog, Then the user can
pick a date from the CDateTimeCtrl, but what if they try to delete/
empty the date they picked before? This part I do not know how. add a
button say "clear"
also, How could I know the DateTime is picked by the user or is the
underline datetime for SetFormat(""), or should I set a special date
so that I know the user did not pick a date?
thanks again
On Mar 24, 1:16 pm, "Sheng Jiang[MVP]"
message
Post by Doug Harrison [MVP]
On Wed, 26 Mar 2008 09:47:38 -0600, "Sheng Jiang[MVP]"
Last time I looked, DTS_SHOWNONE added a checkbox inside the control, and
when unchecked, simply grayed the text of the control. I never could get
the control to display nothing at all using DTS_SHOWNONE. The only way I
found was the SetFormat method I posted earlier.
--
Doug Harrison
Visual C++ MVP
Post by Sheng Jiang[MVP]
The op wants to
1 pick the date that they visit,
2 allow null which he thinks the CDateTimeCtrl does not support
I just point out CDateTimeCtrl does support null values.
I agree changing the formating will look nicer, but the feedback from data
entry people is that masked editbox makes input faster.
--
Sheng Jiang
Microsoft MVP in VC++
DTS_SHOWNONE
COleDateTime::null
Sheng Jiang[MVP]
2008-03-24 17:16:23 UTC
Permalink
use DTS_SHOWNONE
--
Sheng Jiang
Microsoft MVP in VC++
Post by rockdale
I have a datetime fiels visit_date on my dialog which currently using
CDateTimeCtrl for use to pick the date that they visit, but the visit
date should allow null which the CdateTimeCtrl does not support, what
is the best way to implement this?
I am thinking using a masked CEdit ctrl for user to enter date /delete
date, then put a button besidethe textbox so user can click this
button to shoiw the calendar if they want choose date from the
CDateTimeCtrl?
Or are any better implementations out there already, please point me
to the hyperlinks.
thanks
-rockdale
rockdale
2008-03-24 16:40:30 UTC
Permalink
The first time I can use SetFormat("") to hide Today's date when I
initDialog, Then the user can
pick a date from the CDateTimeCtrl, but what if they try to delete/
empty the date they picked before? This part I do not know how. add a
button say "clear"
also, How could I know the DateTime is picked by the user or is the
underline datetime for SetFormat(""), or should I set a special date
so that I know the user did not pick a date?

thanks again

On Mar 24, 1:16 pm, "Sheng Jiang[MVP]"
Post by Sheng Jiang[MVP]
use DTS_SHOWNONE
--
Sheng Jiang
Post by rockdale
I have a datetime fiels visit_date on my dialog which currently using
CDateTimeCtrl for use to pick the date that they visit, but the visit
date should allow null which the CdateTimeCtrl does not support, what
is the best way to implement this?
I am thinking using a masked CEdit ctrl for user to enter date /delete
date, then put a button besidethe textbox so user can click this
button to shoiw the calendar if they want choose date from the
CDateTimeCtrl?
Or are any better implementations out there already, please point me
to the hyperlinks.
thanks
-rockdale- Hide quoted text -
- Show quoted text -
Sheng Jiang[MVP]
2008-03-26 15:47:38 UTC
Permalink
I am not suggesting changing the formating. I am suggesting the DTS_SHOWNONE
style.
Use SetTime/GetTime to get/set the time.A null time has a COleDateTime::null
status.
--
Sheng Jiang
Microsoft MVP in VC++
"rockdale" <***@gmail.com> wrote in message news:f187b421-065f-4332-9b80-***@m3g2000hsc.googlegroups.com...
The first time I can use SetFormat("") to hide Today's date when I
initDialog, Then the user can
pick a date from the CDateTimeCtrl, but what if they try to delete/
empty the date they picked before? This part I do not know how. add a
button say "clear"
also, How could I know the DateTime is picked by the user or is the
underline datetime for SetFormat(""), or should I set a special date
so that I know the user did not pick a date?

thanks again

On Mar 24, 1:16 pm, "Sheng Jiang[MVP]"
Post by Sheng Jiang[MVP]
use DTS_SHOWNONE
--
Sheng Jiang
Post by rockdale
I have a datetime fiels visit_date on my dialog which currently using
CDateTimeCtrl for use to pick the date that they visit, but the visit
date should allow null which the CdateTimeCtrl does not support, what
is the best way to implement this?
I am thinking using a masked CEdit ctrl for user to enter date /delete
date, then put a button besidethe textbox so user can click this
button to shoiw the calendar if they want choose date from the
CDateTimeCtrl?
Or are any better implementations out there already, please point me
to the hyperlinks.
thanks
-rockdale- Hide quoted text -
- Show quoted text -
Doug Harrison [MVP]
2008-03-26 16:51:53 UTC
Permalink
On Wed, 26 Mar 2008 09:47:38 -0600, "Sheng Jiang[MVP]"
Post by Sheng Jiang[MVP]
I am not suggesting changing the formating. I am suggesting the DTS_SHOWNONE
style.
Use SetTime/GetTime to get/set the time.A null time has a COleDateTime::null
status.
Last time I looked, DTS_SHOWNONE added a checkbox inside the control, and
when unchecked, simply grayed the text of the control. I never could get
the control to display nothing at all using DTS_SHOWNONE. The only way I
found was the SetFormat method I posted earlier.
--
Doug Harrison
Visual C++ MVP
Sheng Jiang[MVP]
2008-03-26 18:07:04 UTC
Permalink
The op wants to
1 pick the date that they visit,
2 allow null which he thinks the CDateTimeCtrl does not support
I just point out CDateTimeCtrl does support null values.

I agree changing the formating will look nicer, but the feedback from data
entry people is that masked editbox makes input faster.
--
Sheng Jiang
Microsoft MVP in VC++
Post by Doug Harrison [MVP]
On Wed, 26 Mar 2008 09:47:38 -0600, "Sheng Jiang[MVP]"
Post by Sheng Jiang[MVP]
I am not suggesting changing the formating. I am suggesting the DTS_SHOWNONE
style.
Use SetTime/GetTime to get/set the time.A null time has a
COleDateTime::null
Post by Doug Harrison [MVP]
Post by Sheng Jiang[MVP]
status.
Last time I looked, DTS_SHOWNONE added a checkbox inside the control, and
when unchecked, simply grayed the text of the control. I never could get
the control to display nothing at all using DTS_SHOWNONE. The only way I
found was the SetFormat method I posted earlier.
--
Doug Harrison
Visual C++ MVP
Loading...