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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formating an EditBox

Status
Not open for further replies.

mpsoutine

Programmer
Jan 6, 2003
87
US
Hi,

I'm trying to format the date I am typing into an edit box to read mm/dd/yy. The code I have below does insert the forward slash where I want. The problem occurs when I use the back space key or arrow key to move through the text to do any additional editing.
The way I originally designed my OnChange() message handler to keep track of the number of time this method was called and insert the forward slash based on a count. This works fine. I'm also able to insert the forward slashes by keeping track of the length of the string in the edit box. But with using the "length" method I reach a point where one of the if() statements for checking the length is always true.

I tried resetting the count value in OnChar but I'm not making progress with this either. Any insight would be appreciated.

MPSoutine

void CEdSample2Dlg::OnChangeEditName()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.

// TODO: Add your control notification handler code here




static bool IgnorNextChange = 0;
char Buffer[50];

m_edDate.LimitText(8);


m_edDate.GetWindowText( Buffer, 50 );

if(!IgnorNextChange && FormatDate(Buffer) )
{




IgnorNextChange = 1;
m_edDate.SetWindowText("");
m_edDate.ReplaceSel(Buffer);
}

IgnorNextChange = 0;




}

bool CEdSample2Dlg::FormatDate(char *Buffer)
{


char Message[25] = "in formatdate()";

char NewChar[3] = "/";
char EOL[3] = "\n";

int nLen = strlen(Buffer);

nCount = nCount++;

// DD/MM/YY

if(nCount == 2)
//if(nLen == 2)///*date buffer needs modifing then*/true )
{
//modifiy the date string etc...
//AfxMessageBox(Message);

strcat(Buffer,NewChar);

int nMonth = atoi(Buffer);

if(nMonth>12)
{
AfxMessageBox("Value to large");
//char Replacement[3] = " ";
//strcpy(Buffer,Replacement);
}


return 1;
}
if(nCount == 5)
//if(nLen == 5)
{

strcat(Buffer,NewChar);
return 1;

}


// if(nLen == 8)
// {

//AfxMessageBox("You entered to many characters");
//strcat(Buffer,EOL);

// return 1;
// }
return 0;

}





 
If you are not limited to using the edit control, try using the DateTimePicker control. It has the functionality that you are describing built into it already. Additionally it presents a nice graphical method for users to choose the date.

 
Hi,

I am limited to the edit control. The end user finds typing in the date is the fastest. I'll continue to plug away with it. I'm going to assume that the OnChar() and OnChange() methods are the correct methods I need to use. Maybe keeping track of the count value is not the correct way. I can see how the count values are restricting me.

MPSoutine
 
Using a standard Edit Box to implement a date editing mechanism is not a wise choice. That’s why the Date controls were added to Windows! If you must submit to your users whims, the best thing to do is to build your own control that looks like an edit box but has strict behavior related to dates.

-pete
 
Hi,

Thanks Pete. I'm trying to format the date because it will be stored in a database. It's not important to me that it is a date. What is important is being able to add the forward slashes as I'm entering text. I could be entering characters as well. I hope I'm clear on this. I think it is going to be a fun problem to solve.

MPSoutine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top