Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting a date for a CMonthCalCtrl

Status
Not open for further replies.

mpsoutine

Programmer
Jan 6, 2003
87
US
Hi,

I'm trying to retrieve the date in the first record in a recordset and set the date in CMonthCalCtrl object to the retrieved date. I'm using: TmpDlg.m_mcCal.GetToday(TmpSet.m_Date) but this line is causing a runtime error:
Any insight into what I'm missing

MPSoutine

The Error message
BOOL CMonthCalCtrl::GetToday(COleDateTime& refTime) const
{
ASSERT:):IsWindow(m_hWnd));

// can't use this method on multiple selection controls
ASSERT(!(GetStyle() & MCS_MULTISELECT));

SYSTEMTIME sysTime;
BOOL bResult = (BOOL)
::SendMessage(m_hWnd, MCM_GETTODAY, 0, (LPARAM) &sysTime);

if (bResult)
refTime = COleDateTime(sysTime);

return bResult;
}

This is the function I am using to try and display the retrieved date in the CMonthCalCtrl of a modal dialog box.


void CTimeDlg::OnBtnSettime()
{


//m_mcCal is the CMonthCalCtrl object in CDialogGetDate
//m_Date is a COleDateTime object in CTimeSet.
CDialogGetDate TmpDlg;
CTimeSet TmpSet;
COleDateTime TmpDate;

int nYear;
int nMonth;
int nDay;


TmpSet.m_strSort = "TimeID DESC";
//this will give us the first record in the recordset in descending order.

if(!TmpSet.IsOpen())
TmpSet.Open();


//How do we get the m_Date value from the recordset and set the m_mcCal to that date.


nDay = TmpSet.m_Date.GetDay();
nMonth = TmpSet.m_Date.GetMonth();
nYear = TmpSet.m_Date.GetYear();
CString strDate;
strDate.Format("The date is: %d/ %d/ %d ",nMonth,nDay,nYear);
AfxMessageBox(strDate);




TmpDlg.m_mcCal.GetToday(TmpSet.m_Date);


TmpDlg.DoModal();




}
 
TmpDlg.m_mcCal.GetToday(TmpSet.m_Date);
TmpDlg.DoModal();


Your accessing the Calendar Control of the dialog before you create the dialog, therefore if the dialog window does not exist then neither does the child window (calendar control).

-pete


 
Hi,

Doesn't the line:

void CTimeDlg::OnBtnSettime()
{

.
.
.

CDialogGetDate TmpDlg;
.
.
.
}

create the dialog box object? I added an edit box to CDialogGetDate and was able to show the date from the recordset.

TmpDlg.m_strSelDate = strDate;


MPSoutine.
 
MPSoutine,

The MFC class CWnd encapsulates window handles. When you create a C++ object that C++ object instance of course exists. However the OS Window that the CWnd class encapsulates (HWND) has not yet been created. Look at the CreateXXX functions of the CWnd class, that is how the OS Window objects are created and the HWND member of the CWnd class is set to a valid HWND value.

Until the Window has been created any call that uses the HWND member variable will of course fail since the HWND is invalid.

-pete


 
Hi Pete,

I'm making progress if you call the program not crashing progress. I go over the problem again in case I've confused you. Below is an update of the problem.

. I have a simple dialog based application with two buttons; "Select a date" and "Get first date from DB". Each button will invokes the SAME dialog box; MonthDlg. MonthDlg has Month control and an Edit control.


This is what works so far:

1: click "Select a date"
2: MonthDlg appears
3: click a date on the Month control
4: click the OK button on the dialog box to update the
recordset.



The problem is with the "Get first date from DB" button.

1: click "Get first date from DB"
2: MonthDlg appears with the first date in the DB appearing in the Edit control.

WHAT I WANT TO DO: have the Month control reflect this date as well.


You indicate the control has not been created. I read up on Create for CMonthCalCtrl. I did add a Create method in my code. I placed it in my GetDate method. I'm confused about the Create method because 1: I've never used it before and 2: I did not use it in my "SelectDate" method. As I mentioned earlier this method invokes the same dialog box as GetDate(). I'm wondering why it is not used in my GetDate() method but I get the results I want.
When I added a Create method in GetDate() the program still executes but the Month control does not reflect the first date in the database. Not crashing is progress to me.

