Delete text in a RichTextBox

After setting RichTextBox's ReadOnly property to true, I can reproduce out
your problem with the code snippet below:
this.richTextBox1.SelectionStart = 0;
this.richTextBox1.SelectionLength =4;
this.richTextBox1.SelectedText ="";

Then I used Reflector to view the source code of RichTextBox.SelectedText
property's set accessor, which is listed below:
public virtual void set_SelectedText(string value)
{
if (base.IsHandleCreated)
{
base.SendMessage(0xc5, 0x7fff, 0);
base.SendMessage(0xc2, -1, (value == null) ? "" : value);
base.SendMessage(0xb9, 0, 0);
base.SendMessage(0xc5, this.maxLength, 0);
}
else
{
string text1 = this.Text.Substring(0, this.SelectionStart);
string text2 = "";
if ((this.SelectionStart + this.SelectionLength) <
this.Text.Length)
{
this.Text.Substring(this.SelectionStart +
this.SelectionLength);
}
base.Text = text1 + value + text2;
}
this.ClearUndo();
}
In the code, 0xc5 is EM_LIMITTEXT, 0xc2 is EM_REPLACESEL, and 0xb9 is
EM_SETMODIFY.(You can search these consts messages in WinUser.h header file)

However, I still did not see any problem with this code snippet. What is
even strange is that I rewrite this function with customized P/invoke like
this:

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam,
string lParam);

private int EM_REPLACESEL=0xc2;
private void button1_Click(object sender, System.EventArgs e)
{
this.richTextBox1.SelectionStart = 0;
this.richTextBox1.SelectionLength =4;
string test=string.Empty;

MessageBox.Show(this.richTextBox1.IsHandleCreated.ToString());
SendMessage(this.richTextBox1.Handle, 0xc5, 0x7fff, null);
SendMessage(this.richTextBox1.Handle, EM_REPLACESEL, -1, (test == null) ?
"" : test);
SendMessage(this.richTextBox1.Handle,0xb9, 0, null);
SendMessage(this.richTextBox1.Handle,0xc5, this.richTextBox1.MaxLength,
null);
this.richTextBox1.ClearUndo();
}
All works well.

I am not sure the cause of this issue. However, currently, I think we can
workaround this issue with my code snippet above. Hope this helps.
===============================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值