Hi Jeronimo,
Thanks for your feedback.
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.