-
Re: The Better Solution
Well, I thought I'd leave that task to you guys... But now that you've shown me your solution, let me share mine....
Part of OOP is encapsulation, so you really want to keep everything inside the single class -- so when you reuse the CFocusEdit class, all you have to do is create an object of type CFocusEdit and include the header file.
So, in my example, just create a class member variable of type BOOL (let's call it m_bFocusFlag) and in the constructor make it FALSE -- then:
void CFocusEdit::OnLButtonDown(UINT nFlags, CPoint point)
{
CEdit::OnLButtonDown(nFlags, point);
if (!m_bFocusFlag)
{
SetSel(0, -1);
}
m_bFocusFlag = TRUE;
}
void CFocusEdit::OnKillfocus()
{
m_bFocusFlag = FALSE;
}
I updated the example on my web site to add this functionality.
(BTW - to change your name from Anonymous, edit your profile)
Rail
Recording Engineer/Software Developer
Rail Jon Rogut Software
[email protected]
http://home.earthlink.net/~railro/
-
Re: The Better Solution
Yes but the advantage to my solution is that it works no matter how the dialog recieves focus, your way only works when the user click on it with the mouse. However sticking with your OOP logic you could put the OnSetFocus() member into the CFocusEdit class, instead of the dialog class like so...
void CFocusEdit::OnLButtonDown(UINT nFlags, CPoint point)
{
if(this != GetFocus())
SetFocus();
else
CEdit::OnLButtonDown(nFlags, point);
}
void CFocusEdit::OnSetFocus(CWnd* pOldWnd)
{
CEdit::OnSetFocus(pOldWnd);
SetSel(0, -1);
}
This has the best of both worlds!!!
Dan
P.S. Even though I didn't post the original message this thread has helped me learn alot (I'm still pretty new at this.) Thanks for your help!!!
-
Re: The Better Solution
Actually your code and mine now accomplish the same end result. There are only three ways that the edit control can receive focus. Either through a SetFocus() function call, the user Tabs to the control or the user left clicks inside the edit control. Actually there is one other way, if the user right clicks inside the edit control as well - and that normally brings up the contextual menu.
The first two methods are handled by the normal CEdit functionality, when you tab to a control or focus is passed to the control via a SetFocus() function call, then the contents of the edit control are selected. However, we added this functionality for the third method by subclassing CEdit and adding a message handler for WM_LBUTTONDOWN.
So, what I'm getting at, is that you really don't need to handle OnSetFocus() to highlight the contents, because by default that already happens.
Download my updated example and compare it to your code and see if there is any difference -- there shouldn't be.
Anyhow, glad to be of assistance :^)
Best regards.
Rail
Recording Engineer/Software Developer
Rail Jon Rogut Software
[email protected]
http://home.earthlink.net/~railro/