void CTimeDlg::OnBtnGetDate()
{


//m_mcCal is the CMonthCalCtrl object in CMonthDlg
//m_Date is a COleDateTime object in CTimeSet.

CMonthDlg TmpDlg;

CPoint TmpPt;
TmpPt.x = 10;
TmpPt.y = 10;

TmpDlg.m_mcCal.Create(MCS_DAYSTATE,TmpPt,this,IDC_MC_CAL);

CTimeSet TmpSet;
CTimeSet* pSet;

pSet = &TmpSet;


COleDateTime TmpDate;

int nYear;
int nMonth;
int nDay;


TmpSet.m_strSort = "TimeID DESC";
//this will give us the first record in the recordset in descending order.

if(!TmpSet.IsOpen())
TmpSet.Open();


//How do we get the m_Date value from the recordset and set the m_mcCal to that date.


nDay = TmpSet.m_Date.GetDay();
nMonth = TmpSet.m_Date.GetMonth();
nYear = TmpSet.m_Date.GetYear();
CString strDate;
strDate.Format("%d/ %d/ %d ",nMonth,nDay,nYear);
AfxMessageBox(strDate);

TmpDlg.m_strSelDate = strDate;


//TmpDlg.m_mcCal.SetToday(TmpSet.m_Date);
TmpDlg.m_mcCal.SetCurSel(TmpSet.m_Date);




TmpDlg.DoModal();




}


 
Add a COleDateTime object as a member variable to your MonthDlg. Then also add BOOL m_bInitializeCalendar.

In the GetFirstDateFromDB code, set the COleDateTime to the value you want the calendar control initialized with, and set m_bInitializeCalendar to TRUE.

In your InitInstance, test the value of m_bInitalizeCalendar, if it is TRUE then initialize the monthcalendar control to the COleDateTime value.

Set m_bInitializeCalendar to FALSE in the SelectDate code.

I hope you follow, and I REALLY hope that helps.
Will
 
The calendar is a Child window of the Dialog window so you can’t create it before you create the Dialog window. The Dialog window, and all children windows are created when you call the TmpDlg.DoModal() function.
Code:
void CTimeDlg::OnBtnGetDate(){

	TmpDlg dlg;
	// at this point a C++ class exists but no windows have been created. But we
	// can still pass data to the class and/or call functions of the class that
	// are not going to use the HWND for the dialog since it does not exists.
	dlg.setDateTo( ...);	// you have to provide this function in the TmpDlg class
	
	dlg.DoModal();	// this creates all the windows of the dialog and sends
						// a WM_INITDIALOG message to the TmpDlg C++ class.. so
						// now you can initialize the calendar control. You do that
						// by handling the WM_INITDIALOG message in the TmpDlg class
}

void TmpDlg::setDateTo( COleDateTime& date){
	m_date = date;
}

BOOL TmpDlg::OnInitDialog(){
	// now use m_date to set the date of the calendar since the calendar window
	// now exists
	m_mcCal.SetCurSel(...);
}

-pete
I just can't seem to get back my IntelliSense
 
Hi,

Cool! I finally got it to work. I guess I'm not completely familiar the use of OnInitDialog() and WM_INITDIALOG. I just did not think of it because I did not see the OnInitDialog() method in the class CMonthDlg. I did see this method in the class CTimeDlg, the main dialog box. I was thinking one ONINITDIALOG() per application instead of each dialog box has an ONINITDIALOG(). I guess the rule of thumb is: USE ONINITDIALOG if you want a window to be initialized to a specific value. Thanks for the help. It did clear up a lot of confusion

MPSoutine
 
>> Thanks for the help. It did clear up a lot of confusion

Glad it’s working, that’s the result we are looking for here. Also thanks for posting a result reply. Those are important as both feedback to those that posted solutions and as verification of solutions to readers having similar problems.

-pete
I just can't seem to get back my IntelliSense
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top