Re: Write to an Edit Control with the << operator
Thank you so much David. This is simply amazing! Like magic... I can
recommend this piece of code to anyone!
On 5 Sep., 18:15, David Wilkinson <no-re...@effisols.com> wrote:
// untested
template <typename T>
CEdit& operator << (CEdit& edit, const T& t)
{
std::basic_ostringstream<TCHAR> os;
os << t;
// append text os.str().c_str() using SetSel() and ReplaceSel()
return edit;
}
My complete code looks like this now:
in GUI_Dlg.h I added (Outside any class)
template <typename T>
CEdit& operator << (CEdit& edit, const T& t) ;
in GUI_Dlg.cpp I added
template <typename T>
CEdit& operator << (CEdit& edit, const T& t)
{
std::basic_ostringstream<TCHAR> os;
os << t;
// Print out the new text
int nLength = edit.GetWindowTextLength();
edit.SetSel(nLength, nLength);
edit.ReplaceSel(os.str().c_str());
return edit;
}
And this is an example how I use it:
m_edit << "new text " << 10 << " " << 1234.5678 << "\r\n";
I wonder tough why endl doesn't work:
m_edit << std::endl; //Gives a compile error
Anyway, thank you again David!
Cheers
Gabriel