Nick,

The problem is the memory for CString is allocated locally in both your calls and needs to be around for the entire lifetime of the control. I suggest adding a member variable like

CString m_controlLabel;

to your header file and then writing your code like.

Boolean CMyForm::OnOpen(EventPtr pEvent, Boolean& bHandled)
{
 theTime.SetToCurrentDateTime();
 m_controlLabel = theTime.FormatDate(true);
 CButton * pButton = new CButton(MyDateButton2);
 pButton->SetLabel(m_controlLabel);
 bHandled = false;
 return true;
}



You can so something similar for the other method and things should work.

On a different note you may want to give the framework a crack at handling the OnOpen method as well by setting return = false. The other thing that comes to mind is you typically want to match up new and delete to avoid memory leaks, but that's probably another post:-)

Hope this helps,
-Mark


On Tuesday, June 24, 2003, at 12:21 AM, Nick wrote:


Hi,

I am trying to set the label of a selector trigger control to the current
date. I'm using POL 4.04.00 + CW 9.1. I have a form that traps the OnOpen
and OnDateButton calls to attempt to change the label (code is below).
MyDateButton2 is the ID of the SELECTORTRIGGER control. theTime is a
CDateTime instance.


I am definitely doing something wrong. There are two symptoms:

1. When execution enters the OnOpen event handler, everything executes and
the variables in the debugger reflect what the code is doing. That is,
theTime contains correct data. However upon stepping out of this code and
tracing back to the event handler macros in MyForm.h, the program eventually
crashes when it executes END_EVENT_MAP. The error returned from the
emulator is long winded, but here is an excerpt:


"MyApp just read from memory location 0x00037b72 which is an unallocated
chunk of memory. ...<snip>... Such an access usually means that an
application is accessing a chunk that used to be allocated but has since
been returned with MemPtrFree or MemFreeHandle."


2. If I comment out the first 4 lines of OnOpen, the form launches
correctly. Next, I tap on the control and the OnDateButton code executes.
Although the content of the control changes to the correct date, and goes
back to a normal state, if I click the control again, the emulator again
throws a similar error except it happens seemingly for each character in the
cs variable because as I click continue, I can see the characters contained
in the cs variable being drawn in the control.


I'm guessing that I am doing the control label changes at the wrong time.
Do I need to be deriving from CComponent, CComponentEvent, or something to
handle a pre and or post event for this control?


Thanks,
Nick.

Boolean CMyForm::OnOpen(EventPtr pEvent, Boolean& bHandled)
{
 theTime.SetToCurrentDateTime();
 CString cs = theTime.FormatDate(true);
 CButton * pButton = new CButton(MyDateButton2);
 pButton->SetLabel(cs);
 bHandled = false;
 return true;
}

Boolean CMyForm::OnDateButton(EventPtr pEvent, Boolean& bHandled)
{
 const char *pString = "Pick A Date";

 if(theTime.SelectDay(pString))
 {
  UInt16 x;
  x = theTime.GetMonth();
  x = theTime.GetDay();
  x = theTime.GetYear();

  CString cs = theTime.FormatDate(true);
  CButton *pButton = new CButton(MyDateButton2);
  pButton->SetLabel(cs);
//  pButton->ShowControl();

 }
 return true;
}



--
For information on using the Palm Developer Forums, or to unsubscribe, please see http://www.palmos.com/dev/support/forums/




--
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to