Edit Control: How to append text to an edit control?

原文地址在这儿,谢谢作者的分享。

下面是原文:

Edit Control: How to append text to an edit control?
Q: Which is the best method to append text to an edit control?

A: One method is to call 'GetWindowText()' to get the initial text, append the new text to it, then put it back with 'SetWindowText()'.
This works, but is not practical, especially if the text lenght is huge and the appending is made very frequently. 

A better solution is to put the edit selection at the end of the initial text, then simply replace the selection with the text to append:

Code:

void CFoo::AppendTextToEditCtrl(CEdit& edit, LPCTSTR pszText)
{
   // get the initial text length
   int nLength = edit.GetWindowTextLength();
   // put the selection at the end of text
   edit.SetSel(nLength, nLength);
   // replace the selection
   edit.ReplaceSel(pszText);
}

For appending lines in a multiline edit control we must add CR/LF to the text:

Code:
void CFoo::AppendLineToMultilineEditCtrl(CEdit& edit, LPCTSTR pszText)
{
   CString strLine;
   // add CR/LF to text
   strLine.Format(_T("\r\n%s"), pszText);
   AppendTextToEditCtrl(edit, strLine);
}

补充:还需要设置Edit Box控件的属性,选中“Styles”页面下的“Mulitline”选项。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值